1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-08-24 16:42:48 +02:00

Compare commits

...

49 Commits

Author SHA1 Message Date
Mikael Roos
b44be78f06 Fix deprecation notice on "Creation of dynamic property" for PHP 8.2. 2023-10-27 10:20:16 +02:00
Mikael Roos
1056f0a5ee Spelling in README 2022-11-17 16:13:56 +01:00
Mikael Roos
1666ea1412 Build to prepare to tag 2022-11-17 16:08:48 +01:00
Mikael Roos
bb57af697b Remove build status from README (since it is not up to date) 2022-11-17 16:06:25 +01:00
Mikael Roos
55ce23ae5e Add fix for WINDOWS2WSL 2022-11-17 16:05:25 +01:00
Mikael Roos
4589b3b3cd Enable configuration fix for solving Windows 2 WSL2 issue with is_readable/is_writable #189 2022-11-17 15:40:43 +01:00
Mikael Roos
d5ca10cebc Add a webpage with links useful to test various aspects of img.php 2022-11-17 15:31:40 +01:00
Mikael Roos
9d7343a2df Merge pull request #188 from niciz/patch-1
Update CHttpGet.php for php 8.1 deprecated notice
2022-11-08 13:50:12 +01:00
niciz
3d7adcdbde Update CHttpGet.php for php 8.1 deprecated notice 2022-11-08 12:23:07 +01:00
Mikael Roos
80cd4e092f v0.8.4 (2022-05-30) 2022-05-30 15:32:20 +02:00
Mikael Roos
4c0ac8ed23 Support PHP 8.1 and remove deprecated messages when run in in development mode 2022-05-24 16:41:01 +02:00
Mikael Roos
39b86628db Generate prebuilt all include files for various settings 2022-05-24 16:38:48 +02:00
Mikael Roos
5f4280d387 Fix deprecated for PHP 8.1 2022-05-24 16:37:50 +02:00
Mikael Roos
dd315dbd21 Fix deprecated for PHP 8.1 2022-05-24 16:33:08 +02:00
Mikael Roos
493118e1c5 Add php version as output in verbose mode 2022-05-24 12:57:27 +02:00
Mikael Roos
e41b3d9877 Add PHP 81 as test environment 2022-05-24 12:57:08 +02:00
Mikael Roos
bb60001c36 Prepare to tag 2021-10-27 11:40:40 +02:00
Mikael Roos
adeca3f9f9 Prepare to tag 2021-10-27 11:40:18 +02:00
Mikael Roos
a5662690fe Remove bad configuration 2021-10-27 11:21:29 +02:00
Mikael Roos
3cfa9a6a98 Add htmlentities to escape input, fix #183 2021-09-13 08:17:47 +02:00
Mikael Roos
4ecebcd5b4 Add a security policy #183 2021-09-09 13:51:13 +02:00
Mikael Roos
9196d1ee41 Update the docker-compose.yaml to include PHP 8.0, #181 2021-05-10 16:01:23 +02:00
Mikael Roos
0249056761 Updated version number in define.php. 2020-06-08 12:38:44 +02:00
Mikael Roos
4ea72be49a Enable to set JPEG image as interlaced, implement feature #177. 2020-06-08 12:26:40 +02:00
Mikael Roos
2ce1f18fe5 Set PHP 7.0 as precondition (to prepare to update the codebase). 2020-06-08 12:26:01 +02:00
Mikael Roos
0f1f537b62 Add function getValue() to read from querystring. 2020-06-08 12:25:47 +02:00
Mikael Roos
1411adc828 Fix error in composer.json 2020-05-06 09:51:12 +02:00
Mikael Roos
86737af69e Update composer.json and move ext-gd from required to suggested to ease installation where cli does not have all extensions installed. 2020-05-06 09:06:46 +02:00
Mikael Roos
c563275ed5 Support PHP 7.4, some minor fixes with notices. 2020-01-15 16:51:39 +01:00
Mikael Roos
8fec09b195 Merge pull request #176 from SpaceLenore/patch-1
fixed markdown syntax
2018-10-28 19:08:45 +01:00
Lenore
ac16343cd7 fixed markdown syntax
added spaces so the markdown gets parsed correctly
2018-10-28 17:49:35 +01:00
Mikael Roos
cd142c5880 prepare to tag 2017-11-06 10:24:47 +01:00
Mikael Roos
91dd92d483 Remove webroot/img/{round8.PNG,wider.JPEG,wider.JPG} to avoid unzip warning message when installing with composer. 2017-11-06 10:24:16 +01:00
Mikael Roos
4b64d921d1 Adding docker-compose.yml #169. 2017-06-26 10:30:21 +02:00
Mikael Roos
4211d7e0e6 Adding docker-compose.yml #169. 2017-06-26 10:30:08 +02:00
Mikael Roos
dd8878c8bd add issue number 2017-03-31 00:55:38 +02:00
Mikael Roos
f9604518e4 prepare to tag v0.7.19 2017-03-31 00:47:55 +02:00
Mikael Roos
61aa52854e Move exception handler from functions.php to img.php. 2017-03-31 00:46:43 +02:00
Mikael Roos
401478c839 Correct XSS injection in check_system.php. 2016-08-31 15:26:14 +02:00
Mikael Roos
f0ab9479d6 read lena.tif, will need it in the future 2016-08-13 18:33:09 +02:00
Mikael Roos
9ff7a61ca9 remove tif version of lena 2016-08-13 17:49:36 +02:00
Mikael Roos
3170beb832 Composer suggests ext-imagick and ext-curl. 2016-08-11 17:39:45 +02:00
Mikael Roos
8001f72a1a prepare to tag v0.7.18 2016-08-09 13:27:46 +02:00
Mikael Roos
0f9e0220f1 Made &lossless a requirement to not use the original image. 2016-08-09 13:27:30 +02:00
Mikael Roos
e59ef91991 prepare to tag v0.7.17 2016-08-09 13:23:04 +02:00
Mikael Roos
2337dbe94c Made &lossless part of the generated cache filename. 2016-08-09 13:22:33 +02:00
Mikael Roos
c5de59a754 prepare to tag v0.7.16 2016-08-09 13:02:25 +02:00
Mikael Roos
7ab19d39d6 adding support for pngquant 2016-08-09 13:01:38 +02:00
Mikael Roos
9f6cba9292 changed date of release 2016-08-09 10:21:41 +02:00
22 changed files with 971 additions and 201 deletions

View File

