mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-05 15:47:30 +02:00
A lot of updates when preparing for release.
This commit is contained in:
813
CImage.php
813
CImage.php
File diff suppressed because it is too large
Load Diff
33
README.md
33
README.md
@@ -26,6 +26,12 @@ Enjoy!
|
||||
Mikael Roos (me@mikaelroos.se)
|
||||
|
||||
|
||||
License
|
||||
-------------------------------------
|
||||
|
||||
Free and opensource software. License according to MIT.
|
||||
|
||||
|
||||
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.
|
||||
|
||||
|
||||
|
||||
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
|
||||
-------------------------------------
|
||||
|
||||
@@ -64,13 +88,16 @@ center of the image from which the crop is done.
|
||||
* Support for resizing opaque images.
|
||||
* Clean up code in `CImage.php`.
|
||||
* 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 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)
|
||||
@@ -101,4 +128,4 @@ v0.1 (2012-04-25)
|
||||
* 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
1
cache/README.md
vendored
@@ -1 +0,0 @@
|
||||
Make this directory writable by the webserver.
|
96
img.php
96
img.php
@@ -10,30 +10,68 @@
|
||||
error_reporting(-1);
|
||||
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
|
||||
$cimageClassFile = __DIR__ .'/CImage.php';
|
||||
$pathToImages = __DIR__.'/img/';
|
||||
$pathToCache = __DIR__.'/cache/';
|
||||
$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
|
||||
$area = array(
|
||||
// Set sizes to map constant to value, easier to use with width or height
|
||||
$sizes = array(
|
||||
'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
|
||||
$srcImage = isset($_GET['src']) ? $_GET['src'] : 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);
|
||||
$keepRatio = isset($_GET['no-ratio']) ? false : true;
|
||||
$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);
|
||||
$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
|
||||
$filters = array();
|
||||
@@ -44,6 +82,8 @@ for($i=0; $i<10;$i++) {
|
||||
if($filter) { $filters[] = $filter; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Do some sanity checks
|
||||
function errorPage($msg) {
|
||||
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.');
|
||||
$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);
|
||||
$img = new CImage($srcImage, $pathToImages, $pathToCache);
|
||||
$img->ResizeAndOutput(array('newWidth'=>$newWidth, 'newHeight'=>$newHeight, 'keepRatio'=>$keepRatio,
|
||||
'cropToFit'=>$cropToFit, 'quality'=>$quality,
|
||||
'crop'=>$crop, 'filters'=>$filters,
|
||||
));
|
||||
$img->ResizeAndOutput(array(
|
||||
'newWidth' => $newWidth,
|
||||
'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,
|
||||
));
|
||||
|
||||
|
Reference in New Issue
Block a user