1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-08-04 07:07:32 +02:00

prepare to tag v0.7.15

This commit is contained in:
Mikael Roos
2016-08-09 10:19:49 +02:00
parent 66c5a07767
commit 21e53887b8
5 changed files with 146 additions and 42 deletions

View File

@@ -49,14 +49,14 @@ There are several ways of installing. You either install the whole project which
The [sourcode is available on GitHub](https://github.com/mosbth/cimage). Clone, fork or [download as zip](https://github.com/mosbth/cimage/archive/master.zip). The [sourcode is available on GitHub](https://github.com/mosbth/cimage). Clone, fork or [download as zip](https://github.com/mosbth/cimage/archive/master.zip).
**Latest stable version is v0.7.14 released 2016-08-08.** **Latest stable version is v0.7.15 released 2016-08-08.**
I prefer cloning like this. Do switch to the latest stable version. I prefer cloning like this. Do switch to the latest stable version.
```bash ```bash
git clone git://github.com/mosbth/cimage.git git clone git://github.com/mosbth/cimage.git
cd cimage cd cimage
git checkout v0.7.14 git checkout v0.7.15
``` ```
Make the cache-directory writable by the webserver. Make the cache-directory writable by the webserver.
@@ -79,7 +79,7 @@ There are some all-included bundles of `img.php` that can be downloaded and used
Dowload the version of your choice like this. Dowload the version of your choice like this.
```bash ```bash
wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.14/webroot/imgp.php wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.15/webroot/imgp.php
``` ```
Open up the file in your editor and edit the array `$config`. Ensure that the paths to the image directory and the cache directory matches your environment, or create an own config-file for the script. Open up the file in your editor and edit the array `$config`. Ensure that the paths to the image directory and the cache directory matches your environment, or create an own config-file for the script.

View File

@@ -8,8 +8,8 @@ Revision history
v0.7.15 (2016-08-08) 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}`. * 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}`, #152.
* Support saving to imgp format through `sa=webp`, #132. * Limited and basic support for WEBP format, se #132.
v0.7.14 (2016-08-08) v0.7.14 (2016-08-08)

View File

