Created
January 31, 2018 06:55
-
-
Save neufeind/9dc00796ff01935debab78bff6deffaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SpeedPartner; | |
class ImageResizeCached extends \Gumlet\ImageResize | |
{ | |
protected $source_image_filename; | |
private function loadSourceImage() | |
{ | |
switch ($this->source_type) { | |
case IMAGETYPE_GIF: | |
$this->source_image = imagecreatefromgif($this->source_image_filename); | |
break; | |
case IMAGETYPE_JPEG: | |
$this->source_image = $this->imageCreateJpegfromExif($this->source_image_filename); | |
// set new width and height for image, maybe it has changed | |
$this->original_w = ImageSX($this->source_image); | |
$this->original_h = ImageSY($this->source_image); | |
break; | |
case IMAGETYPE_PNG: | |
$this->source_image = imagecreatefrompng($this->source_image_filename); | |
break; | |
case IMAGETYPE_WEBP: | |
if (version_compare(PHP_VERSION, '5.5.0', '<')) { | |
throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required'); | |
} | |
$this->source_image = imagecreatefromwebp($this->source_image_filename); | |
break; | |
default: | |
throw new ImageResizeException('Unsupported image type'); | |
break; | |
} | |
if (!$this->source_image) { | |
throw new ImageResizeException('Could not load image'); | |
} | |
} | |
/** | |
* Loads image source and its properties to the instanciated object | |
* | |
* Similar to original constructor but with loading of the source-image defered | |
* | |
* @param string $filename | |
* @param boolean $deferLoadingSourceImage | |
* @return ImageResize | |
* @throws ImageResizeException | |
*/ | |
public function __construct($filename, $deferLoadingSourceImage = true) | |
{ | |
if (!defined('IMAGETYPE_WEBP')) { | |
define('IMAGETYPE_WEBP', 18); | |
} | |
if ($filename === null || empty($filename) || (substr($filename, 0, 7) !== 'data://' && !is_file($filename))) { | |
throw new ImageResizeException('File does not exist'); | |
} | |
$finfo = finfo_open(FILEINFO_MIME_TYPE); | |
if (strstr(finfo_file($finfo, $filename), 'image') === false) { | |
throw new ImageResizeException('Unsupported file type'); | |
} | |
if (!$image_info = getimagesize($filename, $this->source_info)) { | |
$image_info = getimagesize($filename); | |
} | |
if (!$image_info) { | |
throw new ImageResizeException('Could not read file'); | |
} | |
list( | |
$this->original_w, | |
$this->original_h, | |
$this->source_type | |
) = $image_info; | |
$this->source_image_filename = $filename; | |
if (!$deferLoadingSourceImage) { | |
$this->loadSourceImage(); | |
} | |
return $this->resize($this->getSourceWidth(), $this->getSourceHeight()); | |
} | |
/** | |
* Returns temp-path to new image (from cache or freshly created) | |
* | |
* @param string $cache_path | |
* @param string $filename | |
* @param string $image_type | |
* @param integer $quality | |
* @param integer $permissions | |
* @return string | |
*/ | |
public function getProcessedFilepath($cache_path, $filename = null, $image_type = null, $quality = null, $permissions = null) | |
{ | |
$filename = $filename ?: $this->source_image_filename; | |
$processingHashParts = array( | |
$this->source_image_filename, | |
filemtime($this->source_image_filename), | |
$this->source_x, | |
$this->source_y, | |
$this->dest_w, | |
$this->dest_h, | |
$this->source_w, | |
$this->source_h, | |
$filename, | |
$image_type, | |
$quality, | |
$permissions, | |
); | |
$processingHash = md5(serialize($processingHashParts)); | |
$filenamePathinfo = pathinfo($filename); | |
$cacheFilename = $filenamePathinfo['filename'] . '.' . $processingHash . '.' . $filenamePathinfo['extension']; | |
$cacheFilenameWithPath = $cache_path . '/' . $cacheFilename; | |
if (file_exists($cacheFilenameWithPath)) { | |
return $cacheFilenameWithPath; | |
} | |
if (!$this->source_image) { | |
$this->loadSourceImage(); | |
} | |
$this->save($cacheFilenameWithPath, $image_type, $quality, $permissions); | |
return $cacheFilenameWithPath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment