mirror of
https://github.com/mosbth/cimage.git
synced 2025-07-31 21:40:12 +02:00
implemented filters and quality, chenged how arguments was handled.
This commit is contained in:
138
CImage.php
138
CImage.php
@@ -19,7 +19,12 @@ class CImage {
|
||||
private $cropWidth;
|
||||
private $cropHeight;
|
||||
public $keepRatio;
|
||||
public $cropToFit;
|
||||
public $crop;
|
||||
public $crop_x;
|
||||
public $crop_y;
|
||||
private $quality;
|
||||
public $filters;
|
||||
public $saveFolder;
|
||||
public $newName;
|
||||
private $newFileName;
|
||||
@@ -35,20 +40,12 @@ class CImage {
|
||||
* Constructor, can take arguments to init the object.
|
||||
*
|
||||
* @param $pathToImage string the filepath to the image.
|
||||
* @param $newWidth integer the new width or null.
|
||||
* @param $newHeight integer the new width or null.
|
||||
* @param $keepRatio boolean true to keep aspect ratio else false.
|
||||
* @param $saveFolder string path to folder where to save the new file or null to skip saving.
|
||||
* @param $newName string new filename or leave to null to autogenerate filename.
|
||||
*/
|
||||
public function __construct($pathToImage=null, $newWidth=null, $newHeight=null,
|
||||
$keepRatio=true, $crop=false, $saveFolder=null, $newName=null) {
|
||||
public function __construct($pathToImage=null, $saveFolder=null, $newName=null) {
|
||||
$this->pathToImage = $pathToImage;
|
||||
$this->fileExtension = pathinfo($this->pathToImage, PATHINFO_EXTENSION);
|
||||
$this->newWidth = $newWidth;
|
||||
$this->newHeight = $newHeight;
|
||||
$this->keepRatio = $keepRatio;
|
||||
$this->crop = $crop;
|
||||
$this->saveFolder = $saveFolder;
|
||||
$this->newName = $newName;
|
||||
}
|
||||
@@ -69,8 +66,20 @@ class CImage {
|
||||
*/
|
||||
public function CreateFilename() {
|
||||
$parts = pathinfo($this->pathToImage);
|
||||
$crop = $this->crop ? '_c_' : null;
|
||||
return $this->saveFolder . '/' . $parts['filename'] . '_' . round($this->newWidth) . '_' . round($this->newHeight) . $crop . '.' . $parts['extension'];
|
||||
$crop = $this->cropToFit ? '_cf' : null;
|
||||
$crop_x = $this->crop_x ? "_x{$this->crop_x}" : null;
|
||||
$crop_y = $this->crop_y ? "_y{$this->crop_y}" : null;
|
||||
$quality = $this->quality == 100 ? null : "_q{$this->quality}";
|
||||
$filters = null;
|
||||
foreach($this->filters as $filter) {
|
||||
if(is_array($filter)) {
|
||||
$filters .= "_f{$filter['id']}";
|
||||
for($i=1;$i<=$filter['argc'];$i++) {
|
||||
$filters .= ":".$filter["arg{$i}"];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->saveFolder . '/' . $parts['filename'] . '_' . round($this->newWidth) . '_' . round($this->newHeight) . $crop . $crop_x . $crop_y . $quality . $filters . '.' . $parts['extension'];
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +89,9 @@ class CImage {
|
||||
public function Init() {
|
||||
is_null($this->newWidth) or is_numeric($this->newWidth) or $this->RaiseError('Width not numeric');
|
||||
is_null($this->newHeight) or is_numeric($this->newHeight) or $this->RaiseError('Height not numeric');
|
||||
is_numeric($this->quality) and $this->quality >= 0 and $this->quality <= 100 or $this->RaiseError('Quality not in range.');
|
||||
//is_numeric($this->crop_x) && is_numeric($this->crop_y) or $this->RaiseError('Quality not in range.');
|
||||
//filter
|
||||
is_readable($this->pathToImage) or $this->RaiseError('File does not exist.');
|
||||
in_array($this->fileExtension, $this->validExtensions) or $this->RaiseError('Not a valid file extension.');
|
||||
is_null($this->saveFolder) or is_writable($this->saveFolder) or $this->RaiseError('Save directory does not exist or is not writable.');
|
||||
@@ -126,6 +138,34 @@ class CImage {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Map filter name to PHP filter and id.
|
||||
*
|
||||
* @param string $name the name of the filter.
|
||||
*/
|
||||
private function MapFilter($name) {
|
||||
$map = array(
|
||||
'negate' => array('id'=>0, 'argc'=>0, 'type'=>IMG_FILTER_NEGATE),
|
||||
'grayscale' => array('id'=>1, 'argc'=>0, 'type'=>IMG_FILTER_GRAYSCALE),
|
||||
'brightness' => array('id'=>2, 'argc'=>1, 'type'=>IMG_FILTER_BRIGHTNESS),
|
||||
'contrast' => array('id'=>3, 'argc'=>1, 'type'=>IMG_FILTER_CONTRAST),
|
||||
'colorize' => array('id'=>4, 'argc'=>4, 'type'=>IMG_FILTER_COLORIZE),
|
||||
'edgedetect' => array('id'=>5, 'argc'=>0, 'type'=>IMG_FILTER_EDGEDETECT),
|
||||
'emboss' => array('id'=>6, 'argc'=>0, 'type'=>IMG_FILTER_EMBOSS),
|
||||
'gaussian_blur' => array('id'=>7, 'argc'=>0, 'type'=>IMG_FILTER_GAUSSIAN_BLUR),
|
||||
'selective_blur' => array('id'=>8, 'argc'=>0, 'type'=>IMG_FILTER_SELECTIVE_BLUR),
|
||||
'mean_removal' => array('id'=>9, 'argc'=>0, 'type'=>IMG_FILTER_MEAN_REMOVAL),
|
||||
'smooth' => array('id'=>10, 'argc'=>1, 'type'=>IMG_FILTER_SMOOTH),
|
||||
'pixelate' => array('id'=>11, 'argc'=>2, 'type'=>IMG_FILTER_PIXELATE),
|
||||
);
|
||||
if(isset($map[$name]))
|
||||
return $map[$name];
|
||||
else {
|
||||
$this->RaiseError('No such filter.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculate new width and height of image.
|
||||
*/
|
||||
@@ -137,7 +177,7 @@ class CImage {
|
||||
if(isset($this->newWidth) && isset($this->newHeight)) {
|
||||
|
||||
// Use newWidth and newHeigh as min width/height, image should fit the area.
|
||||
if($this->crop) {
|
||||
if($this->cropToFit) {
|
||||
$ratioWidth = $this->width/$this->newWidth;
|
||||
$ratioHeight = $this->height/$this->newHeight;
|
||||
$ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight;
|
||||
@@ -179,8 +219,55 @@ class CImage {
|
||||
|
||||
/**
|
||||
* Resize the image and optionally store/cache the new imagefile. Output the image.
|
||||
*
|
||||
* @param integer $newWidth the new width or null. Default is null.
|
||||
* @param integer $newHeight the new width or null. Default is null.
|
||||
* @param boolean $keepRatio true to keep aspect ratio else false. Default is true.
|
||||
* @param boolean $cropToFit true to crop image to fit in box specified by $newWidth and $newHeight. Default is false.
|
||||
* @param integer $quality the quality to use when saving the file, range 0-100, default is full quality which is 100.
|
||||
* @param array $crop.
|
||||
* @param array $filter.
|
||||
*/
|
||||
public function ResizeAndOutput() {
|
||||
public function ResizeAndOutput($args) {
|
||||
$defaults = array(
|
||||
'newWidth'=>null,
|
||||
'newHeight'=>null,
|
||||
'keepRatio'=>true,
|
||||
'cropToFit'=>false,
|
||||
'quality'=>100,
|
||||
'crop'=>array('w'=>null, 'h'=>null, 'x'=>0, 'y'=>0),
|
||||
'filters'=>null,
|
||||
);
|
||||
// Convert crop settins from string to array
|
||||
if(isset($args['crop']) && !is_array($args['crop'])) {
|
||||
$args['crop'] = array();
|
||||
}
|
||||
|
||||
// Convert filter settins from array of string to array of array
|
||||
if(isset($args['filters']) && is_array($args['filters'])) {
|
||||
foreach($args['filters'] as $key => $filterStr) {
|
||||
$parts = explode(',', $filterStr);
|
||||
$filter = $this->MapFilter($parts[0]);
|
||||
$filter['str'] = $filterStr;
|
||||
for($i=1;$i<=$filter['argc'];$i++) {
|
||||
if(isset($parts[$i])) {
|
||||
$filter["arg{$i}"] = $parts[$i];
|
||||
} else {
|
||||
$this->RaiseError('Missing arg to filter, review how many arguments are needed at http://php.net/manual/en/function.imagefilter.php');
|
||||
}
|
||||
}
|
||||
$args['filters'][$key] = $filter;
|
||||
}
|
||||
}
|
||||
//echo "<pre>" . print_r($args['filters'], true) . "</pre>";
|
||||
|
||||
// Merge default arguments with incoming and set properties.
|
||||
$args = array_merge($defaults, $args);
|
||||
foreach($defaults as $key=>$val) {
|
||||
$this->{$key} = $args[$key];
|
||||
}
|
||||
|
||||
// Init the object and do sanity checks on arguments
|
||||
$this->Init()->CalculateNewWidthAndHeight();
|
||||
|
||||
// Use original image?
|
||||
@@ -188,8 +275,6 @@ class CImage {
|
||||
$this->Output($this->pathToImage);
|
||||
}
|
||||
|
||||
//echo "{$this->newWidth}:{$this->newHeight}";
|
||||
|
||||
// Check cache before resizing.
|
||||
$this->newFileName = $this->CreateFilename();
|
||||
if(is_readable($this->newFileName)) {
|
||||
@@ -208,10 +293,9 @@ class CImage {
|
||||
/**
|
||||
* Resize, crop and output the image.
|
||||
*
|
||||
* @param $imageQuality number the quality to use when saving the file, default is full quality.
|
||||
*/
|
||||
public function ResizeAndSave($imageQuality="100") {
|
||||
if($this->crop) {
|
||||
public function ResizeAndSave() {
|
||||
if($this->cropToFit) {
|
||||
$cropX = ($this->cropWidth/2) - ($this->newWidth/2);
|
||||
$cropY = ($this->cropHeight/2) - ($this->newHeight/2);
|
||||
$imgPreCrop = imagecreatetruecolor($this->cropWidth, $this->cropHeight);
|
||||
@@ -222,14 +306,26 @@ class CImage {
|
||||
$imageResized = imagecreatetruecolor($this->newWidth, $this->newHeight);
|
||||
imagecopyresampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);
|
||||
}
|
||||
|
||||
|
||||
if(isset($this->filters) && is_array($this->filters)) {
|
||||
foreach($this->filters as $filter) {
|
||||
switch($filter['argc']) {
|
||||
case 0: imagefilter($imageResized, $filter['type']); break;
|
||||
case 1: imagefilter($imageResized, $filter['type'], $filter['arg1']); break;
|
||||
case 2: imagefilter($imageResized, $filter['type'], $filter['arg1'], $filter['arg2']); break;
|
||||
case 3: imagefilter($imageResized, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3']); break;
|
||||
case 4: imagefilter($imageResized, $filter['type'], $filter['arg1'], $filter['arg2'], $filter['arg3'], $filter['arg4']); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch($this->fileExtension)
|
||||
{
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
if(imagetypes() & IMG_JPG) {
|
||||
if($this->saveFolder) {
|
||||
imagejpeg($imageResized, $this->newFileName, $imageQuality);
|
||||
imagejpeg($imageResized, $this->newFileName, $this->quality);
|
||||
}
|
||||
$imgFunction = 'imagejpeg';
|
||||
}
|
||||
@@ -249,7 +345,7 @@ class CImage {
|
||||
$quality = 9 - round(($imageQuality/100) * 9);
|
||||
if (imagetypes() & IMG_PNG) {
|
||||
if($this->saveFolder) {
|
||||
imagepng($imageResized, $this->newFileName, $quality);
|
||||
imagepng($imageResized, $this->newFileName, $this->quality);
|
||||
}
|
||||
$imgFunction = 'imagepng';
|
||||
}
|
||||
|
Reference in New Issue
Block a user