.circleci
.github
app
chewy
controllers
helpers
javascript
fonts
images
mastodon
actions
components
containers
features
locales
middleware
reducers
selectors
service_worker
storage
store
utils
__tests__
base64.js
html.js
numbers.js
resize_image.js
api.js
base_polyfills.js
common.js
compare_id.js
extra_polyfills.js
initial_state.js
is_mobile.js
load_polyfills.js
main.js
performance.js
ready.js
rtl.js
scroll.js
settings.js
stream.js
test_setup.js
uuid.js
packs
styles
lib
mailers
models
policies
presenters
serializers
services
validators
views
workers
bin
config
db
dist
lib
log
nanobox
public
spec
streaming
vendor
.buildpacks
.codeclimate.yml
.dockerignore
.editorconfig
.env.nanobox
.env.production.sample
.env.test
.env.vagrant
.eslintignore
.eslintrc.js
.foreman
.gitattributes
.gitignore
.haml-lint.yml
.nanoignore
.nvmrc
.profile
.rspec
.rubocop.yml
.ruby-version
.scss-lint.yml
.slugignore
.yarnclean
AUTHORS.md
Aptfile
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Capfile
Dockerfile
Gemfile
Gemfile.lock
LICENSE
Procfile
Procfile.dev
README.md
Rakefile
Vagrantfile
app.json
babel.config.js
boxfile.yml
config.ru
docker-compose.yml
package.json
postcss.config.js
priv-config
scalingo.json
yarn.lock
* Add WebP support * Remove the changes to the tooltip refs: https://github.com/tootsuite/mastodon/pull/9879#pullrequestreview-199312528
107 lines
2.8 KiB
JavaScript
107 lines
2.8 KiB
JavaScript
import EXIF from 'exif-js';
|
|
|
|
const MAX_IMAGE_PIXELS = 1638400; // 1280x1280px
|
|
|
|
const getImageUrl = inputFile => new Promise((resolve, reject) => {
|
|
if (window.URL && URL.createObjectURL) {
|
|
try {
|
|
resolve(URL.createObjectURL(inputFile));
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const reader = new FileReader();
|
|
reader.onerror = (...args) => reject(...args);
|
|
reader.onload = ({ target }) => resolve(target.result);
|
|
|
|
reader.readAsDataURL(inputFile);
|
|
});
|
|
|
|
const loadImage = inputFile => new Promise((resolve, reject) => {
|
|
getImageUrl(inputFile).then(url => {
|
|
const img = new Image();
|
|
|
|
img.onerror = (...args) => reject(...args);
|
|
img.onload = () => resolve(img);
|
|
|
|
img.src = url;
|
|
}).catch(reject);
|
|
});
|
|
|
|
const getOrientation = (img, type = 'image/png') => new Promise(resolve => {
|
|
if (!['image/jpeg', 'image/webp'].includes(type)) {
|
|
resolve(1);
|
|
return;
|
|
}
|
|
|
|
EXIF.getData(img, () => {
|
|
const orientation = EXIF.getTag(img, 'Orientation');
|
|
resolve(orientation);
|
|
});
|
|
});
|
|
|
|
const processImage = (img, { width, height, orientation, type = 'image/png' }) => new Promise(resolve => {
|
|
const canvas = document.createElement('canvas');
|
|
|
|
if (4 < orientation && orientation < 9) {
|
|
canvas.width = height;
|
|
canvas.height = width;
|
|
} else {
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
}
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
switch (orientation) {
|
|
case 2: context.transform(-1, 0, 0, 1, width, 0); break;
|
|
case 3: context.transform(-1, 0, 0, -1, width, height); break;
|
|
case 4: context.transform(1, 0, 0, -1, 0, height); break;
|
|
case 5: context.transform(0, 1, 1, 0, 0, 0); break;
|
|
case 6: context.transform(0, 1, -1, 0, height, 0); break;
|
|
case 7: context.transform(0, -1, -1, 0, height, width); break;
|
|
case 8: context.transform(0, -1, 1, 0, 0, width); break;
|
|
}
|
|
|
|
context.drawImage(img, 0, 0, width, height);
|
|
|
|
canvas.toBlob(resolve, type);
|
|
});
|
|
|
|
const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => {
|
|
const { width, height } = img;
|
|
|
|
const newWidth = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (width / height)));
|
|
const newHeight = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (height / width)));
|
|
|
|
getOrientation(img, type)
|
|
.then(orientation => processImage(img, {
|
|
width: newWidth,
|
|
height: newHeight,
|
|
orientation,
|
|
type,
|
|
}))
|
|
.then(resolve)
|
|
.catch(reject);
|
|
});
|
|
|
|
export default inputFile => new Promise((resolve, reject) => {
|
|
if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
|
|
resolve(inputFile);
|
|
return;
|
|
}
|
|
|
|
loadImage(inputFile).then(img => {
|
|
if (img.width * img.height < MAX_IMAGE_PIXELS) {
|
|
resolve(inputFile);
|
|
return;
|
|
}
|
|
|
|
resizeImage(img, inputFile.type)
|
|
.then(resolve)
|
|
.catch(() => resolve(inputFile));
|
|
}).catch(reject);
|
|
});
|