15namespace Depage\Graphics;
24 if (strpos($class, __NAMESPACE__ .
'\\') == 0) {
25 $class = str_replace(
'\\',
'/', str_replace(__NAMESPACE__ .
'\\',
'', $class));
26 $file = __DIR__ .
'/' . $class .
'.php';
28 if (file_exists($file)) {
34spl_autoload_register(__NAMESPACE__ .
'\autoload');
106 private $oldIgnoreUserAbort =
false;
116 public static function factory($options = array())
118 $extension = (isset($options[
'extension'])) ? $options[
'extension'] :
'gd';
120 if ($extension ==
'imagick' && extension_loaded(
'imagick')) {
122 } elseif ($extension ==
'im' || $extension ==
'imagemagick') {
123 if (isset($options[
'executable'])) {
127 if ($executable ==
null) {
128 trigger_error(
"Cannot find ImageMagick, falling back to GD", E_USER_WARNING);
130 $options[
'executable'] = $executable;
135 } elseif ($extension ==
'gm' || $extension ==
'graphicsmagick') {
136 if (isset($options[
'executable'])) {
140 if ($executable ==
null) {
141 trigger_error(
"Cannot find GraphicsMagick, falling back to GD", E_USER_WARNING);
143 $options[
'executable'] = $executable;
159 $this->background = (isset($options[
'background'])) ? $options[
'background'] :
'transparent';
160 $this->quality = (isset($options[
'quality'])) ? intval($options[
'quality']) :
null;
161 $this->format = (isset($options[
'format'])) ? $options[
'format'] :
null;
162 $this->optimize = (isset($options[
'optimize'])) ? $options[
'optimize'] :
false;
163 $this->optimizers = (isset($options[
'optimizers'])) ? $options[
'optimizers'] : [];
164 $this->limits = (isset($options[
'limits'])) ? $options[
'limits'] : [
168 if (isset($this->limits[
'memory'])) {
169 putenv(
"MAGICK_MEMORY_LIMIT=" . $this->limits[
'memory']);
170 putenv(
"MAGICK_LIMIT_MEMORY=" . $this->limits[
'memory']);
172 if (isset($this->limits[
'map'])) {
173 putenv(
"MAGICK_MAP_LIMIT=" . $this->limits[
'map']);
174 putenv(
"MAGICK_LIMIT_MAP=" . $this->limits[
'map']);
186 return in_array($ext, [
'jpg',
'jpeg',
'png',
'gif',
'webp']);
196 return in_array($ext, [
'png']);
197 return in_array($ext, [
'jpg',
'jpeg',
'png',
'gif',
'webp']);
225 public function addCrop($width, $height, $x = 0, $y = 0)
227 $this->queue[] = array(
'crop', func_get_args());
242 $this->queue[] = array(
'resize', func_get_args());
257 $this->queue[] = array(
'thumb', func_get_args());
272 public function addThumbfill($width, $height, $centerX = 50, $centerY = 50)
274 $this->queue[] = array(
'thumbfill', func_get_args());
289 return (is_numeric($number)) ? intval($number) :
null;
303 if (!is_numeric($width) && !is_numeric($height)) {
306 } elseif (!is_numeric($height)) {
307 $height = round(($this->size[1] / $this->size[0]) * $width);
308 } elseif (!is_numeric($width)) {
309 $width = round(($this->size[0] / $this->size[1]) * $height);
312 return [$width, $height];
324 foreach ($this->queue as $task) {
326 $arguments = array_map(array($this,
'escapeNumber'), $task[1]);
328 call_user_func_array(array($this, $action), $arguments);
342 if (!file_exists(
$input)) {
350 $this->size = $this->getImageSize();
351 $this->otherRender =
false;
355 $this->oldIgnoreUserAbort = ignore_user_abort();
356 ignore_user_abort(
true);
365 ignore_user_abort($this->oldIgnoreUserAbort);
377 if (!file_exists($filename)) {
382 $success = $optimizer->optimize($filename);
394 $this->outputLockFp = fopen($this->output .
".lock",
'w');
395 $locked = flock($this->outputLockFp, LOCK_EX | LOCK_NB, $wouldblock);
397 if (!$locked && $wouldblock) {
398 $this->otherRender =
true;
399 flock($this->outputLockFp, LOCK_EX);
410 if (isset($this->outputLockFp)) {
411 flock($this->outputLockFp, LOCK_UN);
412 unlink($this->output .
".lock");
414 $this->outputLockFp =
null;
426 $parts = explode(
'.', $fileName);
427 $extension = strtolower(end($parts));
429 if ($extension ==
'jpeg') {
433 && $extension !=
'png'
434 && $extension !=
'gif'
436 if (is_callable(
'getimagesize') && file_exists($fileName)) {
437 $info = getimagesize($fileName);
438 if (isset($info[2])) {
463 public static function which($binary)
465 exec(
'which ' . $binary, $commandOutput, $returnStatus);
466 if ($returnStatus === 0) {
467 return $commandOutput[0];
490 if ($this->outputFormat ==
'jpg') {
492 is_numeric($this->quality)
493 && $this->quality >= 0
494 && $this->quality <= 100
500 } elseif ($this->outputFormat ==
"webp") {
502 is_numeric($this->quality)
503 && $this->quality >= 0
504 && $this->quality <= 100
510 } elseif ($this->outputFormat ==
'png') {
512 is_numeric($this->quality)
513 && $this->quality >= 0
514 && $this->quality <= 95
515 && $this->quality % 10 <= 5
517 $quality = sprintf(
"%02d", $this->quality);
535 if ($this->inputFormat ==
"pdf") {
551 protected function bypassTest($width, $height, $x = 0, $y = 0)
554 ($width !==
null && $width < 1)
555 || ($height !==
null && $height < 1)
556 || ($width ==
null && $height ==
null)
564 $width == $this->size[0]
565 && $height == $this->size[1]
581 if ($this->input != $this->output) {
582 copy($this->input, $this->output);
general graphics related exception class
Input file not found exception.
$background
Image background string.
$quality
Image quality string.
canWrite($ext)
Checks if extension supports writing file type.
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
getPageNumber()
getPageNumber
canRead($ext)
Checks if extension support reading file type.
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.