23 if (strpos($class, __NAMESPACE__ .
'\\') == 0) {
24 $class = str_replace(
'\\',
'/', str_replace(__NAMESPACE__ .
'\\',
'', $class));
25 $file = __DIR__ .
'/' . $class .
'.php';
27 if (file_exists($file)) {
33spl_autoload_register(__NAMESPACE__ .
'\autoload');
105 private $oldIgnoreUserAbort =
false;
115 public static function factory($options = array())
117 $extension = (isset($options[
'extension'])) ? $options[
'extension'] :
'gd';
119 if ($extension ==
'imagick' && extension_loaded(
'imagick')) {
121 } elseif ($extension ==
'im' || $extension ==
'imagemagick') {
122 if (isset($options[
'executable'])) {
126 if ($executable ==
null) {
127 trigger_error(
"Cannot find ImageMagick, falling back to GD", E_USER_WARNING);
129 $options[
'executable'] = $executable;
134 } elseif ($extension ==
'gm' || $extension ==
'graphicsmagick') {
135 if (isset($options[
'executable'])) {
139 if ($executable ==
null) {
140 trigger_error(
"Cannot find GraphicsMagick, falling back to GD", E_USER_WARNING);
142 $options[
'executable'] = $executable;
158 $this->background = (isset($options[
'background'])) ? $options[
'background'] :
'transparent';
159 $this->quality = (isset($options[
'quality'])) ? intval($options[
'quality']) :
null;
160 $this->format = (isset($options[
'format'])) ? $options[
'format'] :
null;
161 $this->optimize = (isset($options[
'optimize'])) ? $options[
'optimize'] :
false;
162 $this->optimizers = (isset($options[
'optimizers'])) ? $options[
'optimizers'] : [];
163 $this->limits = (isset($options[
'limits'])) ? $options[
'limits'] : [
167 if (isset($this->limits[
'memory'])) {
168 putenv(
"MAGICK_MEMORY_LIMIT=" . $this->limits[
'memory']);
169 putenv(
"MAGICK_LIMIT_MEMORY=" . $this->limits[
'memory']);
171 if (isset($this->limits[
'map'])) {
172 putenv(
"MAGICK_MAP_LIMIT=" . $this->limits[
'map']);
173 putenv(
"MAGICK_LIMIT_MAP=" . $this->limits[
'map']);
202 public function addCrop($width, $height, $x = 0, $y = 0)
204 $this->queue[] = array(
'crop', func_get_args());
219 $this->queue[] = array(
'resize', func_get_args());
234 $this->queue[] = array(
'thumb', func_get_args());
249 public function addThumbfill($width, $height, $centerX = 50, $centerY = 50)
251 $this->queue[] = array(
'thumbfill', func_get_args());
266 return (is_numeric($number)) ? intval($number) :
null;
280 if (!is_numeric($width) && !is_numeric($height)) {
283 } elseif (!is_numeric($height)) {
284 $height = round(($this->size[1] / $this->size[0]) * $width);
285 } elseif (!is_numeric($width)) {
286 $width = round(($this->size[0] / $this->size[1]) * $height);
289 return array($width, $height);
301 foreach ($this->queue as $task) {
303 $arguments = array_map(array($this,
'escapeNumber'), $task[1]);
305 call_user_func_array(array($this, $action), $arguments);
325 $this->size = $this->getImageSize();
326 $this->otherRender =
false;
330 $this->oldIgnoreUserAbort = ignore_user_abort();
331 ignore_user_abort(
true);
340 ignore_user_abort($this->oldIgnoreUserAbort);
352 if (!file_exists($filename)) {
357 $success = $optimizer->optimize($filename);
369 $this->outputLockFp = fopen($this->output .
".lock",
'w');
370 $locked = flock($this->outputLockFp, LOCK_EX | LOCK_NB, $wouldblock);
372 if (!$locked && $wouldblock) {
373 $this->otherRender =
true;
374 flock($this->outputLockFp, LOCK_EX);
385 if (isset($this->outputLockFp)) {
386 flock($this->outputLockFp, LOCK_UN);
387 unlink($this->output .
".lock");
389 $this->outputLockFp =
null;
401 $parts = explode(
'.', $fileName);
402 $extension = strtolower(end($parts));
404 if ($extension ==
'jpeg') {
408 && $extension !=
'png'
409 && $extension !=
'gif'
411 if (is_callable(
'getimagesize') && file_exists($fileName)) {
412 $info = getimagesize($fileName);
413 if (isset($info[2])) {
438 public static function which($binary)
440 exec(
'which ' . $binary, $commandOutput, $returnStatus);
441 if ($returnStatus === 0) {
442 return $commandOutput[0];
465 if ($this->outputFormat ==
'jpg') {
467 is_numeric($this->quality)
468 && $this->quality >= 0
469 && $this->quality <= 100
475 } elseif ($this->outputFormat ==
"webp") {
477 is_numeric($this->quality)
478 && $this->quality >= 0
479 && $this->quality <= 100
485 } elseif ($this->outputFormat ==
'png') {
487 is_numeric($this->quality)
488 && $this->quality >= 0
489 && $this->quality <= 95
490 && $this->quality % 10 <= 5
492 $quality = sprintf(
"%02d", $this->quality);
512 protected function bypassTest($width, $height, $x = 0, $y = 0)
515 ($width !==
null && $width < 1)
516 || ($height !==
null && $height < 1)
517 || ($width ==
null && $height ==
null)
525 $width == $this->size[0]
526 && $height == $this->size[1]
542 if ($this->input != $this->output) {
543 copy($this->input, $this->output);
general graphics related exception class
Input file not found exception.
$background
Image background string.
$quality
Image quality string.
addThumbfill($width, $height, $centerX=50, $centerY=50)
Adds thumb-fill action.
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.
$limits
limits (mainly for imagemagick and graphicsmagick)
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.
autoload($class)
PHP autoloader.