mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-04 07:07:32 +02:00
test support for webp images
This commit is contained in:
86
CImage.php
86
CImage.php
@@ -640,7 +640,7 @@ class CImage
|
|||||||
*/
|
*/
|
||||||
private function checkFileExtension($extension)
|
private function checkFileExtension($extension)
|
||||||
{
|
{
|
||||||
$valid = array('jpg', 'jpeg', 'png', 'gif');
|
$valid = array('jpg', 'jpeg', 'png', 'gif', 'webp');
|
||||||
|
|
||||||
in_array(strtolower($extension), $valid)
|
in_array(strtolower($extension), $valid)
|
||||||
or $this->raiseError('Not a valid file extension.');
|
or $this->raiseError('Not a valid file extension.');
|
||||||
@@ -663,7 +663,7 @@ class CImage
|
|||||||
|
|
||||||
if ($extension == 'jpeg') {
|
if ($extension == 'jpeg') {
|
||||||
$extension = 'jpg';
|
$extension = 'jpg';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $extension;
|
return $extension;
|
||||||
}
|
}
|
||||||
@@ -941,17 +941,56 @@ class CImage
|
|||||||
is_readable($file)
|
is_readable($file)
|
||||||
or $this->raiseError('Image file does not exist.');
|
or $this->raiseError('Image file does not exist.');
|
||||||
|
|
||||||
// Get details on image
|
return $this->getImageDetails();
|
||||||
$info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get image details.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function getImageDetails()
|
||||||
|
{
|
||||||
|
$info = list($this->width, $this->height, $this->fileType) = getimagesize($this->pathToImage);
|
||||||
if (empty($info)) {
|
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) {
|
if ($this->verbose) {
|
||||||
$this->log("Loading image details for: {$file}");
|
$this->log("Loading image details for: {$file}");
|
||||||
$this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType}).");
|
$this->log(" Image width x height (type): {$this->width} x {$this->height} ({$this->fileType}).");
|
||||||
$this->log(" Image filesize: " . filesize($file) . " bytes.");
|
$this->log(" Image filesize: " . filesize($this->pathToImage) . " bytes.");
|
||||||
$this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType));
|
$this->log(" Image mimetype: " . $this->getMimeType());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
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
|
* Init new width and height and do some sanity checks on constraints, before any
|
||||||
* processing can be done.
|
* processing can be done.
|
||||||
@@ -1470,13 +1526,18 @@ class CImage
|
|||||||
$this->setSource($src, $dir);
|
$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) {
|
if ($this->image === false) {
|
||||||
throw new Exception("Could not load image.");
|
throw new Exception("Could not load image.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->getImageDetails();
|
||||||
|
|
||||||
|
|
||||||
/* Removed v0.7.7
|
/* Removed v0.7.7
|
||||||
if (image_type_to_mime_type($this->fileType) == 'image/png') {
|
if (image_type_to_mime_type($this->fileType) == 'image/png') {
|
||||||
$type = $this->getPngType();
|
$type = $this->getPngType();
|
||||||
@@ -2391,6 +2452,11 @@ class CImage
|
|||||||
imagegif($this->image, $this->cacheFileName);
|
imagegif($this->image, $this->cacheFileName);
|
||||||
break;
|
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':
|
case 'png':
|
||||||
default:
|
default:
|
||||||
$this->Log("Saving image as PNG to cache using compression = {$this->compress}.");
|
$this->Log("Saving image as PNG to cache using compression = {$this->compress}.");
|
||||||
@@ -2691,7 +2757,7 @@ class CImage
|
|||||||
$this->load($file);
|
$this->load($file);
|
||||||
|
|
||||||
$details['filename'] = basename($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['width'] = $this->width;
|
||||||
$details['height'] = $this->height;
|
$details['height'] = $this->height;
|
||||||
$details['aspectRatio'] = round($this->width / $this->height, 3);
|
$details['aspectRatio'] = round($this->width / $this->height, 3);
|
||||||
|
@@ -5,10 +5,18 @@ Revision history
|
|||||||
[](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/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)
|
v0.7.14 (2016-08-08)
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
* Re-add removed cache directory.
|
* Re-add removed cache directory.
|
||||||
|
* Make fast track cache disabled by default in the config file.
|
||||||
|
|
||||||
|
|
||||||
v0.7.13 (2016-08-08)
|
v0.7.13 (2016-08-08)
|
||||||
|
@@ -4,3 +4,8 @@ define("CIMAGE_VERSION", "v0.7.13 (2016-08-08)");
|
|||||||
|
|
||||||
// For CRemoteImage
|
// For CRemoteImage
|
||||||
define("CIMAGE_USER_AGENT", "CImage/" . CIMAGE_VERSION);
|
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);
|
||||||
|
}
|
||||||
|
@@ -15,7 +15,6 @@ echo "Extension imagick is $no loaded.<br>";
|
|||||||
|
|
||||||
$no = extension_loaded('gd') ? null : 'NOT';
|
$no = extension_loaded('gd') ? null : 'NOT';
|
||||||
echo "Extension gd is $no loaded.<br>";
|
echo "Extension gd is $no loaded.<br>";
|
||||||
|
|
||||||
if (!$no) {
|
if (!$no) {
|
||||||
echo "<pre>", var_dump(gd_info()), "</pre>";
|
echo "<pre>", var_dump(gd_info()), "</pre>";
|
||||||
}
|
}
|
||||||
|
@@ -41,7 +41,7 @@ return array(
|
|||||||
* mode: 'production'
|
* mode: 'production'
|
||||||
*/
|
*/
|
||||||
//'mode' => 'production',
|
//'mode' => 'production',
|
||||||
//'mode' => 'development',
|
'mode' => 'development',
|
||||||
//'mode' => 'strict',
|
//'mode' => 'strict',
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user