mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-26 17:14:27 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
570fe57240 | ||
|
28f3c55d8c | ||
|
230e9189a3 | ||
|
e886c86f72 | ||
|
d7b6f7e25d | ||
|
7849f69801 | ||
|
7117ac2a4c |
42
CImage.php
42
CImage.php
@@ -40,8 +40,6 @@ class CImage {
|
||||
private $quality;
|
||||
public $filters;
|
||||
public $saveFolder;
|
||||
public $newName;
|
||||
//private $newFileName; // OBSOLETE, using cacheFileName instead.
|
||||
private $mime; // Calculated from source image
|
||||
private $width; // Calculated from source image
|
||||
private $height; // Calculated from source image
|
||||
@@ -71,16 +69,14 @@ class CImage {
|
||||
* @param string $imageName filename which may contain subdirectory.
|
||||
* @param string $imageFolder path to root folder for images.
|
||||
* @param string $saveFolder path to folder where to save the new file or null to skip saving.
|
||||
* @param string $newName new filename or leave to null to autogenerate filename.
|
||||
*/
|
||||
public function __construct($imageName=null, $imageFolder=null, $saveFolder=null, $newName=null) {
|
||||
public function __construct($imageName=null, $imageFolder=null, $saveFolder=null) {
|
||||
$this->imageName = ltrim($imageName, '/');
|
||||
$this->imageFolder = rtrim($imageFolder, '/');
|
||||
$this->pathToImage = $this->imageFolder . '/' . $this->imageName;
|
||||
$this->fileExtension = pathinfo($this->pathToImage, PATHINFO_EXTENSION);
|
||||
$this->extension = $this->fileExtension;
|
||||
$this->saveFolder = $saveFolder;
|
||||
$this->newName = $newName;
|
||||
}
|
||||
|
||||
|
||||
@@ -109,11 +105,11 @@ class CImage {
|
||||
foreach($this->log as $val) {
|
||||
if(is_array($val)) {
|
||||
foreach($val as $val1) {
|
||||
$log .= $val1 . '<br/>';
|
||||
$log .= htmlentities($val1) . '<br/>';
|
||||
}
|
||||
}
|
||||
else {
|
||||
$log .= $val . '<br/>';
|
||||
$log .= htmlentities($val) . '<br/>';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,9 +271,12 @@ EOD;
|
||||
$info = list($width, $height, $type, $attr) = getimagesize($file);
|
||||
!empty($info) or $this->RaiseError("The file doesn't seem to be an image.");
|
||||
$mime = $info['mime'];
|
||||
$time = filemtime($file);
|
||||
$lastModified = filemtime($file);
|
||||
$gmdate = gmdate("D, d M Y H:i:s", $lastModified);
|
||||
|
||||
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $time){
|
||||
if(!$this->verbose) { header('Last-Modified: ' . $gmdate . " GMT"); }
|
||||
|
||||
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified){
|
||||
if($this->verbose) {
|
||||
$this->Log("304 not modified");
|
||||
$this->VerboseOutput();
|
||||
@@ -285,8 +284,6 @@ EOD;
|
||||
}
|
||||
header("HTTP/1.0 304 Not Modified");
|
||||
} else {
|
||||
$gmdate = gmdate("D, d M Y H:i:s", $time);
|
||||
|
||||
if($this->verbose) {
|
||||
$this->Log("Last modified: " . $gmdate . " GMT");
|
||||
$this->VerboseOutput();
|
||||
@@ -294,7 +291,6 @@ EOD;
|
||||
}
|
||||
|
||||
header('Content-type: ' . $mime);
|
||||
header('Last-Modified: ' . $gmdate . " GMT");
|
||||
readfile($file);
|
||||
}
|
||||
exit;
|
||||
@@ -336,6 +332,7 @@ EOD;
|
||||
protected function SetQuality() {
|
||||
if(!$this->quality) {
|
||||
switch($this->extension) {
|
||||
case 'jpeg':
|
||||
case 'jpg':
|
||||
$this->quality = self::JPEG_QUALITY_DEFAULT;
|
||||
break;
|
||||
@@ -495,8 +492,8 @@ EOD;
|
||||
$height = $this->height;
|
||||
|
||||
if($this->crop) {
|
||||
$width = $this->crop['width'];
|
||||
$height = $this->crop['height'];
|
||||
$width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->width + $this->crop['width'] : $this->crop['width'];
|
||||
$height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->height + $this->crop['height'] : $this->crop['height'];
|
||||
|
||||
if($this->crop['start_x'] == 'left') {
|
||||
$this->crop['start_x'] = 0;
|
||||
@@ -561,6 +558,13 @@ EOD;
|
||||
|
||||
// Use newWidth and newHeigh as defined width/height, image should fit the area.
|
||||
if($this->cropToFit) {
|
||||
/*
|
||||
if($cropToFit && $newWidth && $newHeight) {
|
||||
$targetRatio = $newWidth / $newHeight;
|
||||
$cropWidth = $targetRatio > $aspectRatio ? $width : round($height * $targetRatio);
|
||||
$cropHeight = $targetRatio > $aspectRatio ? round($width / $targetRatio) : $height;
|
||||
}
|
||||
*/
|
||||
$ratioWidth = $width / $this->newWidth;
|
||||
$ratioHeight = $height / $this->newHeight;
|
||||
$ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight;
|
||||
@@ -924,6 +928,15 @@ EOD;
|
||||
|
||||
// Resize by crop to fit
|
||||
if($this->cropToFit) {
|
||||
/*
|
||||
$cropX = round(($width - $cropWidth) / 2);
|
||||
$cropY = round(($height - $cropHeight) / 2);
|
||||
$imageResized = createImageKeepTransparency($newWidth, $newHeight);
|
||||
imagecopyresampled($imageResized, $image, 0, 0, $cropX, $cropY, $newWidth, $newHeight, $cropWidth, $cropHeight);
|
||||
$image = $imageResized;
|
||||
$width = $newWidth;
|
||||
$height = $newHeight;
|
||||
*/
|
||||
$this->Log("Crop to fit");
|
||||
$cropX = round(($this->cropWidth/2) - ($this->newWidth/2));
|
||||
$cropY = round(($this->cropHeight/2) - ($this->newHeight/2));
|
||||
@@ -996,6 +1009,7 @@ EOD;
|
||||
*/
|
||||
protected function SaveToCache() {
|
||||
switch($this->extension) {
|
||||
case 'jpeg':
|
||||
case 'jpg':
|
||||
if($this->saveFolder) {
|
||||
$this->Log("Saving image as JPEG to cache using quality = {$this->quality}.");
|
||||
|
21
README.md
21
README.md
@@ -27,12 +27,12 @@ License according to MIT.
|
||||
Installation and get going
|
||||
-------------------------------------
|
||||
|
||||
**Latest stable version is v0.4 released 2013-10-08.**
|
||||
**Latest stable version is v0.4.1 released 2014-01-27.**
|
||||
|
||||
```bash
|
||||
git clone git://github.com/mosbth/cimage.git
|
||||
cd cimage
|
||||
git checkout v0.4
|
||||
git checkout v0.4.1
|
||||
```
|
||||
|
||||
Make the cache-directory writable by the webserver.
|
||||
@@ -60,7 +60,7 @@ Usage
|
||||
| `nr, no-ratio, stretch` | Do *not* keep aspect ratio when resizing using both width & height constraints. Results in stretching the image, if needed, to fit in the resulting box. |
|
||||
| `cf, crop-to-fit` | Set together with both `h` & `w` to make the image fit into dimensions, and crop out the rest of the image. |
|
||||
| `a, area` | Define the area of the image to work with. Set `area=10,10,10,10` (top,right,bottom,left) to crop out the 10% of the outermost area. It works like an offset to define which part of the image you want to process. Its an alternative to use `crop`. |
|
||||
| `c, crop` | Crops an area from the original image, set width, height, start_x and start_y to define the area to crop, for example `crop=100,100,10,10` (`crop=width,height,start_x,start_y`). Left top corner is 0, 0. You can use left, right or center when setting start_x. You may use top, bottom or center when setting start_y. |
|
||||
| `c, crop` | Crops an area from the original image, set width, height, start_x and start_y to define the area to crop, for example `crop=100,100,10,10` (`crop=width,height,start_x,start_y`). Left top corner is 0, 0. You can use left, right or center when setting start_x. You may use top, bottom or center when setting start_y. You can use negative values for x and y. Use 0 for width or height to get the width/height of the original image. Use negative values for width/height to get original width/height minus selected value. |
|
||||
| `q, quality` | Quality affects lossy compression and file size for JPEG images by setting the quality between 1-100, default is 60. Quality has no effect on PNG or GIF. |
|
||||
| `d, deflate` | For PNG images it defines the compression algorithm, values can be 1-9, default is defined by PHP GD. Quality has no effect on JPEG or GIF. |
|
||||
| `sharpen` | Appy a filter that sharpens the image. |
|
||||
@@ -83,6 +83,18 @@ Combine the parameters to get the desired behavior and resulting image. For exam
|
||||
Revision history
|
||||
-------------------------------------
|
||||
|
||||
v0.4.1 (2014-01-27)
|
||||
|
||||
* Changed => to == on Modified-Since.
|
||||
* Always send Last-Modified-Header.
|
||||
* Added `htmlentities()` to verbose output.
|
||||
* Fixed support for jpeg, not only jpg.
|
||||
* Fixed crop whole image by setting crop=0,0,0,0
|
||||
* Use negative values for crop width & height to base calulation on original width/height and withdraw selected amount.
|
||||
* Correcting jpeg when setting quality.
|
||||
* Removed obsolete reference to `$newName` in `CImage::__construct()` (issue 1).
|
||||
|
||||
|
||||
v0.4 (2013-10-08)
|
||||
|
||||
* Improved support for pre-defined sizes.
|
||||
@@ -101,7 +113,8 @@ v0.4 (2013-10-08)
|
||||
* Added usage of all parameters to README.md
|
||||
* Added documentation here http://dbwebb.se/opensource/cimage
|
||||
* Adding `.gitignore`
|
||||
* Readding `cache` directory
|
||||
* Re-adding `cache` directory
|
||||
|
||||
|
||||
v0.3 (2012-10-02)
|
||||
|
||||
|
9
img.php
9
img.php
@@ -24,9 +24,9 @@ set_exception_handler('myExceptionHandler');
|
||||
|
||||
|
||||
// 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');
|
||||
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
|
||||
@@ -151,13 +151,14 @@ else {
|
||||
|
||||
|
||||
|
||||
// Display image if vebose mode
|
||||
// Display image if verbose mode
|
||||
if($verbose) {
|
||||
$query = array();
|
||||
parse_str($_SERVER['QUERY_STRING'], $query);
|
||||
unset($query['verbose']);
|
||||
unset($query['v']);
|
||||
unset($query['nocache']);
|
||||
unset($query['nc']);
|
||||
$url1 = '?' . http_build_query($query);
|
||||
echo <<<EOD
|
||||
<a href=$url1><code>$url1</code></a><br>
|
||||
|
BIN
img/kodim15.png
Normal file
BIN
img/kodim15.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 589 KiB |
Reference in New Issue
Block a user