@@ -38,11 +38,16 @@ $config = array(
// Version of cimage and img.php // Version of cimage and img.php
define("CIMAGE_VERSION", "v0.7.13 (2016-08-08)"); define("CIMAGE_VERSION", "v0.7.15 (2016-08-09)");
// 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);
}
/** /**
@@ -1740,7 +1745,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.');
@@ -2041,17 +2046,34 @@ 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 $info = list($this->width, $this->height, $this->fileType) = getimagesize($file);
$info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file);
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")) {
$this->fileType = exif_imagetype($file);
if ($this->fileType === false) {
if (function_exists("imagecreatefromwebp")) {
$webp = imagecreatefromwebp($file);
if ($webp !== false) {
$this->width = imagesx($webp);
$this->height = imagesy($webp);
$this->fileType = IMG_WEBP;
}
}
}
}
}
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($file) . " bytes.");
$this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); $this->log(" Image mimetype: " . $this->getMimeType());
} }
return $this; return $this;
@@ -2059,6 +2081,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.
@@ -2570,9 +2609,14 @@ class CImage
$this->setSource($src, $dir); $this->setSource($src, $dir);
} }
$this->loadImageDetails($this->pathToImage); $this->loadImageDetails();
$this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); if ($this->fileType === IMG_WEBP) {
$this->image = imagecreatefromwebp($this->pathToImage);
} else {
$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.");
} }
@@ -3433,9 +3477,11 @@ class CImage
// switch on mimetype // switch on mimetype
if (isset($this->extension)) { if (isset($this->extension)) {
return strtolower($this->extension); return strtolower($this->extension);
} else { } elseif ($this->fileType === IMG_WEBP) {
return substr(image_type_to_extension($this->fileType), 1); return "webp";
} }
return substr(image_type_to_extension($this->fileType), 1);
} }
@@ -3491,6 +3537,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}.");
@@ -3679,6 +3730,7 @@ class CImage
$format = $this->outputFormat; $format = $this->outputFormat;
} }
$this->log("### Output");
$this->log("Output format is: $format"); $this->log("Output format is: $format");
if (!$this->verbose && $format == 'json') { if (!$this->verbose && $format == 'json') {
@@ -3712,7 +3764,8 @@ class CImage
$this->fastTrackCache->addHeader($header); $this->fastTrackCache->addHeader($header);
} }
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
&& strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) {
if ($this->verbose) { if ($this->verbose) {
$this->log("304 not modified"); $this->log("304 not modified");
@@ -3727,10 +3780,8 @@ class CImage
} else { } else {
// Get details on image $this->loadImageDetails($file);
$info = getimagesize($file); $mime = $this->getMimeType();
!empty($info) or $this->raiseError("The file doesn't seem to be an image.");
$mime = $info['mime'];
$size = filesize($file); $size = filesize($file);
if ($this->verbose) { if ($this->verbose) {
@@ -3791,7 +3842,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);
@@ -3893,6 +3944,7 @@ class CImage
private function verboseOutput() private function verboseOutput()
{ {
$log = null; $log = null;
$this->log("### Summary of verbose log");
$this->log("As JSON: \n" . $this->json()); $this->log("As JSON: \n" . $this->json());
$this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M");
$this->log("Memory limit: " . ini_get('memory_limit')); $this->log("Memory limit: " . ini_get('memory_limit'));

View File

@@ -38,11 +38,16 @@ $config = array(
// Version of cimage and img.php // Version of cimage and img.php
define("CIMAGE_VERSION", "v0.7.13 (2016-08-08)"); define("CIMAGE_VERSION", "v0.7.15 (2016-08-09)");
// 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);
}
/** /**
@@ -1740,7 +1745,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.');
@@ -2041,17 +2046,34 @@ 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 $info = list($this->width, $this->height, $this->fileType) = getimagesize($file);
$info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file);
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")) {
$this->fileType = exif_imagetype($file);
if ($this->fileType === false) {
if (function_exists("imagecreatefromwebp")) {
$webp = imagecreatefromwebp($file);
if ($webp !== false) {
$this->width = imagesx($webp);
$this->height = imagesy($webp);
$this->fileType = IMG_WEBP;
}
}
}
}
}
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($file) . " bytes.");
$this->log(" Image mimetype: " . image_type_to_mime_type($this->fileType)); $this->log(" Image mimetype: " . $this->getMimeType());
} }
return $this; return $this;
@@ -2059,6 +2081,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.
@@ -2570,9 +2609,14 @@ class CImage
$this->setSource($src, $dir); $this->setSource($src, $dir);
} }
$this->loadImageDetails($this->pathToImage); $this->loadImageDetails();
$this->image = imagecreatefromstring(file_get_contents($this->pathToImage)); if ($this->fileType === IMG_WEBP) {
$this->image = imagecreatefromwebp($this->pathToImage);
} else {
$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.");
} }
@@ -3433,9 +3477,11 @@ class CImage
// switch on mimetype // switch on mimetype
if (isset($this->extension)) { if (isset($this->extension)) {
return strtolower($this->extension); return strtolower($this->extension);
} else { } elseif ($this->fileType === IMG_WEBP) {
return substr(image_type_to_extension($this->fileType), 1); return "webp";
} }
return substr(image_type_to_extension($this->fileType), 1);
} }
@@ -3491,6 +3537,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}.");
@@ -3679,6 +3730,7 @@ class CImage
$format = $this->outputFormat; $format = $this->outputFormat;
} }
$this->log("### Output");
$this->log("Output format is: $format"); $this->log("Output format is: $format");
if (!$this->verbose && $format == 'json') { if (!$this->verbose && $format == 'json') {
@@ -3712,7 +3764,8 @@ class CImage
$this->fastTrackCache->addHeader($header); $this->fastTrackCache->addHeader($header);
} }
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
&& strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) {
if ($this->verbose) { if ($this->verbose) {
$this->log("304 not modified"); $this->log("304 not modified");
@@ -3727,10 +3780,8 @@ class CImage
} else { } else {
// Get details on image $this->loadImageDetails($file);
$info = getimagesize($file); $mime = $this->getMimeType();
!empty($info) or $this->raiseError("The file doesn't seem to be an image.");
$mime = $info['mime'];
$size = filesize($file); $size = filesize($file);
if ($this->verbose) { if ($this->verbose) {
@@ -3791,7 +3842,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);
@@ -3893,6 +3944,7 @@ class CImage
private function verboseOutput() private function verboseOutput()
{ {
$log = null; $log = null;
$this->log("### Summary of verbose log");
$this->log("As JSON: \n" . $this->json()); $this->log("As JSON: \n" . $this->json());
$this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M"); $this->log("Memory peak: " . round(memory_get_peak_usage() /1024/1024) . "M");
$this->log("Memory limit: " . ini_get('memory_limit')); $this->log("Memory limit: " . ini_get('memory_limit'));

File diff suppressed because one or more lines are too long