From 3d0b25abe0b294de4274913ea83a76efdc83984d Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 8 Aug 2016 16:13:51 +0200 Subject: [PATCH] test support for webp images --- CImage.php | 86 +++++++++++++++++++++++++++++++++++----- REVISION.md | 8 ++++ defines.php | 5 +++ webroot/check_system.php | 1 - webroot/img_config.php | 2 +- 5 files changed, 90 insertions(+), 12 deletions(-) diff --git a/CImage.php b/CImage.php index 38a1cb0..0502e9c 100644 --- a/CImage.php +++ b/CImage.php @@ -640,7 +640,7 @@ class CImage */ private function checkFileExtension($extension) { - $valid = array('jpg', 'jpeg', 'png', 'gif'); + $valid = array('jpg', 'jpeg', 'png', 'gif', 'webp'); in_array(strtolower($extension), $valid) or $this->raiseError('Not a valid file extension.'); @@ -663,7 +663,7 @@ class CImage if ($extension == 'jpeg') { $extension = 'jpg'; - } + } return $extension; } @@ -941,17 +941,56 @@ class CImage is_readable($file) or $this->raiseError('Image file does not exist.'); - // Get details on image - $info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file); + return $this->getImageDetails(); + } + + + + /** + * Get image details. + * + * @return $this + * @throws Exception + */ + protected function getImageDetails() + { + $info = list($this->width, $this->height, $this->fileType) = getimagesize($this->pathToImage); if (empty($info)) { - throw new Exception("The file doesn't seem to be a valid image."); + // To support webp + $this->fileType = false; + if (function_exists("exif_imagetype")) { + var_dump("has exif"); + $this->fileType = exif_imagetype($this->pathToImage); + var_dump($this->fileType); + if ($this->fileType === false) { + if (function_exists("imagecreatefromwebp")) { + var_dump("has imagecreatefromwebp"); + + //die("before"); + $webp = imagecreatefromwebp($this->pathToImage); + var_dump($webp); + die(); + if ($webp !== false) { + $this->width = imagesx($webp); + $this->height = imagesy($webp); + $this->fileType = IMG_WEBP; + } + die(); + + } + } + } + } + + if (!$this->fileType) { + throw new Exception("Loading image details, the file doesn't seem to be a valid image."); } if ($this->verbose) { $this->log("Loading image details for: {$file}"); $this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType})."); - $this->log(" Image filesize: " . filesize($file) . " bytes."); - $this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); + $this->log(" Image filesize: " . filesize($this->pathToImage) . " bytes."); + $this->log(" Image mimetype: " . $this->getMimeType()); } return $this; @@ -959,6 +998,23 @@ class CImage + /** + * Get mime type for image type. + * + * @return $this + * @throws Exception + */ + protected function getMimeType() + { + if ($this->fileType === IMG_WEBP) { + return "image/webp"; + } + + return image_type_to_mime_type($this->fileType); + } + + + /** * Init new width and height and do some sanity checks on constraints, before any * processing can be done. @@ -1470,13 +1526,18 @@ class CImage $this->setSource($src, $dir); } - $this->loadImageDetails($this->pathToImage); + is_readable($this->pathToImage) + or $this->raiseError('Image file does not exist.'); - $this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); + $imageAsString = file_get_contents($this->pathToImage); + $this->image = imagecreatefromstring($imageAsString); if ($this->image === false) { throw new Exception("Could not load image."); } + $this->getImageDetails(); + + /* Removed v0.7.7 if (image_type_to_mime_type($this->fileType) == 'image/png') { $type = $this->getPngType(); @@ -2391,6 +2452,11 @@ class CImage imagegif($this->image, $this->cacheFileName); break; + case 'webp': + $this->Log("Saving image as WEBP to cache using quality = {$this->quality}."); + imagewebp($this->image, $this->cacheFileName, $this->quality); + break; + case 'png': default: $this->Log("Saving image as PNG to cache using compression = {$this->compress}."); @@ -2691,7 +2757,7 @@ class CImage $this->load($file); $details['filename'] = basename($file); - $details['mimeType'] = image_type_to_mime_type($this->fileType); + $details['mimeType'] = $this->getMimeType($this->fileType); $details['width'] = $this->width; $details['height'] = $this->height; $details['aspectRatio'] = round($this->width / $this->height, 3); diff --git a/REVISION.md b/REVISION.md index 2c47505..2e688bc 100644 --- a/REVISION.md +++ b/REVISION.md @@ -5,10 +5,18 @@ Revision history [![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master) +v0.7.15 (2016-08-08) +------------------------------------- + +* Added the [Lenna/Lena sample image](http://www.cs.cmu.edu/~chuck/lennapg/) as tif and created a png, jpeg and webp version using Imagick convert `convert lena.tif lena.{png,jpg,webp}`. +* Support saving to imgp format through `sa=webp`, #132. + + v0.7.14 (2016-08-08) ------------------------------------- * Re-add removed cache directory. +* Make fast track cache disabled by default in the config file. v0.7.13 (2016-08-08) diff --git a/defines.php b/defines.php index b64ecbe..2e7ed52 100644 --- a/defines.php +++ b/defines.php @@ -4,3 +4,8 @@ define("CIMAGE_VERSION", "v0.7.13 (2016-08-08)"); // For CRemoteImage define("CIMAGE_USER_AGENT", "CImage/" . CIMAGE_VERSION); + +// Image type IMG_WEBP is only defined from 5.6.25 +if (!defined("IMG_WEBP")) { + define("IMG_WEBP", -1); +} diff --git a/webroot/check_system.php b/webroot/check_system.php index 2ae22b9..b101998 100644 --- a/webroot/check_system.php +++ b/webroot/check_system.php @@ -15,7 +15,6 @@ echo "Extension imagick is $no loaded.
"; $no = extension_loaded('gd') ? null : 'NOT'; echo "Extension gd is $no loaded.
"; - if (!$no) { echo "
", var_dump(gd_info()), "
"; } diff --git a/webroot/img_config.php b/webroot/img_config.php index df1d5fa..9654dd5 100644 --- a/webroot/img_config.php +++ b/webroot/img_config.php @@ -41,7 +41,7 @@ return array( * mode: 'production' */ //'mode' => 'production', - //'mode' => 'development', + 'mode' => 'development', //'mode' => 'strict',