14 namespace Depage\Graphics;
23 if (strpos($class, __NAMESPACE__ .
'\\') == 0) {
24 $class = str_replace(
'\\',
'/', str_replace(__NAMESPACE__ .
'\\',
'', $class));
25 $file = __DIR__ .
'/' . $class .
'.php';
27 if (file_exists($file)) {
33 spl_autoload_register(__NAMESPACE__ .
'\autoload');
97 private $oldIgnoreUserAbort =
false;
107 public static function factory($options = array())
109 $extension = (isset($options[
'extension'])) ? $options[
'extension'] :
'gd';
111 if ($extension ==
'im' || $extension ==
'imagemagick') {
112 if (isset($options[
'executable'])) {
116 if ($executable ==
null) {
117 trigger_error(
"Cannot find ImageMagick, falling back to GD", E_USER_WARNING);
119 $options[
'executable'] = $executable;
124 } elseif ($extension ==
'gm' || $extension ==
'graphicsmagick') {
125 if (isset($options[
'executable'])) {
129 if ($executable ==
null) {
130 trigger_error(
"Cannot find GraphicsMagick, falling back to GD", E_USER_WARNING);
132 $options[
'executable'] = $executable;
148 $this->background = (isset($options[
'background'])) ? $options[
'background'] :
'transparent';
149 $this->quality = (isset($options[
'quality'])) ? intval($options[
'quality']) :
null;
150 $this->format = (isset($options[
'format'])) ? $options[
'format'] :
null;
151 $this->optimize = (isset($options[
'optimize'])) ? $options[
'optimize'] :
false;
152 $this->optimizers = (isset($options[
'optimizers'])) ? $options[
'optimizers'] : array();
180 public function addCrop($width, $height, $x = 0, $y = 0)
182 $this->queue[] = array(
'crop', func_get_args());
197 $this->queue[] = array(
'resize', func_get_args());
212 $this->queue[] = array(
'thumb', func_get_args());
227 $this->queue[] = array(
'thumbfill', func_get_args());
242 return (is_numeric($number)) ? intval($number) :
null;
256 if (!is_numeric($width) && !is_numeric($height)) {
259 } elseif (!is_numeric($height)) {
260 $height = round(($this->size[1] / $this->size[0]) * $width);
261 } elseif (!is_numeric($width)) {
262 $width = round(($this->size[0] / $this->size[1]) * $height);
265 return array($width, $height);
277 foreach ($this->queue as $task) {
279 $arguments = array_map(array($this,
'escapeNumber'), $task[1]);
281 call_user_func_array(array($this, $action), $arguments);
300 $this->outputFormat = ($this->format ==
null) ? $this->
obtainFormat($this->output) : $this->format;
301 $this->size = $this->getImageSize();
302 $this->otherRender =
false;
306 $this->oldIgnoreUserAbort = ignore_user_abort();
307 ignore_user_abort(
true);
316 ignore_user_abort($this->oldIgnoreUserAbort);
328 if (!file_exists($filename)) {
333 $success = $optimizer->optimize($filename);
345 $this->outputLockFp = fopen($this->output .
".lock",
'w');
346 $locked = flock($this->outputLockFp, LOCK_EX | LOCK_NB, $wouldblock);
348 if (!$locked && $wouldblock) {
349 $this->otherRender =
true;
350 flock($this->outputLockFp, LOCK_EX);
361 if (isset($this->outputLockFp)) {
362 flock($this->outputLockFp, LOCK_UN);
363 unlink($this->output .
".lock");
365 $this->outputLockFp =
null;
377 $parts = explode(
'.', $fileName);
378 $extension = strtolower(end($parts));
380 if ($extension ==
'jpeg') {
384 && $extension !=
'png'
385 && $extension !=
'gif'
387 if (is_callable(
'getimagesize') && file_exists($fileName)) {
388 $info = getimagesize($fileName);
389 if (isset($info[2])) {
394 } elseif ($format == 2) {
396 } elseif ($format == 3) {
414 public static function which($binary)
416 exec(
'which ' . $binary, $commandOutput, $returnStatus);
417 if ($returnStatus === 0) {
418 return $commandOutput[0];
441 if ($this->outputFormat ==
'jpg') {
443 is_numeric($this->quality)
444 && $this->quality >= 0
445 && $this->quality <= 100
451 } elseif ($this->outputFormat ==
"webp") {
453 is_numeric($this->quality)
454 && $this->quality >= 0
455 && $this->quality <= 100
461 } elseif ($this->outputFormat ==
'png') {
463 is_numeric($this->quality)
464 && $this->quality >= 0
465 && $this->quality <= 95
466 && $this->quality % 10 <= 5
468 $quality = sprintf(
"%02d", $this->quality);
488 protected function bypassTest($width, $height, $x = 0, $y = 0)
491 ($width !==
null && $width < 1)
492 || ($height !==
null && $height < 1)
493 || ($width ==
null && $height ==
null)
501 $width == $this->size[0]
502 && $height == $this->size[1]
518 if ($this->input != $this->output) {
519 copy($this->input, $this->output);
autoload($class)
PHP autoloader.
general graphics related exception class
Input file not found exception.
$background
Image background string.
$quality
Image quality string.
processQueue()
Process action queue.
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
$inputFormat
Input image format.
getQuality()
Returns quality-index for current image format.
addThumb($width, $height)
Adds thumb action.
$queue
Action queue array.
$otherRender
otherRender is set to true if another render process has already locked file
render($input, $output=null)
Main method for image handling.
addThumbfill($width, $height)
Adds thumb-fill action.
static factory($options=array())
graphics object factory
__construct($options=array())
graphics class constructor
addCrop($width, $height, $x=0, $y=0)
Adds crop action.
$outputFormat
Output image format.
setQuality($quality)
Sets quality parameter.
dimensions($width, $height)
Scales image dimensions.
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
obtainFormat($fileName)
Determines image format from file extension.
$optimizers
List of optimizer binaries.
static which($binary)
Executes "which" command.
$bypass
Process bypass bool.
renderFinished()
Called after rendering has finished.
addBackground($background)
Background "action".
addResize($width, $height)
Adds resize action.
bypass()
Runs bypass (copies file)
escapeNumber($number)
Validates integers.
$size
Image size array(width, height)
$optimize
Optimize output images.
PHP GD extension interface.
GraphicsMagick interface.