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

A lot of updates when preparing for release.

This commit is contained in:
Mikael Roos
2013-10-03 18:16:33 +02:00
parent fedbf9e381
commit 664fd6a3be
4 changed files with 813 additions and 130 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -26,6 +26,12 @@ Enjoy!
Mikael Roos (me@mikaelroos.se) Mikael Roos (me@mikaelroos.se)
License
-------------------------------------
Free and opensource software. License according to MIT.
Installation Installation
------------------------------------- -------------------------------------
@@ -52,6 +58,24 @@ RewriteRule ^image/(.*)$ img/img.php?src=$1 [QSA,NC,L]
Now you can access and resize your images through `/image/someimage.jpg?w=80`. Very handy. Now you can access and resize your images through `/image/someimage.jpg?w=80`. Very handy.
Usage
-------------------------------------
`img.php?src=image.jpg&sharpen`
-v, -verbose, Do verbose output and print out a log what happens.
-no-cache, Do not use the cached version, do all conversions.
-skip-original, Skip using the original image, always resize and use cached image.
-save-as, Save image as jpg, png or gif, loosing transparency.
-sharpen to appy a filter that sharpens the image. Good to apply when resizing to smaller dimensions.
-emboss to apply a emboss effect.
-blur to apply a blur effect.
-palette to create a palette version of the image with up to 256 colors.
Revision history Revision history
------------------------------------- -------------------------------------
@@ -64,13 +88,16 @@ center of the image from which the crop is done.
* Support for resizing opaque images. * Support for resizing opaque images.
* Clean up code in `CImage.php`. * Clean up code in `CImage.php`.
* Better errorhandling for invalid dimensions. * Better errorhandling for invalid dimensions.
* Crop-to-fit does not work. * Crop-to-fit with offste top, bottom, center, left, right, center.
* Define the color of the background of the resulting image, when loosing transparency.
v0.3x (latest) v0.3.x (latest)
* Adding grid column size as predefined size, c1-c24 for a 24 column grid. Configure in `img.php`.
* Corrected error on naming cache-files using subdir. * Corrected error on naming cache-files using subdir.
* Corrected calculation error on width & height for crop-to-fit. * Corrected calculation error on width & height for crop-to-fit.
* Adding effects for sharpen, emboss and blur through imageconvolution using matrixes.
v0.3 (2012-10-02) v0.3 (2012-10-02)
@@ -101,4 +128,4 @@ v0.1 (2012-04-25)
* Initial release after rewriting some older code I had lying around. * Initial release after rewriting some older code I had lying around.
. .
..: Copyright 2012 by Mikael Roos (me@mikaelroos.se) ..: Copyright 2012-2013 by Mikael Roos (me@mikaelroos.se)

1
cache/README.md vendored
View File

@@ -1 +0,0 @@
Make this directory writable by the webserver.

96
img.php
View File