@@ -51,6 +51,15 @@ class CCache
return $path;
}
if ($create && defined('WINDOWS2WSL')) {
// Special case to solve Windows 2 WSL integration
$path = $this->path . "/" . $subdir;
if (mkdir($path)) {
return realpath($path);
}
}
if ($create && is_writable($this->path)) {
$path = $this->path . "/" . $subdir;

View File

@@ -215,7 +215,7 @@ class CHttpGet
{
$type = isset($this->response['header']['Content-Type'])
? $this->response['header']['Content-Type']
: null;
: '';
return preg_match('#[a-z]+/[a-z]+#', $type)
? $type

View File

@@ -6,6 +6,7 @@
* @example http://dbwebb.se/opensource/cimage
* @link https://github.com/mosbth/cimage
*/
#[AllowDynamicProperties]
class CImage
{
@@ -155,6 +156,13 @@ class CImage
/**
* Do lossy output using external postprocessing tools.
*/
private $lossy = null;
/**
* Verbose mode to print out a trace and display the created image
*/
@@ -190,7 +198,15 @@ class CImage
/**
* Path to command for filter optimize, for example optipng or null.
* Path to command for lossy optimize, for example pngquant.
*/
private $pngLossy;
private $pngLossyCmd;
/**
* Path to command for filter optimize, for example optipng.
*/
private $pngFilter;
private $pngFilterCmd;
@@ -198,7 +214,7 @@ class CImage
/**
* Path to command for deflate optimize, for example pngout or null.
* Path to command for deflate optimize, for example pngout.
*/
private $pngDeflate;
private $pngDeflateCmd;
@@ -408,6 +424,13 @@ class CImage
/*
* Use interlaced progressive mode for JPEG images.
*/
private $interlace = false;
/*
* Image copy strategy, defaults to RESAMPLE.
*/
@@ -657,9 +680,9 @@ class CImage
*
* @return string $extension as a normalized file extension.
*/
private function normalizeFileExtension($extension = null)
private function normalizeFileExtension($extension = "")
{
$extension = strtolower($extension ? $extension : $this->extension);
$extension = strtolower($extension ? $extension : $this->extension ?? "");
if ($extension == 'jpeg') {
$extension = 'jpg';
@@ -823,10 +846,14 @@ class CImage
'blur' => null,
'convolve' => null,
'rotateAfter' => null,
'interlace' => null,
// Output format
'outputFormat' => null,
'dpr' => 1,
// Postprocessing using external tools
'lossy' => null,
);
// Convert crop settings from string to array
@@ -938,8 +965,11 @@ class CImage
{
$file = $file ? $file : $this->pathToImage;
is_readable($file)
or $this->raiseError('Image file does not exist.');
// Special case to solve Windows 2 WSL integration
if (!defined('WINDOWS2WSL')) {
is_readable($file)
or $this->raiseError('Image file does not exist.');
}
$info = list($this->width, $this->height, $this->fileType) = getimagesize($file);
if (empty($info)) {
@@ -1005,13 +1035,15 @@ class CImage
$this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}.");
// width as %
if ($this->newWidth[strlen($this->newWidth)-1] == '%') {
if ($this->newWidth
&& $this->newWidth[strlen($this->newWidth)-1] == '%') {
$this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100;
$this->log("Setting new width based on % to {$this->newWidth}");
}
// height as %
if ($this->newHeight[strlen($this->newHeight)-1] == '%') {
if ($this->newHeight
&& $this->newHeight[strlen($this->newHeight)-1] == '%') {
$this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100;
$this->log("Setting new height based on % to {$this->newHeight}");
}
@@ -1163,7 +1195,7 @@ class CImage
$this->newWidth = $width;
$this->newHeight = $height;
}
// Get image dimensions for pre-resize image.
if ($this->cropToFit || $this->fillToFit) {
@@ -1347,6 +1379,7 @@ class CImage
&& !$this->autoRotate
&& !$this->bgColor
&& ($this->upscale === self::UPSCALE_DEFAULT)
&& !$this->lossy
) {
$this->log("Using original image.");
$this->output($this->pathToImage);
@@ -1380,6 +1413,8 @@ class CImage
$compress = $this->compress ? "_co{$this->compress}" : null;
$rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null;
$rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null;
$lossy = $this->lossy ? "_l" : null;
$interlace = $this->interlace ? "_i" : null;
$saveAs = $this->normalizeFileExtension();
$saveAs = $saveAs ? "_$saveAs" : null;
@@ -1446,7 +1481,7 @@ class CImage
. $quality . $filters . $sharpen . $emboss . $blur . $palette
. $optimize . $compress
. $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor
. $convolve . $copyStrat . $saveAs;
. $convolve . $copyStrat . $lossy . $interlace . $saveAs;
return $this->setTarget($file, $base);
}
@@ -1766,7 +1801,7 @@ class CImage
// Resize by crop to fit
$this->log("Resizing using strategy - Crop to fit");
if (!$this->upscale
if (!$this->upscale
&& ($this->width < $this->newWidth || $this->height < $this->newHeight)) {
$this->log("Resizing - smaller image, do not upscale.");
@@ -2345,6 +2380,14 @@ class CImage
$this->jpegOptimizeCmd = null;
}
if (array_key_exists("png_lossy", $options)
&& $options['png_lossy'] !== false) {
$this->pngLossy = $options['png_lossy'];
$this->pngLossyCmd = $options['png_lossy_cmd'];
} else {
$this->pngLossyCmd = null;
}
if (isset($options['png_filter']) && $options['png_filter']) {
$this->pngFilterCmd = $options['png_filter_cmd'];
} else {
@@ -2401,8 +2444,10 @@ class CImage
return;
}
is_writable($this->saveFolder)
if (!defined("WINDOWS2WSL")) {
is_writable($this->saveFolder)
or $this->raiseError('Target directory is not writable.');
}
$type = $this->getTargetImageExtension();
$this->Log("Saving image as " . $type);
@@ -2410,6 +2455,12 @@ class CImage
case 'jpeg':
case 'jpg':
// Set as interlaced progressive JPEG
if ($this->interlace) {
$this->Log("Set JPEG image to be interlaced.");
$res = imageinterlace($this->image, true);
}
$this->Log("Saving image as JPEG to cache using quality = {$this->quality}.");
imagejpeg($this->image, $this->cacheFileName, $this->quality);
@@ -2446,6 +2497,24 @@ class CImage
imagesavealpha($this->image, true);
imagepng($this->image, $this->cacheFileName, $this->compress);
// Use external program to process lossy PNG, if defined
$lossyEnabled = $this->pngLossy === true;
$lossySoftEnabled = $this->pngLossy === null;
$lossyActiveEnabled = $this->lossy === true;
if ($lossyEnabled || ($lossySoftEnabled && $lossyActiveEnabled)) {
if ($this->verbose) {
clearstatcache();
$this->log("Lossy enabled: $lossyEnabled");
$this->log("Lossy soft enabled: $lossySoftEnabled");
$this->Log("Filesize before lossy optimize: " . filesize($this->cacheFileName) . " bytes.");
}
$res = array();
$cmd = $this->pngLossyCmd . " $this->cacheFileName $this->cacheFileName";
exec($cmd, $res);
$this->Log($cmd);
$this->Log($res);
}
// Use external program to filter PNG, if defined
if ($this->pngFilterCmd) {
if ($this->verbose) {
@@ -2730,18 +2799,18 @@ class CImage
$lastModified = filemtime($this->pathToImage);
$details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$details['cache'] = basename($this->cacheFileName);
$lastModified = filemtime($this->cacheFileName);
$details['cache'] = basename($this->cacheFileName ?? "");
$lastModified = filemtime($this->cacheFileName ?? "");
$details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$this->load($file);
$details['filename'] = basename($file);
$details['filename'] = basename($file ?? "");
$details['mimeType'] = $this->getMimeType($this->fileType);
$details['width'] = $this->width;
$details['height'] = $this->height;
$details['aspectRatio'] = round($this->width / $this->height, 3);
$details['size'] = filesize($file);
$details['size'] = filesize($file ?? "");
$details['colors'] = $this->colorsTotal($this->image);
$details['includedFiles'] = count(get_included_files());
$details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ;

View File

@@ -2,8 +2,10 @@ Image conversion on the fly using PHP
=====================================
[![Join the chat at https://gitter.im/mosbth/cimage](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mosbth/cimage?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!--
[![Build Status](https://travis-ci.org/mosbth/cimage.svg?branch=master)](https://travis-ci.org/mosbth/cimage)
[![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master)
-->
About
-------------------------------------
@@ -45,18 +47,18 @@ There are several ways of installing. You either install the whole project which
###Install source from GitHub
### Install source from GitHub
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.15 released 2016-08-08.**
**Latest stable version is v0.7.18 released 2016-08-09.**
I prefer cloning like this. Do switch to the latest stable version.
```bash
git clone git://github.com/mosbth/cimage.git
cd cimage
git checkout v0.7.15
git checkout v0.7.18
```
Make the cache-directory writable by the webserver.
@@ -66,7 +68,7 @@ chmod 777 cache
```
###Install all-included bundle
### Install all-included bundle
There are some all-included bundles of `img.php` that can be downloaded and used without dependency to the rest of the sourcecode.
@@ -79,14 +81,14 @@ There are some all-included bundles of `img.php` that can be downloaded and used
Dowload the version of your choice like this.
```bash
wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.15/webroot/imgp.php
wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.18/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.
###Install from Packagist
### Install from Packagist
You can install the package [`mos/cimage` from Packagist](https://packagist.org/packages/mos/cimage) using composer.
@@ -139,13 +141,13 @@ Get going quickly
###Check out the test page
### Check out the test page
Try it out by pointing your browser to the test file `webroot/test/test.php`. It will show some example images and you can review how they are created.
###Process your first image
### Process your first image
<img src="https://cimage.se/cimage/imgd.php?src=example/kodim04.png&amp;w=w2&amp;a=40,0,50,0" alt=''>
@@ -159,20 +161,20 @@ and try to resize it to a thumbnail by adding the options
###What does "processing the image" involves?
### What does "processing the image" involves?
Add `&verbose` to the link to get a verbose output of what is happens during image processing. This is useful for developers and those who seek a deeper understanding on how it works behind the scene.
###Check your system
### Check your system
Open up `webroot/check_system.php` if you are uncertain that your system has the right extensions loaded.
###How does it work?
### How does it work?
Review the settings in `webroot/img_config.php` and check out `webroot/img.php` on how it uses `CImage`.
@@ -193,7 +195,7 @@ Basic usage
###Select the source
### Select the source
Open an image through `img.php` by using its `src` attribute.
@@ -209,7 +211,7 @@ All images are stored in a directory structure and you access them as:
###Resize using constraints on width and height
### Resize using constraints on width and height
Create a thumbnail of the image by applying constraints on width and height, or one of them.
@@ -223,7 +225,7 @@ Think of the constraints as a imaginary box where the image should fit. With `wi
###Resize to fit a certain dimension
### Resize to fit a certain dimension
Creating a thumbnail with a certain dimension of width and height, usually involves stretching or cropping the image to fit in the selected dimensions. Here is how you create a image that has the exact dimensions of 300x150 pixels, by either *stretching*, *cropping* or *fill to fit*.
@@ -243,7 +245,7 @@ Fill to fit is useful when you have some image that must fit in a certain dimens
###List of parameters
### List of parameters
`img.php` supports a lot of parameters. Combine the parameters to get the desired behavior and resulting image. For example, take the original image, resize it using width, aspect-ratio and crop-to-fit, apply a sharpen effect, save the image as JPEG using quality 30.
@@ -255,7 +257,7 @@ Fill to fit is useful when you have some image that must fit in a certain dimens
Here is a list of all parameters that you can use together with `img.php`, grouped by its basic intent of usage.
####Mandatory options and debugging
#### Mandatory options and debugging
Option `src` is the only mandatory option. The options in this section is useful for debugging or deciding what version of the target image is used.
@@ -270,7 +272,7 @@ Option `src` is the only mandatory option. The options in this section is useful
####Options for deciding width and height of target image
#### Options for deciding width and height of target image
These options are all affecting the final dimensions, width and height, of the resulting image.
@@ -283,7 +285,7 @@ These options are all affecting the final dimensions, width and height, of the r
####Options for resize strategy
#### Options for resize strategy
These options affect strategy to use when resizing an image into a target image that has both width and height set.
@@ -296,7 +298,7 @@ These options affect strategy to use when resizing an image into a target image
####Options for cropping part of image
#### Options for cropping part of image
These options enable to decide what part of image to crop out.
@@ -307,7 +309,7 @@ These options enable to decide what part of image to crop out.
####General processing options
#### General processing options
These options are general options affecting processing.
@@ -317,7 +319,7 @@ These options are general options affecting processing.
####Processing of image before resizing
#### Processing of image before resizing
This option are executed *before* the image is resized.
@@ -329,7 +331,7 @@ This option are executed *before* the image is resized.
####Processing of image after resizing
#### Processing of image after resizing
These options are executed *after* the image is resized.
@@ -347,7 +349,7 @@ These options are executed *after* the image is resized.
####Saving image, affecting quality and file size
#### Saving image, affecting quality and file size
Options for saving the target image.
@@ -403,7 +405,7 @@ Consult the file `webroot/img-config.php` for a complete list together with the
###Create and name the config file
### Create and name the config file
The file `img.php` looks for the config-file `img_config.php`, and uses it if its found. The three files where everything is included -- `imgd.php`, `imgp.php` and `imgs.php` -- includes an empty `$config`-array which can be overridden by saving a config-file in the same directory. If the script is `imgp.php` then name the config-file `imgp_config.php` and it will find it and use those settings.
@@ -468,19 +470,19 @@ Here are some thoughts when applying `img.php` on a live system.
###Select the proper mode
### Select the proper mode
Select the proper mode for `img.php`. Set it to "strict" or "production" to prevent outsiders to get information about your system. Use only "development" for internal use since its quite verbose in its nature of error reporting.
###Put the installation directory outside web root
### Put the installation directory outside web root
Edit the config file to put the installation directory -- and the cache directory -- outside of the web root. Best practice would be to store the installation directory and cache, outside of the web root. The only thing needed in the web root is `img.php` and `img_config.php` (if used) which can be placed, for example, in `/img/img.php` or just as `/img.php`.
###Friendly urls through `.htaccess`
### Friendly urls through `.htaccess`
Use `.htaccess`and rewrite rules (Apache) to get friendly image urls. Put `img.php` in the `/img` directory. Put the file `.htaccess` in the web root.
@@ -518,7 +520,7 @@ The result is good readable urls to your images. Its easy for the search engine
###Monitor cache size
### Monitor cache size
There is a utility `cache.bash` included for monitoring the size of the cache-directory. It generates an output like this.
@@ -556,13 +558,13 @@ Use it as a base if you feel the need to monitor the size och the cache-director
###Read-only cache
### Read-only cache
The cache directory need to be writable for `img.php` to create new files. But its possible to first create all cache-files and then set the directory to be read-only. This will give you a way of shutting of `img.php` from creating new cache files. `img.php` will then continue to work for all images having a cached version but will fail if someone tries to create a new, not previously cached, version of the image.
###Post-processing with external tools
### Post-processing with external tools
You can use external tools to post-process the images to optimize the file size. This option is available for JPEG and for PNG images. Post-processing is disabled by default, edit `img_config.php` to enable it.
@@ -572,7 +574,7 @@ These tools for post processing is not a part of `CImage` and `img.php`, you nee
###Allowing remote download of images
### Allowing remote download of images
You can allow `img.php` to download remote images. That can be enabled in the config-file. However, before doing so, consider the implications on allowing anyone to download a file, hopefully an image, to your server and then the possibility to access it through the webserver.

View File

@@ -1,11 +1,125 @@
Revision history
=====================================
<!--
[![Build Status](https://travis-ci.org/mosbth/cimage.svg?branch=master)](https://travis-ci.org/mosbth/cimage)
[![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.8.6 (2023-10-27)
-------------------------------------
* Fix deprecation notice on "Creation of dynamic property" for PHP 8.2.
v0.7.15 (2016-08-08)
v0.8.5 (2022-11-17)
-------------------------------------
* Enable configuration fix for solving Windows 2 WSL2 issue with is_readable/is_writable #189.
* Update CHttpGet.php for php 8.1 deprecated notice #188.
* Remove build status from README (since it is not up to date).
v0.8.4 (2022-05-30)
-------------------------------------
* Support PHP 8.1 and remove (more) deprecated messages when run in in development mode.
v0.8.3 (2022-05-24)
-------------------------------------
* Support PHP 8.1 and remove deprecated messages when run in in development mode.
* Generate prebuilt all include files for various settings
* Fix deprecated for PHP 8.1
* Fix deprecated for PHP 8.1
* Add php version as output in verbose mode
* Add PHP 81 as test environment
v0.8.2 (2021-10-27)
-------------------------------------
* Remove bad configuration.
v0.8.1 (2020-06-08)
-------------------------------------
* Updated version number in define.php.
v0.8.0 (2020-06-08)
-------------------------------------
* Enable to set JPEG image as interlaced, implement feature #177.
* Add function getValue() to read from querystring.
* Set PHP 7.0 as precondition (to prepare to update the codebase).
v0.7.23 (2020-05-06)
-------------------------------------
* Fix error in composer.json
v0.7.22 (2020-05-06)
-------------------------------------
* Update composer.json and move ext-gd from required to suggested to ease installation where cli does not have all extensions installed.
v0.7.21 (2020-01-15)
-------------------------------------
* Support PHP 7.4, some minor fixes with notices.
v0.7.20 (2017-11-06)
-------------------------------------
* Remove webroot/img/{round8.PNG,wider.JPEG,wider.JPG} to avoid unzip warning message when installing with composer.
* Adding docker-compose.yml #169.
v0.7.19 (2017-03-31)
-------------------------------------
* Move exception handler from functions.php to img.php #166.
* Correct XSS injection in `check_system.php`.
* Composer suggests ext-imagick and ext-curl.
v0.7.18 (2016-08-09)
-------------------------------------
* Made `&lossless` a requirement to not use the original image.
v0.7.17 (2016-08-09)
-------------------------------------
* Made `&lossless` part of the generated cache filename.
v0.7.16 (2016-08-09)
-------------------------------------
* Fix default mode to be production.
* Added pngquant as extra postprocessing utility for PNG-images, #154.
* Bug `&status` wrong variable name for fast track cache.
v0.7.15 (2016-08-09)
-------------------------------------
* 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.
@@ -66,7 +180,7 @@ v0.7.8 (2015-12-06)
* HTTP error messages now 403, 404 and 500 as in #128 and #127.
* More examples on dealing with cache through bash `bin/cache.bash`, #129.
* Added conversion to sRGB using option `?srgb`. #120.
* Added conversion to sRGB using option `?srgb`. #120.
* Added Gitter badge to README, #126.
* Fix proper download url in README, #125.
* Change path in `webroot/htaccess` to make it work in current environment.
@@ -77,11 +191,11 @@ v0.7.7 (2015-10-21)
* One can now add a HTTP header for Cache-Control in the config file, #109.
* Added hook in img,php before CImage is called, #123.
* Added configuration for default jpeg quality and png compression in the config file, #107.
* Added configuration for default jpeg quality and png compression in the config file, #107.
* Strip comments and whitespace in imgs.php, #115.
* Bundle imgs.php did not have the correct mode.
* Adding option &status to get an overview of the installed on configured utilities, #116.
* Bug, all files saved as png-files, when not saving as specific file.
* Bug, all files saved as png-files, when not saving as specific file.
* Removed saving filename extension for alias images.
* Added option to decide if resample or resize when copying images internally. `&no-resample` makes resize, instead of resample as is default.
* Verbose now correctly states if transparent color is detected.
@@ -136,14 +250,14 @@ v0.7.1 (2015-07-25)
* Using `CWhitelist` for checking hotlinking to images, fix #88.
* Added mode for `test` which enables logging verbose mode to file, fix #97.
* Improved codestyle and added `phpcs.xml` to start using phpcs to check code style, fix #95.
* Adding `composer.json` for publishing on packagist.
* Adding `composer.json` for publishing on packagist.
* Add permalink to setup for comparing images with `webroot/compare/compare.php`, fix #92.
* Allow space in filename by using `urlencode()` and allow space as valid filenam character. fix #91.
* Support redirections for remote images, fix #87, fix #90.
* Support redirections for remote images, fix #87, fix #90.
* Improving usage of Travis and Scrutinizer.
* Naming cache-file using md5 for remote images, fix #86.
* Loading images without depending on filename extension, fix #85.
* Adding unittest with phpunit #84, fix #13
* Adding unittest with phpunit #84, fix #13
* Adding support for whitelist of remote hostnames, #84
* Adding phpdoc, fix #48.
* Adding travis, fix #15.

6
SECURITY.md Normal file
View File

@@ -0,0 +1,6 @@
Security policy
======================
To report security vulnerabilities in the project, send en email to mikael.t.h.roos@gmail.com.
For other security related issues, please open an issue on the project.

View File

@@ -1,5 +1,5 @@
{
"name": "mos/cimage",
"name": "mos/cimage",
"type": "library",
"description": "Process, scale, resize, crop and filter images.",
"keywords": ["image", "imageprocessing", "gd"],
@@ -18,11 +18,13 @@
"docs": "http://dbwebb.se/opensource/cimage"
},
"require": {
"php": ">=5.3",
"ext-gd": "*"
"php": ">=7.0"
},
"suggest": {
"ext-exif": "*"
"ext-curl": "*",
"ext-exif": "*",
"ext-gd": "*",
"ext-imagick": "*"
},
"autoload": {
"files": [

View File

@@ -1,6 +1,6 @@
<?php
// Version of cimage and img.php
define("CIMAGE_VERSION", "v0.7.15 (2016-08-09)");
define("CIMAGE_VERSION", "v0.8.6 (2023-10-27)");
// For CRemoteImage
define("CIMAGE_USER_AGENT", "CImage/" . CIMAGE_VERSION);

97
docker-compose.yaml Normal file
View File

@@ -0,0 +1,97 @@
version: "3"
services:
cli:
image: anax/dev
volumes: [ ".:/home/anax/repo" ]
apache:
image: anax/dev:apache
volumes: [ ".:/home/anax/repo" ]
ports: [ "11000:80" ]
remserver:
image: anax/dev:apache
ports:
- "8090:80"
volumes: [ ".:/home/anax/repo" ]
php82:
image: anax/dev:php82
volumes: [ ".:/home/anax/repo" ]
php82-apache:
image: anax/dev:php82-apache
ports: [ "11082:80" ]
volumes: [ ".:/home/anax/repo" ]
php81:
image: anax/dev:php81
volumes: [ ".:/home/anax/repo" ]
php81-apache:
image: anax/dev:php81-apache
ports: [ "11081:80" ]
volumes: [ ".:/home/anax/repo" ]
php80:
image: anax/dev:php80
volumes: [ ".:/home/anax/repo" ]
php80-apache:
image: anax/dev:php80-apache
ports: [ "11080:80" ]
volumes: [ ".:/home/anax/repo" ]
php74:
image: anax/dev:php74
volumes: [ ".:/home/anax/repo" ]
php74-apache:
image: anax/dev:php74-apache
ports: [ "11074:80" ]
volumes: [ ".:/home/anax/repo" ]
php73:
image: anax/dev:php73
volumes: [ ".:/home/anax/repo" ]
php73-apache:
image: anax/dev:php73-apache
ports: [ "11073:80" ]
volumes: [ ".:/home/anax/repo" ]
php72:
image: anax/dev:php72
volumes: [ ".:/home/anax/repo" ]
php72-apache:
image: anax/dev:php72-apache
ports: [ "11072:80" ]
volumes: [ ".:/home/anax/repo" ]
php71:
image: anax/dev:php71
volumes: [ ".:/home/anax/repo" ]
php71-apache:
image: anax/dev:php71-apache
ports: [ "11071:80" ]
volumes: [ ".:/home/anax/repo" ]
php70:
image: anax/dev:php70
volumes: [ ".:/home/anax/repo" ]
php70:
image: anax/dev:php70-apache
ports: [ "11070:80" ]
volumes: [ ".:/home/anax/repo" ]
php56:
image: anax/dev:php56
volumes: [ ".:/home/anax/repo" ]
php56:
image: anax/dev:php56-apache
ports: [ "11056:80" ]
volumes: [ ".:/home/anax/repo" ]

View File

@@ -67,22 +67,6 @@ function errorPage($msg, $type = 500)
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/**
* Get input from query string or return default value if not set.
*
@@ -123,6 +107,25 @@ function getDefined($key, $defined, $undefined)
/**
* Get value of input from query string or else $undefined.
*
* @param mixed $key as string or array of string values to look for in $_GET.
* @param mixed $undefined value to return when $key has no, or empty value in $_GET.
*
* @return mixed value as or $undefined.
*/
function getValue($key, $undefined)
{
$val = get($key);
if (is_null($val) || $val === "") {
return $undefined;
}
return $val;
}
/**
* Get value from config array or default if key is not set in config array.
*
@@ -148,7 +151,7 @@ function getConfig($key, $default)
*
* @return void or array.
*/
function verbose($msg = null)
function verbose($msg = null, $arg = "")
{
global $verbose, $verboseFile;
static $log = array();
@@ -161,5 +164,34 @@ function verbose($msg = null)
return $log;
}
$log[] = $msg;
if (is_null($arg)) {
$arg = "null";
} elseif ($arg === false) {
$arg = "false";
} elseif ($arg === true) {
$arg = "true";
}
$log[] = $msg . $arg;
}
/**
* Log when verbose mode, when used without argument it returns the result.
*
* @param string $msg to log.
*
* @return void or array.
*/
function checkExternalCommand($what, $enabled, $commandString)
{
$no = $enabled ? null : 'NOT';
$text = "Post processing $what is $no enabled.<br>";
list($command) = explode(" ", $commandString);
$no = is_executable($command) ? null : 'NOT';
$text .= "The command for $what is $no an executable.<br>";
return $text;
}

View File

@@ -2,7 +2,7 @@
echo 'Current PHP version: ' . phpversion() . '<br><br>';
echo 'Running on: ' . $_SERVER['SERVER_SOFTWARE'] . '<br><br>';
echo 'Running on: ' . htmlentities($_SERVER['SERVER_SOFTWARE']) . '<br><br>';
$no = extension_loaded('exif') ? null : 'NOT';
echo "Extension exif is $no loaded.<br>";
@@ -21,6 +21,9 @@ if (!$no) {
echo "<strong>Checking path for postprocessing tools</strong>";
echo "<br>pngquant: ";
system("which pngquant");
echo "<br>optipng: ";
system("which optipng");

View File

@@ -3,6 +3,12 @@
<head>
<style>
<?php
function e($str) {
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
?>
body {
}
@@ -128,15 +134,15 @@ if (isset($_GET['input1'])) {
// Use incoming from querystring as defaults
?>
CImage.compare({
"input1": "<?=$_GET['input1']?>",
"input2": "<?=$_GET['input2']?>",
"input3": "<?=$_GET['input3']?>",
"input4": "<?=$_GET['input4']?>",
"input5": "<?=$_GET['input5']?>",
"input6": "<?=$_GET['input6']?>",
"json": <?=$_GET['json']?>,
"stack": <?=$_GET['stack']?>,
"bg": <?=$_GET['bg']?>
"input1": "<?=e($_GET['input1'])?>",
"input2": "<?=e($_GET['input2'])?>",
"input3": "<?=e($_GET['input3'])?>",
"input4": "<?=e($_GET['input4'])?>",
"input5": "<?=e($_GET['input5'])?>",
"input6": "<?=e($_GET['input6'])?>",
"json": <?=e($_GET['json'])?>,
"stack": <?=e($_GET['stack'])?>,
"bg": <?=e($_GET['bg'])?>
});
<?php
} elseif (isset($script)) {

View File

@@ -8,6 +8,22 @@
*
*/
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/**
* Get configuration options from file, if the file exists, else use $config
* if its defined or create an empty $config.
@@ -170,7 +186,7 @@ $hotlinkingWhitelist = getConfig('hotlinking_whitelist', array());
$serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
$refererHost = parse_url($referer, PHP_URL_HOST);
$refererHost = parse_url($referer ?? "", PHP_URL_HOST);
if (!$allowHotlinking) {
if ($passwordMatch) {
@@ -269,7 +285,7 @@ $allowRemote = getConfig('remote_allow', false);
if ($allowRemote && $passwordMatch !== false) {
$cacheRemote = $cache->getPathToSubdir("remote");
$pattern = getConfig('remote_pattern', null);
$img->setRemoteDownload($allowRemote, $cacheRemote, $pattern);
@@ -303,11 +319,11 @@ if (isset($shortcut)
/**
* src - the source image file.
*/
$srcImage = urldecode(get('src'))
$srcImage = urldecode(get('src', ""))
or errorPage('Must set src-attribute.', 404);
// Get settings for src-alt as backup image
$srcAltImage = urldecode(get('src-alt', null));
$srcAltImage = urldecode(get('src-alt', ""));
$srcAltConfig = getConfig('src_alt', null);
if (empty($srcAltImage)) {
$srcAltImage = $srcAltConfig;
@@ -365,7 +381,7 @@ if ($dummyEnabled && $srcImage === $dummyFilename) {
matching file exists on the filesystem.',
404
);
}
}
}
if ($imagePathConstraint && !$dummyImage && !$remoteSource) {
@@ -424,7 +440,7 @@ if (isset($sizes[$newWidth])) {
}
// Support width as % of original width
if ($newWidth[strlen($newWidth)-1] == '%') {
if ($newWidth && $newWidth[strlen($newWidth)-1] == '%') {
is_numeric(substr($newWidth, 0, -1))
or errorPage('Width % not numeric.', 404);
} else {
@@ -449,7 +465,7 @@ if (isset($sizes[$newHeight])) {
}
// height
if ($newHeight[strlen($newHeight)-1] == '%') {
if ($newHeight && $newHeight[strlen($newHeight)-1] == '%') {
is_numeric(substr($newHeight, 0, -1))
or errorPage('Height % out of range.', 404);
} else {
@@ -480,7 +496,7 @@ $aspectRatioConstant = getConfig('aspect_ratio_constant', function () {
// Check to replace predefined aspect ratio
$aspectRatios = call_user_func($aspectRatioConstant);
$negateAspectRatio = ($aspectRatio[0] == '!') ? true : false;
$negateAspectRatio = ($aspectRatio && $aspectRatio[0] == '!') ? true : false;
$aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio;
if (isset($aspectRatios[$aspectRatio])) {
@@ -840,6 +856,9 @@ verbose("upscale = $upscale");
* Get details for post processing
*/
$postProcessing = getConfig('postprocessing', array(
'png_lossy' => false,
'png_lossy_cmd' => '/usr/local/bin/pngquant --force --output',
'png_filter' => false,
'png_filter_cmd' => '/usr/local/bin/optipng -q',
@@ -852,6 +871,15 @@ $postProcessing = getConfig('postprocessing', array(
/**
* lossy - Do lossy postprocessing, if available.
*/
$lossy = getDefined(array('lossy'), true, null);
verbose("lossy = $lossy");
/**
* alias - Save resulting image to another alias name.
* Password always apply, must be defined.
@@ -892,6 +920,18 @@ if ($cacheControl) {
/**
* interlace - Enable configuration for interlaced progressive JPEG images.
*/
$interlaceConfig = getConfig('interlace', null);
$interlaceValue = getValue('interlace', null);
$interlaceDefined = getDefined('interlace', true, null);
$interlace = $interlaceValue ?? $interlaceDefined ?? $interlaceConfig;
verbose("interlace (configfile) = ", $interlaceConfig);
verbose("interlace = ", $interlace);
/**
* Prepare a dummy image and use it as source image.
*/
@@ -970,7 +1010,7 @@ if ($status) {
$res = $cache->getStatusOfSubdir("srgb");
$text .= "Cache srgb $res\n";
$res = $cache->getStatusOfSubdir($fasttrackCache);
$res = $cache->getStatusOfSubdir($fastTrackCache);
$text .= "Cache fasttrack $res\n";
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
@@ -987,6 +1027,11 @@ if ($status) {
$no = extension_loaded('gd') ? null : 'NOT';
$text .= "Extension gd is $no loaded.<br>";
$text .= checkExternalCommand("PNG LOSSY", $postProcessing["png_lossy"], $postProcessing["png_lossy_cmd"]);
$text .= checkExternalCommand("PNG FILTER", $postProcessing["png_filter"], $postProcessing["png_filter_cmd"]);
$text .= checkExternalCommand("PNG DEFLATE", $postProcessing["png_deflate"], $postProcessing["png_deflate_cmd"]);
$text .= checkExternalCommand("JPEG OPTIMIZE", $postProcessing["jpeg_optimize"], $postProcessing["jpeg_optimize_cmd"]);
if (!$no) {
$text .= print_r(gd_info(), 1);
}
@@ -1048,6 +1093,7 @@ if (is_callable($hookBeforeCImage)) {
'blur' => $blur,
'convolve' => $convolve,
'rotateAfter' => $rotateAfter,
'interlace' => $interlace,
// Output format
'outputFormat' => $outputFormat,
@@ -1055,6 +1101,7 @@ if (is_callable($hookBeforeCImage)) {
// Other
'postProcessing' => $postProcessing,
'lossy' => $lossy,
));
verbose(print_r($allConfig, 1));
extract($allConfig);
@@ -1102,7 +1149,8 @@ EOD;
/**
* Load, process and output the image
*/
$img->log("Incoming arguments: " . print_r(verbose(), 1))
$img->log("PHP version: " . phpversion())
->log("Incoming arguments: " . print_r(verbose(), 1))
->setSaveFolder($cachePath)
->useCache($useCache)
->setSource($srcImage, $imagePath)
@@ -1135,10 +1183,14 @@ $img->log("Incoming arguments: " . print_r(verbose(), 1))
'blur' => $blur,
'convolve' => $convolve,
'rotateAfter' => $rotateAfter,
'interlace' => $interlace,
// Output format
'outputFormat' => $outputFormat,
'dpr' => $dpr,
// Postprocessing using external tools
'lossy' => $lossy,
)
)
->loadImageDetails()

BIN
webroot/img/duke.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

View File

@@ -21,6 +21,15 @@ if (!defined("CIMAGE_DEBUG")) {
define("CIMAGE_DEBUG_FILE", "/tmp/cimage");
}
/**
* Set this if you work with a webserver in Windows and try to access files
* within WSL2.
* The issue seems to be with functions like `is_writable()` and
* `is_readable()`.
* When WINDOWS2WSL is defined (to any value) it ignores these functions.
*/
#define('WINDOWS2WSL', 1);
return array(
@@ -67,7 +76,7 @@ return array(
*/
'image_path' => __DIR__ . '/img/',
'cache_path' => __DIR__ . '/../cache/',
//'alias_path' => __DIR__ . '/img/alias/',
'alias_path' => __DIR__ . '/img/alias/',
@@ -116,7 +125,7 @@ return array(
* password_type: 'text' // use plain password, not encoded,
*/
//'password_always' => false, // always require password,
//'password' => false, // "secret-password",
//'password' => "moped", // "secret-password",
//'password_type' => 'text', // supports 'text', 'md5', 'hash',
@@ -321,8 +330,16 @@ return array(
* Post processing of images using external tools, set to true or false
* and set command to be executed.
*
* The png_lossy can alos have a value of null which means that its
* enabled but not used as default. Each image having the option
* &lossy will be processed. This means one can individually choose
* when to use the lossy processing.
*
* Default values.
*
* png_lossy: false
* png_lossy_cmd: '/usr/local/bin/pngquant --force --output'
*
* png_filter: false
* png_filter_cmd: '/usr/local/bin/optipng -q'
*
@@ -334,6 +351,9 @@ return array(
*/
/*
'postprocessing' => array(
'png_lossy' => null,
'png_lossy_cmd' => '/usr/local/bin/pngquant --force --output',
'png_filter' => false,
'png_filter_cmd' => '/usr/local/bin/optipng -q',
@@ -474,6 +494,18 @@ return array(
"scale" => 14,
"luminanceStrategy" => 3,
"customCharacterSet" => null,
);
},*/
), */
/**
* Default options for creating interlaced progressive JPEG images. Set
* to true to always render jpeg images as interlaced. This setting can
* be overridden by using `?interlace`, `?interlace=true` or
* `?interlace=false`.
*
* Default values are:
* interlace: false
*/
/*'interlace' => false,*/
);

View File

@@ -38,7 +38,7 @@ $config = array(
// Version of cimage and img.php
define("CIMAGE_VERSION", "v0.7.15 (2016-08-09)");
define("CIMAGE_VERSION", "v0.8.6 (2023-10-27)");
// For CRemoteImage
define("CIMAGE_USER_AGENT", "CImage/" . CIMAGE_VERSION);
@@ -118,22 +118,6 @@ function errorPage($msg, $type = 500)
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/**
* Get input from query string or return default value if not set.
*
@@ -174,6 +158,25 @@ function getDefined($key, $defined, $undefined)
/**
* Get value of input from query string or else $undefined.
*
* @param mixed $key as string or array of string values to look for in $_GET.
* @param mixed $undefined value to return when $key has no, or empty value in $_GET.
*
* @return mixed value as or $undefined.
*/
function getValue($key, $undefined)
{
$val = get($key);
if (is_null($val) || $val === "") {
return $undefined;
}
return $val;
}
/**
* Get value from config array or default if key is not set in config array.
*
@@ -199,7 +202,7 @@ function getConfig($key, $default)
*
* @return void or array.
*/
function verbose($msg = null)
function verbose($msg = null, $arg = "")
{
global $verbose, $verboseFile;
static $log = array();
@@ -212,7 +215,36 @@ function verbose($msg = null)
return $log;
}
$log[] = $msg;
if (is_null($arg)) {
$arg = "null";
} elseif ($arg === false) {
$arg = "false";
} elseif ($arg === true) {
$arg = "true";
}
$log[] = $msg . $arg;
}
/**
* Log when verbose mode, when used without argument it returns the result.
*
* @param string $msg to log.
*
* @return void or array.
*/
function checkExternalCommand($what, $enabled, $commandString)
{
$no = $enabled ? null : 'NOT';
$text = "Post processing $what is $no enabled.<br>";
list($command) = explode(" ", $commandString);
$no = is_executable($command) ? null : 'NOT';
$text .= "The command for $what is $no an executable.<br>";
return $text;
}
@@ -433,7 +465,7 @@ class CHttpGet
{
$type = isset($this->response['header']['Content-Type'])
? $this->response['header']['Content-Type']
: null;
: '';
return preg_match('#[a-z]+/[a-z]+#', $type)
? $type
@@ -1111,6 +1143,7 @@ class CAsciiArt
* @example http://dbwebb.se/opensource/cimage
* @link https://github.com/mosbth/cimage
*/
#[AllowDynamicProperties]
class CImage
{
@@ -1260,6 +1293,13 @@ class CImage
/**
* Do lossy output using external postprocessing tools.
*/
private $lossy = null;
/**
* Verbose mode to print out a trace and display the created image
*/
@@ -1295,7 +1335,15 @@ class CImage
/**
* Path to command for filter optimize, for example optipng or null.
* Path to command for lossy optimize, for example pngquant.
*/
private $pngLossy;
private $pngLossyCmd;
/**
* Path to command for filter optimize, for example optipng.
*/
private $pngFilter;
private $pngFilterCmd;
@@ -1303,7 +1351,7 @@ class CImage
/**
* Path to command for deflate optimize, for example pngout or null.
* Path to command for deflate optimize, for example pngout.
*/
private $pngDeflate;
private $pngDeflateCmd;
@@ -1513,6 +1561,13 @@ class CImage
/*
* Use interlaced progressive mode for JPEG images.
*/
private $interlace = false;
/*
* Image copy strategy, defaults to RESAMPLE.
*/
@@ -1762,9 +1817,9 @@ class CImage
*
* @return string $extension as a normalized file extension.
*/
private function normalizeFileExtension($extension = null)
private function normalizeFileExtension($extension = "")
{
$extension = strtolower($extension ? $extension : $this->extension);
$extension = strtolower($extension ? $extension : $this->extension ?? "");
if ($extension == 'jpeg') {
$extension = 'jpg';
@@ -1928,10 +1983,14 @@ class CImage
'blur' => null,
'convolve' => null,
'rotateAfter' => null,
'interlace' => null,
// Output format
'outputFormat' => null,
'dpr' => 1,
// Postprocessing using external tools
'lossy' => null,
);
// Convert crop settings from string to array
@@ -2043,8 +2102,11 @@ class CImage
{
$file = $file ? $file : $this->pathToImage;
is_readable($file)
or $this->raiseError('Image file does not exist.');
// Special case to solve Windows 2 WSL integration
if (!defined('WINDOWS2WSL')) {
is_readable($file)
or $this->raiseError('Image file does not exist.');
}
$info = list($this->width, $this->height, $this->fileType) = getimagesize($file);
if (empty($info)) {
@@ -2110,13 +2172,15 @@ class CImage
$this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}.");
// width as %
if ($this->newWidth[strlen($this->newWidth)-1] == '%') {
if ($this->newWidth
&& $this->newWidth[strlen($this->newWidth)-1] == '%') {
$this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100;
$this->log("Setting new width based on % to {$this->newWidth}");
}
// height as %
if ($this->newHeight[strlen($this->newHeight)-1] == '%') {
if ($this->newHeight
&& $this->newHeight[strlen($this->newHeight)-1] == '%') {
$this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100;
$this->log("Setting new height based on % to {$this->newHeight}");
}
@@ -2268,7 +2332,7 @@ class CImage
$this->newWidth = $width;
$this->newHeight = $height;
}
// Get image dimensions for pre-resize image.
if ($this->cropToFit || $this->fillToFit) {
@@ -2452,6 +2516,7 @@ class CImage
&& !$this->autoRotate
&& !$this->bgColor
&& ($this->upscale === self::UPSCALE_DEFAULT)
&& !$this->lossy
) {
$this->log("Using original image.");
$this->output($this->pathToImage);
@@ -2485,6 +2550,8 @@ class CImage
$compress = $this->compress ? "_co{$this->compress}" : null;
$rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null;
$rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null;
$lossy = $this->lossy ? "_l" : null;
$interlace = $this->interlace ? "_i" : null;
$saveAs = $this->normalizeFileExtension();
$saveAs = $saveAs ? "_$saveAs" : null;
@@ -2551,7 +2618,7 @@ class CImage
. $quality . $filters . $sharpen . $emboss . $blur . $palette
. $optimize . $compress
. $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor
. $convolve . $copyStrat . $saveAs;
. $convolve . $copyStrat . $lossy . $interlace . $saveAs;
return $this->setTarget($file, $base);
}
@@ -2871,7 +2938,7 @@ class CImage
// Resize by crop to fit
$this->log("Resizing using strategy - Crop to fit");
if (!$this->upscale
if (!$this->upscale
&& ($this->width < $this->newWidth || $this->height < $this->newHeight)) {
$this->log("Resizing - smaller image, do not upscale.");
@@ -3450,6 +3517,14 @@ class CImage
$this->jpegOptimizeCmd = null;
}
if (array_key_exists("png_lossy", $options)
&& $options['png_lossy'] !== false) {
$this->pngLossy = $options['png_lossy'];
$this->pngLossyCmd = $options['png_lossy_cmd'];
} else {
$this->pngLossyCmd = null;
}
if (isset($options['png_filter']) && $options['png_filter']) {
$this->pngFilterCmd = $options['png_filter_cmd'];
} else {
@@ -3506,8 +3581,10 @@ class CImage
return;
}
is_writable($this->saveFolder)
if (!defined("WINDOWS2WSL")) {
is_writable($this->saveFolder)
or $this->raiseError('Target directory is not writable.');
}
$type = $this->getTargetImageExtension();
$this->Log("Saving image as " . $type);
@@ -3515,6 +3592,12 @@ class CImage
case 'jpeg':
case 'jpg':
// Set as interlaced progressive JPEG
if ($this->interlace) {
$this->Log("Set JPEG image to be interlaced.");
$res = imageinterlace($this->image, true);
}
$this->Log("Saving image as JPEG to cache using quality = {$this->quality}.");
imagejpeg($this->image, $this->cacheFileName, $this->quality);
@@ -3551,6 +3634,24 @@ class CImage
imagesavealpha($this->image, true);
imagepng($this->image, $this->cacheFileName, $this->compress);
// Use external program to process lossy PNG, if defined
$lossyEnabled = $this->pngLossy === true;
$lossySoftEnabled = $this->pngLossy === null;
$lossyActiveEnabled = $this->lossy === true;
if ($lossyEnabled || ($lossySoftEnabled && $lossyActiveEnabled)) {
if ($this->verbose) {
clearstatcache();
$this->log("Lossy enabled: $lossyEnabled");
$this->log("Lossy soft enabled: $lossySoftEnabled");
$this->Log("Filesize before lossy optimize: " . filesize($this->cacheFileName) . " bytes.");
}
$res = array();
$cmd = $this->pngLossyCmd . " $this->cacheFileName $this->cacheFileName";
exec($cmd, $res);
$this->Log($cmd);
$this->Log($res);
}
// Use external program to filter PNG, if defined
if ($this->pngFilterCmd) {
if ($this->verbose) {
@@ -3835,18 +3936,18 @@ class CImage
$lastModified = filemtime($this->pathToImage);
$details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$details['cache'] = basename($this->cacheFileName);
$lastModified = filemtime($this->cacheFileName);
$details['cache'] = basename($this->cacheFileName ?? "");
$lastModified = filemtime($this->cacheFileName ?? "");
$details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$this->load($file);
$details['filename'] = basename($file);
$details['filename'] = basename($file ?? "");
$details['mimeType'] = $this->getMimeType($this->fileType);
$details['width'] = $this->width;
$details['height'] = $this->height;
$details['aspectRatio'] = round($this->width / $this->height, 3);
$details['size'] = filesize($file);
$details['size'] = filesize($file ?? "");
$details['colors'] = $this->colorsTotal($this->image);
$details['includedFiles'] = count(get_included_files());
$details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ;
@@ -4045,6 +4146,15 @@ class CCache
return $path;
}
if ($create && defined('WINDOWS2WSL')) {
// Special case to solve Windows 2 WSL integration
$path = $this->path . "/" . $subdir;
if (mkdir($path)) {
return realpath($path);
}
}
if ($create && is_writable($this->path)) {
$path = $this->path . "/" . $subdir;
@@ -4351,6 +4461,22 @@ class CFastTrackCache
*
*/
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/**
* Get configuration options from file, if the file exists, else use $config
* if its defined or create an empty $config.
@@ -4513,7 +4639,7 @@ $hotlinkingWhitelist = getConfig('hotlinking_whitelist', array());
$serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
$refererHost = parse_url($referer, PHP_URL_HOST);
$refererHost = parse_url($referer ?? "", PHP_URL_HOST);
if (!$allowHotlinking) {
if ($passwordMatch) {
@@ -4612,7 +4738,7 @@ $allowRemote = getConfig('remote_allow', false);
if ($allowRemote && $passwordMatch !== false) {
$cacheRemote = $cache->getPathToSubdir("remote");
$pattern = getConfig('remote_pattern', null);
$img->setRemoteDownload($allowRemote, $cacheRemote, $pattern);
@@ -4646,11 +4772,11 @@ if (isset($shortcut)
/**
* src - the source image file.
*/
$srcImage = urldecode(get('src'))
$srcImage = urldecode(get('src', ""))
or errorPage('Must set src-attribute.', 404);
// Get settings for src-alt as backup image
$srcAltImage = urldecode(get('src-alt', null));
$srcAltImage = urldecode(get('src-alt', ""));
$srcAltConfig = getConfig('src_alt', null);
if (empty($srcAltImage)) {
$srcAltImage = $srcAltConfig;
@@ -4708,7 +4834,7 @@ if ($dummyEnabled && $srcImage === $dummyFilename) {
matching file exists on the filesystem.',
404
);
}
}
}
if ($imagePathConstraint && !$dummyImage && !$remoteSource) {
@@ -4767,7 +4893,7 @@ if (isset($sizes[$newWidth])) {
}
// Support width as % of original width
if ($newWidth[strlen($newWidth)-1] == '%') {
if ($newWidth && $newWidth[strlen($newWidth)-1] == '%') {
is_numeric(substr($newWidth, 0, -1))
or errorPage('Width % not numeric.', 404);
} else {
@@ -4792,7 +4918,7 @@ if (isset($sizes[$newHeight])) {
}
// height
if ($newHeight[strlen($newHeight)-1] == '%') {
if ($newHeight && $newHeight[strlen($newHeight)-1] == '%') {
is_numeric(substr($newHeight, 0, -1))
or errorPage('Height % out of range.', 404);
} else {
@@ -4823,7 +4949,7 @@ $aspectRatioConstant = getConfig('aspect_ratio_constant', function () {
// Check to replace predefined aspect ratio
$aspectRatios = call_user_func($aspectRatioConstant);
$negateAspectRatio = ($aspectRatio[0] == '!') ? true : false;
$negateAspectRatio = ($aspectRatio && $aspectRatio[0] == '!') ? true : false;
$aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio;
if (isset($aspectRatios[$aspectRatio])) {
@@ -5183,6 +5309,9 @@ verbose("upscale = $upscale");
* Get details for post processing
*/
$postProcessing = getConfig('postprocessing', array(
'png_lossy' => false,
'png_lossy_cmd' => '/usr/local/bin/pngquant --force --output',
'png_filter' => false,
'png_filter_cmd' => '/usr/local/bin/optipng -q',
@@ -5195,6 +5324,15 @@ $postProcessing = getConfig('postprocessing', array(
/**
* lossy - Do lossy postprocessing, if available.
*/
$lossy = getDefined(array('lossy'), true, null);
verbose("lossy = $lossy");
/**
* alias - Save resulting image to another alias name.
* Password always apply, must be defined.
@@ -5235,6 +5373,18 @@ if ($cacheControl) {
/**
* interlace - Enable configuration for interlaced progressive JPEG images.
*/
$interlaceConfig = getConfig('interlace', null);
$interlaceValue = getValue('interlace', null);
$interlaceDefined = getDefined('interlace', true, null);
$interlace = $interlaceValue ?? $interlaceDefined ?? $interlaceConfig;
verbose("interlace (configfile) = ", $interlaceConfig);
verbose("interlace = ", $interlace);
/**
* Prepare a dummy image and use it as source image.
*/
@@ -5313,7 +5463,7 @@ if ($status) {
$res = $cache->getStatusOfSubdir("srgb");
$text .= "Cache srgb $res\n";
$res = $cache->getStatusOfSubdir($fasttrackCache);
$res = $cache->getStatusOfSubdir($fastTrackCache);
$text .= "Cache fasttrack $res\n";
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
@@ -5330,6 +5480,11 @@ if ($status) {
$no = extension_loaded('gd') ? null : 'NOT';
$text .= "Extension gd is $no loaded.<br>";
$text .= checkExternalCommand("PNG LOSSY", $postProcessing["png_lossy"], $postProcessing["png_lossy_cmd"]);
$text .= checkExternalCommand("PNG FILTER", $postProcessing["png_filter"], $postProcessing["png_filter_cmd"]);
$text .= checkExternalCommand("PNG DEFLATE", $postProcessing["png_deflate"], $postProcessing["png_deflate_cmd"]);
$text .= checkExternalCommand("JPEG OPTIMIZE", $postProcessing["jpeg_optimize"], $postProcessing["jpeg_optimize_cmd"]);
if (!$no) {
$text .= print_r(gd_info(), 1);
}
@@ -5391,6 +5546,7 @@ if (is_callable($hookBeforeCImage)) {
'blur' => $blur,
'convolve' => $convolve,
'rotateAfter' => $rotateAfter,
'interlace' => $interlace,
// Output format
'outputFormat' => $outputFormat,
@@ -5398,6 +5554,7 @@ if (is_callable($hookBeforeCImage)) {
// Other
'postProcessing' => $postProcessing,
'lossy' => $lossy,
));
verbose(print_r($allConfig, 1));
extract($allConfig);
@@ -5445,7 +5602,8 @@ EOD;
/**
* Load, process and output the image
*/
$img->log("Incoming arguments: " . print_r(verbose(), 1))
$img->log("PHP version: " . phpversion())
->log("Incoming arguments: " . print_r(verbose(), 1))
->setSaveFolder($cachePath)
->useCache($useCache)
->setSource($srcImage, $imagePath)
@@ -5478,10 +5636,14 @@ $img->log("Incoming arguments: " . print_r(verbose(), 1))
'blur' => $blur,
'convolve' => $convolve,
'rotateAfter' => $rotateAfter,
'interlace' => $interlace,
// Output format
'outputFormat' => $outputFormat,
'dpr' => $dpr,
// Postprocessing using external tools
'lossy' => $lossy,
)
)
->loadImageDetails()

View File

@@ -38,7 +38,7 @@ $config = array(
// Version of cimage and img.php
define("CIMAGE_VERSION", "v0.7.15 (2016-08-09)");
define("CIMAGE_VERSION", "v0.8.6 (2023-10-27)");
// For CRemoteImage
define("CIMAGE_USER_AGENT", "CImage/" . CIMAGE_VERSION);
@@ -118,22 +118,6 @@ function errorPage($msg, $type = 500)
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/**
* Get input from query string or return default value if not set.
*
@@ -174,6 +158,25 @@ function getDefined($key, $defined, $undefined)
/**
* Get value of input from query string or else $undefined.
*
* @param mixed $key as string or array of string values to look for in $_GET.
* @param mixed $undefined value to return when $key has no, or empty value in $_GET.
*
* @return mixed value as or $undefined.
*/
function getValue($key, $undefined)
{
$val = get($key);
if (is_null($val) || $val === "") {
return $undefined;
}
return $val;
}
/**
* Get value from config array or default if key is not set in config array.
*
@@ -199,7 +202,7 @@ function getConfig($key, $default)
*
* @return void or array.
*/
function verbose($msg = null)
function verbose($msg = null, $arg = "")
{
global $verbose, $verboseFile;
static $log = array();
@@ -212,7 +215,36 @@ function verbose($msg = null)
return $log;
}
$log[] = $msg;
if (is_null($arg)) {
$arg = "null";
} elseif ($arg === false) {
$arg = "false";
} elseif ($arg === true) {
$arg = "true";
}
$log[] = $msg . $arg;
}
/**
* Log when verbose mode, when used without argument it returns the result.
*
* @param string $msg to log.
*
* @return void or array.
*/
function checkExternalCommand($what, $enabled, $commandString)
{
$no = $enabled ? null : 'NOT';
$text = "Post processing $what is $no enabled.<br>";
list($command) = explode(" ", $commandString);
$no = is_executable($command) ? null : 'NOT';
$text .= "The command for $what is $no an executable.<br>";
return $text;
}
@@ -433,7 +465,7 @@ class CHttpGet
{
$type = isset($this->response['header']['Content-Type'])
? $this->response['header']['Content-Type']
: null;
: '';
return preg_match('#[a-z]+/[a-z]+#', $type)
? $type
@@ -1111,6 +1143,7 @@ class CAsciiArt
* @example http://dbwebb.se/opensource/cimage
* @link https://github.com/mosbth/cimage
*/
#[AllowDynamicProperties]
class CImage
{
@@ -1260,6 +1293,13 @@ class CImage
/**
* Do lossy output using external postprocessing tools.
*/
private $lossy = null;
/**
* Verbose mode to print out a trace and display the created image
*/
@@ -1295,7 +1335,15 @@ class CImage
/**
* Path to command for filter optimize, for example optipng or null.
* Path to command for lossy optimize, for example pngquant.
*/
private $pngLossy;
private $pngLossyCmd;
/**
* Path to command for filter optimize, for example optipng.
*/
private $pngFilter;
private $pngFilterCmd;
@@ -1303,7 +1351,7 @@ class CImage
/**
* Path to command for deflate optimize, for example pngout or null.
* Path to command for deflate optimize, for example pngout.
*/
private $pngDeflate;
private $pngDeflateCmd;
@@ -1513,6 +1561,13 @@ class CImage
/*
* Use interlaced progressive mode for JPEG images.
*/
private $interlace = false;
/*
* Image copy strategy, defaults to RESAMPLE.
*/
@@ -1762,9 +1817,9 @@ class CImage
*
* @return string $extension as a normalized file extension.
*/
private function normalizeFileExtension($extension = null)
private function normalizeFileExtension($extension = "")
{
$extension = strtolower($extension ? $extension : $this->extension);
$extension = strtolower($extension ? $extension : $this->extension ?? "");
if ($extension == 'jpeg') {
$extension = 'jpg';
@@ -1928,10 +1983,14 @@ class CImage
'blur' => null,
'convolve' => null,
'rotateAfter' => null,
'interlace' => null,
// Output format
'outputFormat' => null,
'dpr' => 1,
// Postprocessing using external tools
'lossy' => null,
);
// Convert crop settings from string to array
@@ -2043,8 +2102,11 @@ class CImage
{
$file = $file ? $file : $this->pathToImage;
is_readable($file)
or $this->raiseError('Image file does not exist.');
// Special case to solve Windows 2 WSL integration
if (!defined('WINDOWS2WSL')) {
is_readable($file)
or $this->raiseError('Image file does not exist.');
}
$info = list($this->width, $this->height, $this->fileType) = getimagesize($file);
if (empty($info)) {
@@ -2110,13 +2172,15 @@ class CImage
$this->log("Init dimension (before) newWidth x newHeight is {$this->newWidth} x {$this->newHeight}.");
// width as %
if ($this->newWidth[strlen($this->newWidth)-1] == '%') {
if ($this->newWidth
&& $this->newWidth[strlen($this->newWidth)-1] == '%') {
$this->newWidth = $this->width * substr($this->newWidth, 0, -1) / 100;
$this->log("Setting new width based on % to {$this->newWidth}");
}
// height as %
if ($this->newHeight[strlen($this->newHeight)-1] == '%') {
if ($this->newHeight
&& $this->newHeight[strlen($this->newHeight)-1] == '%') {
$this->newHeight = $this->height * substr($this->newHeight, 0, -1) / 100;
$this->log("Setting new height based on % to {$this->newHeight}");
}
@@ -2268,7 +2332,7 @@ class CImage
$this->newWidth = $width;
$this->newHeight = $height;
}
// Get image dimensions for pre-resize image.
if ($this->cropToFit || $this->fillToFit) {
@@ -2452,6 +2516,7 @@ class CImage
&& !$this->autoRotate
&& !$this->bgColor
&& ($this->upscale === self::UPSCALE_DEFAULT)
&& !$this->lossy
) {
$this->log("Using original image.");
$this->output($this->pathToImage);
@@ -2485,6 +2550,8 @@ class CImage
$compress = $this->compress ? "_co{$this->compress}" : null;
$rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null;
$rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null;
$lossy = $this->lossy ? "_l" : null;
$interlace = $this->interlace ? "_i" : null;
$saveAs = $this->normalizeFileExtension();
$saveAs = $saveAs ? "_$saveAs" : null;
@@ -2551,7 +2618,7 @@ class CImage
. $quality . $filters . $sharpen . $emboss . $blur . $palette
. $optimize . $compress
. $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor
. $convolve . $copyStrat . $saveAs;
. $convolve . $copyStrat . $lossy . $interlace . $saveAs;
return $this->setTarget($file, $base);
}
@@ -2871,7 +2938,7 @@ class CImage
// Resize by crop to fit
$this->log("Resizing using strategy - Crop to fit");
if (!$this->upscale
if (!$this->upscale
&& ($this->width < $this->newWidth || $this->height < $this->newHeight)) {
$this->log("Resizing - smaller image, do not upscale.");
@@ -3450,6 +3517,14 @@ class CImage
$this->jpegOptimizeCmd = null;
}
if (array_key_exists("png_lossy", $options)
&& $options['png_lossy'] !== false) {
$this->pngLossy = $options['png_lossy'];
$this->pngLossyCmd = $options['png_lossy_cmd'];
} else {
$this->pngLossyCmd = null;
}
if (isset($options['png_filter']) && $options['png_filter']) {
$this->pngFilterCmd = $options['png_filter_cmd'];
} else {
@@ -3506,8 +3581,10 @@ class CImage
return;
}
is_writable($this->saveFolder)
if (!defined("WINDOWS2WSL")) {
is_writable($this->saveFolder)
or $this->raiseError('Target directory is not writable.');
}
$type = $this->getTargetImageExtension();
$this->Log("Saving image as " . $type);
@@ -3515,6 +3592,12 @@ class CImage
case 'jpeg':
case 'jpg':
// Set as interlaced progressive JPEG
if ($this->interlace) {
$this->Log("Set JPEG image to be interlaced.");
$res = imageinterlace($this->image, true);
}
$this->Log("Saving image as JPEG to cache using quality = {$this->quality}.");
imagejpeg($this->image, $this->cacheFileName, $this->quality);
@@ -3551,6 +3634,24 @@ class CImage
imagesavealpha($this->image, true);
imagepng($this->image, $this->cacheFileName, $this->compress);
// Use external program to process lossy PNG, if defined
$lossyEnabled = $this->pngLossy === true;
$lossySoftEnabled = $this->pngLossy === null;
$lossyActiveEnabled = $this->lossy === true;
if ($lossyEnabled || ($lossySoftEnabled && $lossyActiveEnabled)) {
if ($this->verbose) {
clearstatcache();
$this->log("Lossy enabled: $lossyEnabled");
$this->log("Lossy soft enabled: $lossySoftEnabled");
$this->Log("Filesize before lossy optimize: " . filesize($this->cacheFileName) . " bytes.");
}
$res = array();
$cmd = $this->pngLossyCmd . " $this->cacheFileName $this->cacheFileName";
exec($cmd, $res);
$this->Log($cmd);
$this->Log($res);
}
// Use external program to filter PNG, if defined
if ($this->pngFilterCmd) {
if ($this->verbose) {
@@ -3835,18 +3936,18 @@ class CImage
$lastModified = filemtime($this->pathToImage);
$details['srcGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$details['cache'] = basename($this->cacheFileName);
$lastModified = filemtime($this->cacheFileName);
$details['cache'] = basename($this->cacheFileName ?? "");
$lastModified = filemtime($this->cacheFileName ?? "");
$details['cacheGmdate'] = gmdate("D, d M Y H:i:s", $lastModified);
$this->load($file);
$details['filename'] = basename($file);
$details['filename'] = basename($file ?? "");
$details['mimeType'] = $this->getMimeType($this->fileType);
$details['width'] = $this->width;
$details['height'] = $this->height;
$details['aspectRatio'] = round($this->width / $this->height, 3);
$details['size'] = filesize($file);
$details['size'] = filesize($file ?? "");
$details['colors'] = $this->colorsTotal($this->image);
$details['includedFiles'] = count(get_included_files());
$details['memoryPeek'] = round(memory_get_peak_usage()/1024/1024, 3) . " MB" ;
@@ -4045,6 +4146,15 @@ class CCache
return $path;
}
if ($create && defined('WINDOWS2WSL')) {
// Special case to solve Windows 2 WSL integration
$path = $this->path . "/" . $subdir;
if (mkdir($path)) {
return realpath($path);
}
}
if ($create && is_writable($this->path)) {
$path = $this->path . "/" . $subdir;
@@ -4351,6 +4461,22 @@ class CFastTrackCache
*
*/
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/**
* Get configuration options from file, if the file exists, else use $config
* if its defined or create an empty $config.
@@ -4513,7 +4639,7 @@ $hotlinkingWhitelist = getConfig('hotlinking_whitelist', array());
$serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
$refererHost = parse_url($referer, PHP_URL_HOST);
$refererHost = parse_url($referer ?? "", PHP_URL_HOST);
if (!$allowHotlinking) {
if ($passwordMatch) {
@@ -4612,7 +4738,7 @@ $allowRemote = getConfig('remote_allow', false);
if ($allowRemote && $passwordMatch !== false) {
$cacheRemote = $cache->getPathToSubdir("remote");
$pattern = getConfig('remote_pattern', null);
$img->setRemoteDownload($allowRemote, $cacheRemote, $pattern);
@@ -4646,11 +4772,11 @@ if (isset($shortcut)
/**
* src - the source image file.
*/
$srcImage = urldecode(get('src'))
$srcImage = urldecode(get('src', ""))
or errorPage('Must set src-attribute.', 404);
// Get settings for src-alt as backup image
$srcAltImage = urldecode(get('src-alt', null));
$srcAltImage = urldecode(get('src-alt', ""));
$srcAltConfig = getConfig('src_alt', null);
if (empty($srcAltImage)) {
$srcAltImage = $srcAltConfig;
@@ -4708,7 +4834,7 @@ if ($dummyEnabled && $srcImage === $dummyFilename) {
matching file exists on the filesystem.',
404
);
}
}
}
if ($imagePathConstraint && !$dummyImage && !$remoteSource) {
@@ -4767,7 +4893,7 @@ if (isset($sizes[$newWidth])) {
}
// Support width as % of original width
if ($newWidth[strlen($newWidth)-1] == '%') {
if ($newWidth && $newWidth[strlen($newWidth)-1] == '%') {
is_numeric(substr($newWidth, 0, -1))
or errorPage('Width % not numeric.', 404);
} else {
@@ -4792,7 +4918,7 @@ if (isset($sizes[$newHeight])) {
}
// height
if ($newHeight[strlen($newHeight)-1] == '%') {
if ($newHeight && $newHeight[strlen($newHeight)-1] == '%') {
is_numeric(substr($newHeight, 0, -1))
or errorPage('Height % out of range.', 404);
} else {
@@ -4823,7 +4949,7 @@ $aspectRatioConstant = getConfig('aspect_ratio_constant', function () {
// Check to replace predefined aspect ratio
$aspectRatios = call_user_func($aspectRatioConstant);
$negateAspectRatio = ($aspectRatio[0] == '!') ? true : false;
$negateAspectRatio = ($aspectRatio && $aspectRatio[0] == '!') ? true : false;
$aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio;
if (isset($aspectRatios[$aspectRatio])) {
@@ -5183,6 +5309,9 @@ verbose("upscale = $upscale");
* Get details for post processing
*/
$postProcessing = getConfig('postprocessing', array(
'png_lossy' => false,
'png_lossy_cmd' => '/usr/local/bin/pngquant --force --output',
'png_filter' => false,
'png_filter_cmd' => '/usr/local/bin/optipng -q',
@@ -5195,6 +5324,15 @@ $postProcessing = getConfig('postprocessing', array(
/**
* lossy - Do lossy postprocessing, if available.
*/
$lossy = getDefined(array('lossy'), true, null);
verbose("lossy = $lossy");
/**
* alias - Save resulting image to another alias name.
* Password always apply, must be defined.
@@ -5235,6 +5373,18 @@ if ($cacheControl) {
/**
* interlace - Enable configuration for interlaced progressive JPEG images.
*/
$interlaceConfig = getConfig('interlace', null);
$interlaceValue = getValue('interlace', null);
$interlaceDefined = getDefined('interlace', true, null);
$interlace = $interlaceValue ?? $interlaceDefined ?? $interlaceConfig;
verbose("interlace (configfile) = ", $interlaceConfig);
verbose("interlace = ", $interlace);
/**
* Prepare a dummy image and use it as source image.
*/
@@ -5313,7 +5463,7 @@ if ($status) {
$res = $cache->getStatusOfSubdir("srgb");
$text .= "Cache srgb $res\n";
$res = $cache->getStatusOfSubdir($fasttrackCache);
$res = $cache->getStatusOfSubdir($fastTrackCache);
$text .= "Cache fasttrack $res\n";
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
@@ -5330,6 +5480,11 @@ if ($status) {
$no = extension_loaded('gd') ? null : 'NOT';
$text .= "Extension gd is $no loaded.<br>";
$text .= checkExternalCommand("PNG LOSSY", $postProcessing["png_lossy"], $postProcessing["png_lossy_cmd"]);
$text .= checkExternalCommand("PNG FILTER", $postProcessing["png_filter"], $postProcessing["png_filter_cmd"]);
$text .= checkExternalCommand("PNG DEFLATE", $postProcessing["png_deflate"], $postProcessing["png_deflate_cmd"]);
$text .= checkExternalCommand("JPEG OPTIMIZE", $postProcessing["jpeg_optimize"], $postProcessing["jpeg_optimize_cmd"]);
if (!$no) {
$text .= print_r(gd_info(), 1);
}
@@ -5391,6 +5546,7 @@ if (is_callable($hookBeforeCImage)) {
'blur' => $blur,
'convolve' => $convolve,
'rotateAfter' => $rotateAfter,
'interlace' => $interlace,
// Output format
'outputFormat' => $outputFormat,
@@ -5398,6 +5554,7 @@ if (is_callable($hookBeforeCImage)) {
// Other
'postProcessing' => $postProcessing,
'lossy' => $lossy,
));
verbose(print_r($allConfig, 1));
extract($allConfig);
@@ -5445,7 +5602,8 @@ EOD;
/**
* Load, process and output the image
*/
$img->log("Incoming arguments: " . print_r(verbose(), 1))
$img->log("PHP version: " . phpversion())
->log("Incoming arguments: " . print_r(verbose(), 1))
->setSaveFolder($cachePath)
->useCache($useCache)
->setSource($srcImage, $imagePath)
@@ -5478,10 +5636,14 @@ $img->log("Incoming arguments: " . print_r(verbose(), 1))
'blur' => $blur,
'convolve' => $convolve,
'rotateAfter' => $rotateAfter,
'interlace' => $interlace,
// Output format
'outputFormat' => $outputFormat,
'dpr' => $dpr,
// Postprocessing using external tools
'lossy' => $lossy,
)
)
->loadImageDetails()

File diff suppressed because one or more lines are too long

22
webroot/tests.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
$links = [
"img.php?src=car.png&v",
"img.php?src=car.png&w=700&v",
];
?><!doctype html>
<html>
<head>
<title>Links to use for testing</title>
</head>
<body>
<h1>Links useful for testing</h1>
<p>A collection of linkt to use to test various aspects of the cimage process.</p>
<ul>
<?php foreach ($links as $link) : ?>
<li><a href="<?= $link ?>"><?= $link ?></a></li>
<?php endforeach; ?>
</ul>
</body>
</html>