Permalink
Browse files

fix: don’t resize layout images (except their thumbnails)

  • Loading branch information...
1 parent 4b451cd commit ee28a1466a6ee357eb93a41589209202f4245920 @p3k p3k committed Apr 9, 2016
Showing with 35 additions and 18 deletions.
  1. +35 −18 code/Image/Image.js
View
@@ -258,6 +258,7 @@ Image.prototype.update = function(data) {
var mime = data.file;
var origin = data.file_origin;
+ var isLayout = (this.parent_type === 'Layout' || path && !!path.layout);
if (!mime || mime.contentLength < 1) {
if (origin && origin !== this.origin) {
@@ -287,10 +288,10 @@ Image.prototype.update = function(data) {
throw Error(gettext('This does not seem to be a valid JPG, PNG or GIF image.'));
}
- this.origin = origin;
var mimeName = mime.normalizeFilename(mime.name);
this.contentLength = mime.contentLength;
this.contentType = mime.contentType;
+ this.origin = origin;
if (!this.name) {
var name = File.getName(data.name) || mimeName.split('.')[0];
@@ -301,13 +302,15 @@ Image.prototype.update = function(data) {
data.description = gettext('Source: {0}', origin);
}
- var image = this.getConstraint(mime, res.handlers.site.imageDimensionLimits);
- this.height = image.height;
+ var thumbnail;
+ var image = this.getHelmaImage(mime, isLayout ? null :
+ res.handlers.site.imageDimensionLimits);
this.width = image.width;
+ this.height = image.height;
- var thumbnail;
- if (image.width > Image.THUMBNAILWIDTH) {
- thumbnail = this.getConstraint(mime, [Image.THUMBNAILWIDTH]);
+ // Create a thumbnail version if the image size exceeds
+ if (this.width > Image.THUMBNAILWIDTH) {
+ thumbnail = this.getHelmaImage(mime, [Image.THUMBNAILWIDTH]);
this.thumbnailWidth = thumbnail.width;
this.thumbnailHeight = thumbnail.height;
} else if (this.isPersistent()) {
@@ -318,22 +321,26 @@ Image.prototype.update = function(data) {
// Make the image persistent before proceeding with writing files and
// setting tags (also see Helma bug #607)
- this.isTransient() && this.persist();
-
+ if (this.isTransient()) this.persist();
var fileName = this.name + extension;
+
+ // Remove existing image files if the file name has changed
if (fileName !== this.fileName) {
- // Remove existing image files if the file name has changed
this.removeFiles();
}
+
this.fileName = fileName;
- thumbnail && (this.thumbnailName = this.name + '_small' + extension);
- this.writeFiles(image.resized || mime, thumbnail && thumbnail.resized);
- image.resized && (this.contentLength = this.getFile().getLength());
+ if (thumbnail) this.thumbnailName = this.name + '_small' + extension;
+
+ this.writeFiles(image, thumbnail);
+ this.contentLength = this.getFile().getLength();
}
- if (this.parent_type !== 'Layout') {
+ // Layout images cannot be tagged
+ if (!isLayout) {
this.setTags(data.tags || data.tag_array);
}
+
this.description = data.description;
this.touch();
return;
@@ -496,30 +503,40 @@ Image.prototype.getJSON = function() {
* @throws {Error}
* @returns {Object}
*/
-Image.prototype.getConstraint = function(mime, dimensionLimits) {
- var maxWidth = dimensionLimits[0] || Infinity;
- var maxHeight = dimensionLimits[1] || Infinity;
+Image.prototype.getHelmaImage = function(mime, dimensionLimits) {
+ if (!dimensionLimits) dimensionLimits = [Infinity, Infinity];
+
+ var maxWidth = dimensionLimits[0];
+ var maxHeight = dimensionLimits[1];
+
try {
var image = new helma.Image(mime.inputStream);
+
var factorH = 1, factorV = 1;
+
if (maxWidth && image.width > maxWidth) {
factorH = maxWidth / image.width;
}
+
if (maxHeight && image.height > maxHeight) {
factorV = maxHeight / image.height;
}
+
if (factorH !== 1 || factorV !== 1) {
var width = Math.ceil(image.width *
(factorH < factorV ? factorH : factorV));
var height = Math.ceil(image.height *
(factorH < factorV ? factorH : factorV));
+
image.resize(width, height);
+
if (mime.contentType.endsWith('gif')) {
image.reduceColors(256);
}
- return {resized: image, width: image.width, height: image.height};
}
- return {width: image.width, height: image.height};
+
+ return image;
+
} catch (ex) {
app.log(ex);
throw Error(gettext('Could not resize the image.'));

0 comments on commit ee28a14

Please sign in to comment.