We had a slight problem with ImageSizer recently; everything seemed to be fine but creating thumbnails still failed. The reason for this turned out to be that those images were of unsupported type (windows bitmaps) yet their extension was ".jpg" and thus ImageSizer considered them proper JPEG's and acted accordingly.
Anyway, sorry for the spam but this is the solution I ended up implementing. I'd be grateful if Ryan (or someone else who knows the core well) would take a look and tell me if this is a valid solution -- and if it is, I'd like to suggest changing current extension-based "filtering" to something more like this
I also took a look at the Upload class / InputfieldFile, but seems to me that implementing proper imagetype filtering there would require a lot of work.. and doesn't necessarily even make sense in that context.
--- wire/core/ImageSizer.php (revision 1717)
+++ wire/core/ImageSizer.php (working copy)
@@ -46,6 +46,12 @@
);
+ /**
+ * Type of image
+ *
+ */
+ protected $imagetype;
+
/**
* Allow images to be upscaled / enlarged?
*
*/
@@ -67,11 +73,10 @@
* File extensions that are supported for resizing
*
*/
- protected $supportedExtensions = array(
- 'gif',
- 'jpg',
- 'jpeg',
- 'png',
+ protected $supportedImagetypes = array(
+ IMAGETYPE_GIF,
+ IMAGETYPE_JPEG,
+ IMAGETYPE_PNG,
);
/**
@@ -83,9 +88,10 @@
$this->filename = $filename;
$p = pathinfo($filename);
$this->extension = strtolower($p['extension']);
+ $this->imagetype = exif_imagetype($filename);
$basename = $p['basename'];
- if(!in_array($this->extension, $this->supportedExtensions))
+ if(!in_array($this->imagetype, $this->supportedImagetypes))
throw new WireException("$basename is an unsupported image type");
if(!$this->loadImageInfo())
@@ -130,12 +136,9 @@
$source = $this->filename;
$dest = str_replace("." . $this->extension, "_tmp." . $this->extension, $source);
- switch($this->extension) {
- case 'gif': $image = @imagecreatefromgif($source); break;
- case 'png': $image = @imagecreatefrompng($source); break;
- case 'jpeg':
- case 'jpg': $image = @imagecreatefromjpeg($source); break;
- }
+ if($this->imagetype == IMAGETYPE_GIF) $image = @imagecreatefromgif($source);
+ if($this->imagetype == IMAGETYPE_PNG) $image = @imagecreatefrompng($source);
+ if($this->imagetype == IMAGETYPE_JPEG) $image = @imagecreatefromjpeg($source);
if(!$image) return false;
@@ -143,7 +146,7 @@
$thumb = imagecreatetruecolor($gdWidth, $gdHeight);
- if($this->extension == 'png') { // Adam's PNG transparency fix
+ if($this->imagetype == IMAGETYPE_PNG) { // Adam's PNG transparency fix
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
} else {
@@ -155,7 +158,7 @@
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $gdWidth, $gdHeight, $this->image['width'], $this->image['height']);
$thumb2 = imagecreatetruecolor($targetWidth, $targetHeight);
- if($this->extension == 'png') {
+ if($this->imagetype == IMAGETYPE_PNG) {
imagealphablending($thumb2, false);
imagesavealpha($thumb2, true);
} else {
@@ -170,20 +173,13 @@
imagecopyresampled($thumb2, $thumb, 0, 0, $w1, $h1, $targetWidth, $targetHeight, $targetWidth, $targetHeight);
// write to file
- switch($this->extension) {
- case 'gif':
- imagegif($thumb2, $dest);
- break;
- case 'png':
- // convert 1-100 (worst-best) scale to 0-9 (best-worst) scale for PNG
- $quality = round(abs(($this->quality - 100) / 11.111111));
- imagepng($thumb2, $dest, $quality);
- break;
- case 'jpeg':
- case 'jpg':
- imagejpeg($thumb2, $dest, $this->quality);
- break;
+ if($this->imagetype == IMAGETYPE_GIF) imagegif($thumb2, $dest);
+ if($this->imagetype == IMAGETYPE_PNG) {
+ // convert 1-100 (worst-best) scale to 0-9 (best-worst) scale for PNG
+ $quality = round(abs(($this->quality - 100) / 11.111111));
+ imagepng($thumb2, $dest, $quality);
}
+ if($this->imagetype == IMAGETYPE_JPEG) imagejpeg($thumb2, $dest, $this->quality);
unlink($source);
rename($dest, $source);
... and yes, that's output from SVN diff. I hope you guys can cope with that / understand what's going on in there. I'm not very familiar with Git and how it handles stuff like this. Sorry.