Skip to content

Commit

Permalink
Merge pull request #8 from Pinpickle/efficiency-improvements
Browse files Browse the repository at this point in the history
Add browser caching and EXIF orientation support
  • Loading branch information
bobdenotter committed Dec 30, 2014
2 parents 44489bf + 033e6fe commit b6f9e67
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
101 changes: 100 additions & 1 deletion src/ThumbnailCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ThumbnailCreator implements ResizeInterface
public $defaultSource;
public $errorSource;
public $allowUpscale = false;
public $exifOrientation = true;
public $quality = 80;
public $canvas = array(255, 255, 255);

Expand Down Expand Up @@ -172,7 +173,13 @@ protected function doResize($src, $width, $height, $crop = false, $fit = false,
$img = imagecreatefromgif($src);
break;
case 'jpg':
$img = imagecreatefromjpeg($src);
if ($this->exifOrientation) {
$img = self::imageCreateFromJpegExif($src);
$w = imagesx($img);
$h = imagesy($img);
} else {
$img = imagecreatefromjpeg($src);
}
break;
case 'png':
$img = imagecreatefrompng($src);
Expand Down Expand Up @@ -282,4 +289,96 @@ protected function getOutput($imageContent, $type)

return false;
}

/**
* Create image from jpeg with EXIF orientation
*
* This function is the same as imagecreatefromjpeg except it will take into account
* the EXIF orientation in the file
*
* @param $src the path to the file
**/
public static function imageCreateFromJpegExif($src)
{
$img = imagecreatefromjpeg($src);
$exif = exif_read_data($src);
$ort = $exif['Orientation'];
switch($ort)
{
case 2: // horizontal flip
$img = self::imageFlip($img, 1);
break;
case 3: // 180 rotate left
$img = imagerotate($img, 180, 0);
break;
case 4: // vertical flip
$img = self::imageFlip($img, 0);
break;
case 5: // vertical flip + 90 rotate right
$img = self::imageFlip($img, 0);
$img = imagerotate($img, -90, 0);
break;
case 6: // 90 rotate right
$img = imagerotate($img, -90, 0);
break;
case 7: // horizontal flip + 90 rotate right
$img = self::imageFlip($img, 1);
$img = imagerotate($img, -90, 0);
break;
case 8: // 90 rotate left
$img = imagerotate($img, 90, 0);
break;
}

return $img;
}

/**
* Image Flip
*
* Based on http://stackoverflow.com/a/10001884/1136593
* Thanks Jon Grant
*
* @param $imgsrc (image to flip)
* @param $mode (0 = vertical, 1 = horizontal, 2 = both) - defaults to vertical flip
*
*/
public static function imageFlip($imgsrc, $mode = 0)
{
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);

$src_x = 0;
$src_y = 0;
$src_width = $width;
$src_height = $height;

switch ( $mode )
{
default:
case '0': //vertical
$src_y = $height -1;
$src_height= -$height;
break;
case '1': //horizontal
$src_x = $width -1;
$src_width = -$width;
break;
case '2': //both
$src_x = $width -1;
$src_y = $height -1;
$src_width = -$width;
$src_height = -$height;
break;
}

$imgdest = imagecreatetruecolor ( $width, $height );

if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height))
{
return $imgdest;
}

return $imgsrc;
}
}
16 changes: 16 additions & 0 deletions src/ThumbnailResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ThumbnailResponder
public $file;
public $action;

public $allowCache = false;
public $cacheTime = 2592000;

public $filePaths = array();

/**
Expand Down Expand Up @@ -57,6 +60,15 @@ public function initialize()
$this->resizer->setErrorSource(new File($file, false));
}

if ($this->app['config']->get('general/thumbnails/browser_cache_time')) {
$this->allowCache = true;
$this->cacheTime = $this->app['config']->get('general/thumbnails/browser_cache_time');
}

if ($this->app['config']->get('general/thumbnails/exif_orientation') !== null) {
$this->resizer->exifOrientation = $this->app['config']->get('general/thumbnails/exif_orientation');
}

if ($this->app['config']->get('general/thumbnails/allow_upscale')) {
$this->resizer->allowUpscale = $this->app['config']->get('general/thumbnails/allow_upscale');
}
Expand Down Expand Up @@ -126,6 +138,10 @@ public function respond()
$response->setContent($imageContent);
$response->headers->set('Content-Type', $this->resizer->getSource()->getMimeType());

if ($this->allowCache) {
$response->headers->set('Cache-Control', 'max-age=' . $this->cacheTime . ', public');
}

return $response;
}

Expand Down

0 comments on commit b6f9e67

Please sign in to comment.