Skip to content

Commit

Permalink
Merge pull request #23 from bacbos/2.x
Browse files Browse the repository at this point in the history
Rely on images' metadata instead of file extension when determining image-type
  • Loading branch information
rossriley committed Oct 16, 2015
2 parents 6bea1cc + 33b444a commit ef80cd3
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/ThumbnailCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,28 @@ public function fit($parameters = array())
**/
protected function doResize($src, $width, $height, $crop = false, $fit = false, $border = false)
{
if (!list($w, $h) = getimagesize($src)) {
if (!list($w, $h, $type) = getimagesize($src)) {
return false;
}

$type = strtolower(substr(strrchr($src, '.'), 1));
if ($type == 'jpeg') {
$type = 'jpg';
}
$fileExtension = null;

switch ($type) {
case 'bmp':
switch($type)
{
case IMG_WBMP:
case IMAGETYPE_BMP:
case IMAGETYPE_WBMP:
$img = imagecreatefromwbmp($src);
$fileExtension = 'bmp';
break;
case 'gif':
case IMG_GIF:
case IMAGETYPE_GIF:
$img = imagecreatefromgif($src);
$fileExtension = 'gif';
break;
case 'jpg':
case IMG_JPG:
case IMG_JPEG:
case IMAGETYPE_JPEG:
$img = imagecreatefromjpeg($src);
// Handle exif orientation
if ($this->exifOrientation && function_exists('exif_read_data')) {
Expand All @@ -185,9 +190,12 @@ protected function doResize($src, $width, $height, $crop = false, $fit = false,
$w = imagesx($img);
$h = imagesy($img);
}
$fileExtension = 'jpg';
break;
case 'png':
case IMG_PNG:
case IMAGETYPE_PNG:
$img = imagecreatefrompng($src);
$fileExtension = 'png';
break;
default:
return false;
Expand Down Expand Up @@ -237,7 +245,7 @@ protected function doResize($src, $width, $height, $crop = false, $fit = false,

// Preserve transparency where available

if ($type == 'gif' or $type == 'png') {
if ($fileExtension == 'gif' or $fileExtension == 'png') {
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
Expand All @@ -251,7 +259,7 @@ protected function doResize($src, $width, $height, $crop = false, $fit = false,
imagecopyresampled($new, $img, 0, 0, $x, $y, $width, $height, $w, $h);
}

return $this->getOutput($new, $type);
return $this->getOutput($new, $fileExtension);
}

/**
Expand Down

0 comments on commit ef80cd3

Please sign in to comment.