@@ -10,30 +10,68 @@
error_reporting(-1); error_reporting(-1);
set_time_limit(20); set_time_limit(20);
// Use preprocessing of images
define('PNG_FILTER', '/usr/local/bin/optipng -q');
define('PNG_DEFLATE', '/usr/local/bin/pngout -q');
define('JPEG_OPTIMIZE', '/usr/local/bin/jpegtran -copy none -optimize');
// Append ending slash // Append ending slash
$cimageClassFile = __DIR__ .'/CImage.php'; $cimageClassFile = __DIR__ .'/CImage.php';
$pathToImages = __DIR__.'/img/'; $pathToImages = __DIR__.'/img/';
$pathToCache = __DIR__.'/cache/'; $pathToCache = __DIR__.'/cache/';
$maxWidth = $maxHeight = 2000; $maxWidth = $maxHeight = 2000;
$gridColumnWidth = 30;
$gridGutterWidth = 10;
$gridColumns = 24;
// settings for do not largen smaller images
// settings for max image dimensions
// Set areas to map constant to value, easier to use with width or height // Set sizes to map constant to value, easier to use with width or height
$area = array( $sizes = array(
'w1' => 613, 'w1' => 613,
'w2' => 630,
); );
// Add column width to $area, useful for use as predefined size for width (or height).
for($i = 1; $i <= $gridColumns; $i++) {
$sizes['c' . $i] = ($gridColumnWidth + $gridGutterWidth) * $i - $gridGutterWidth;
}
// Get input from querystring // Get input from querystring
$srcImage = isset($_GET['src']) ? $_GET['src'] : null; $srcImage = isset($_GET['src']) ? $_GET['src'] : null;
$newWidth = isset($_GET['width']) ? $_GET['width'] : (isset($_GET['w']) ? $_GET['w'] : null); $newWidth = isset($_GET['width']) ? $_GET['width'] : (isset($_GET['w']) ? $_GET['w'] : null);
$newHeight = isset($_GET['height']) ? $_GET['height'] : (isset($_GET['h']) ? $_GET['h'] : null); $newHeight = isset($_GET['height']) ? $_GET['height'] : (isset($_GET['h']) ? $_GET['h'] : null);
$keepRatio = isset($_GET['no-ratio']) ? false : true; $keepRatio = isset($_GET['no-ratio']) ? false : true;
$cropToFit = isset($_GET['crop-to-fit']) ? true : false; $cropToFit = isset($_GET['crop-to-fit']) ? true : false;
$area = isset($_GET['area']) ? $_GET['area'] : null;
$crop = isset($_GET['crop']) ? $_GET['crop'] : (isset($_GET['c']) ? $_GET['c'] : null); $crop = isset($_GET['crop']) ? $_GET['crop'] : (isset($_GET['c']) ? $_GET['c'] : null);
$quality = isset($_GET['quality']) ? $_GET['quality'] : (isset($_GET['q']) ? $_GET['q'] : 100); $quality = isset($_GET['quality']) ? $_GET['quality'] : (isset($_GET['q']) ? $_GET['q'] : null);
$verbose = (isset($_GET['verbose']) || isset($_GET['v'])) ? true : false;
$useCache = isset($_GET['no-cache']) ? false : true;
$useOriginal = isset($_GET['skip-original']) ? false : true;
$saveAs = isset($_GET['save-as']) ? $_GET['save-as'] : null;
$sharpen = isset($_GET['sharpen']) ? true : null;
$emboss = isset($_GET['emboss']) ? true : null;
$blur = isset($_GET['blur']) ? true : null;
$palette = isset($_GET['palette']) ? true : null;
// Check to replace area
if(isset($area[$newWidth])) {
$newWidth = $area[$newWidth]; // Check to replace predefined size
if(isset($sizes[$newWidth])) {
$newWidth = $sizes[$newWidth];
} }
if(isset($sizes[$newHeight])) {
$newHeight = $sizes[$newHeight];
}
// Add all filters to an array // Add all filters to an array
$filters = array(); $filters = array();
@@ -44,6 +82,8 @@ for($i=0; $i<10;$i++) {
if($filter) { $filters[] = $filter; } if($filter) { $filters[] = $filter; }
} }
// Do some sanity checks // Do some sanity checks
function errorPage($msg) { function errorPage($msg) {
header("Status: 404 Not Found"); header("Status: 404 Not Found");
@@ -58,10 +98,44 @@ is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) or errorPage('W
is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) or errorPage('Hight out of range.'); is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) or errorPage('Hight out of range.');
$quality >= 0 and $quality <= 100 or errorPage('Quality out of range'); $quality >= 0 and $quality <= 100 or errorPage('Quality out of range');
// Create the image object
// Display image if vebose mode
if($verbose) {
$query = array();
parse_str($_SERVER['QUERY_STRING'], $query);
unset($query['verbose']);
unset($query['v']);
unset($query['nocache']);
$url1 = '?' . http_build_query($query);
echo <<<EOD
<a href=$url1><code>$url1</code></a><br>
<img src='{$url1}' />
EOD;
}
// Create and output the image
require($cimageClassFile); require($cimageClassFile);
$img = new CImage($srcImage, $pathToImages, $pathToCache); $img = new CImage($srcImage, $pathToImages, $pathToCache);
$img->ResizeAndOutput(array('newWidth'=>$newWidth, 'newHeight'=>$newHeight, 'keepRatio'=>$keepRatio, $img->ResizeAndOutput(array(
'cropToFit'=>$cropToFit, 'quality'=>$quality, 'newWidth' => $newWidth,
'crop'=>$crop, 'filters'=>$filters, 'newHeight' => $newHeight,
)); 'keepRatio' => $keepRatio,
'cropToFit' => $cropToFit,
'area' => $area,
'quality' => $quality,
'crop' => $crop,
'filters' => $filters,
'verbose' => $verbose,
'useCache' => $useCache,
'useOriginal' => $useOriginal,
'saveAs' => $saveAs,
'sharpen' => $sharpen,
'emboss' => $emboss,
'blur' => $blur,
'palette' => $palette,
));