42 parent::__construct($options);
44 $this->executable = isset($options[
'executable']) ? $options[
'executable'] :
null;
45 $this->timeout = isset($options[
'timeout']) ? $options[
'timeout'] : 0;
59 protected function crop($width, $height, $x = 0, $y = 0)
61 if (!$this->
bypassTest($width, $height, $x, $y)) {
63 $x = ($x < 0) ? $x :
'+' . $x;
64 $y = ($y < 0) ? $y :
'+' . $y;
66 $this->command .=
" -gravity NorthWest -crop {$width}x{$height}{$x}{$y}! -flatten";
67 $this->size = array($width, $height);
79 protected function resize($width, $height)
83 if (!$this->
bypassTest($newSize[0], $newSize[1])) {
86 $this->command .=
" $resizeAction {$newSize[0]}x{$newSize[1]}!";
88 $this->size = $newSize;
100 protected function thumb($width, $height)
105 $this->command .=
" -gravity Center $resizeAction {$width}x{$height} -extent {$width}x{$height}";
106 $this->size = array($width, $height);
120 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
124 $centerX = $centerX / -100 + 0.5;
125 $centerY = $centerY / -100 + 0.5;
127 if ($newSize[1] < $height) {
129 $x = round(($width - $newSize[0]) * $centerX);
133 $y = round(($height - $newSize[1]) * $centerY);
135 $x = ($x < 0) ? $x :
'+' . $x;
136 $y = ($y < 0) ? $y :
'+' . $y;
140 $this->command .=
" -gravity Center $resizeAction {$width}x{$height}^ -extent {$width}x{$height}{$x}{$y}";
141 $this->size = array($width, $height);
153 if (is_callable(
'getimagesize')) {
154 $imageSize = getimagesize($this->input);
158 $identify = preg_replace(
'/convert$/',
'identify', $this->executable);
159 $command =
"{$identify} -ping -format \"%wx%h\" " . escapeshellarg($this->input) . $pageNumber;
160 $escapedCommand = str_replace(
'!',
'\!', escapeshellcmd(
$command));
162 exec($escapedCommand .
' 2>&1', $commandOutput, $returnStatus);
163 if ($returnStatus === 0) {
164 $imageSize = explode(
'x', $commandOutput[0]);
168 throw new \Depage\Graphics\Exceptions\Exception(implode(
"\n", $commandOutput));
191 if ($this->otherRender && file_exists($this->output)) {
195 && $this->inputFormat == $this->outputFormat
204 $this->command =
"{$this->executable} {$background} ( " . escapeshellarg($this->input) .
"{$pageNumber}{$this->command}";
205 $this->command .=
" ) -colorspace sRGB -flatten {$quality}{$optimize}";
207 $this->command .=
" {$this->outputFormat}:" . escapeshellarg($this->output);
211 if ($this->optimize) {
216 parent::renderFinished();
228 $command = str_replace(
'!',
'\!', escapeshellcmd($this->command));
230 $descriptorspec = array(
231 0 => array(
"pipe",
"r"),
232 1 => array(
"pipe",
"w"),
233 2 => array(
"pipe",
"w")
235 $process = proc_open(
"exec " .
$command, $descriptorspec, $pipes);
238 stream_set_blocking($pipes[1],
false);
239 stream_set_blocking($pipes[2],
false);
242 $output = array(1 =>
"", 2 =>
"");
245 if (is_resource($process)) {
247 while(!feof($pipes[1]) && !feof($pipes[2])) {
248 for ($i = 1; $i < 3; $i++) {
249 $s = fgets($pipes[$i]);
254 if ($this->timeout > 0 && time() - $startTime > $this->timeout) {
256 proc_terminate($process);
261 for ($i = 0; $i < 3; $i++) {
264 $returnStatus = proc_close($process);
270 throw new \Depage\Graphics\Exceptions\Exception(
"Conversion over timeout");
271 }
else if ($returnStatus != 0) {
274 throw new \Depage\Graphics\Exceptions\Exception(
$output[2]);
285 $background =
"-size {$this->size[0]}x{$this->size[1]}";
287 if ($this->background[0] ===
'#') {
289 } elseif ($this->background ==
'checkerboard') {
290 $background .=
" -background none pattern:checkerboard";
292 if ($this->outputFormat ==
'jpg') {
309 $this->outputFormat ==
'jpg'
310 || $this->outputFormat ==
'png'
311 || $this->outputFormat ==
'webp'
313 return '-quality ' . parent::getQuality();
327 if ($this->outputFormat ==
'jpg') {
328 $param .=
" -interlace Plane";
329 }
else if ($this->outputFormat ==
'png') {
330 $param .=
" -define png:format=png00";
331 }
else if ($this->outputFormat ==
"webp" && $this->inputFormat ==
"png") {
332 $param .=
" -define webp:lossless=true -define webp:image-hint=graph";
347 if ($width <= 160 && $height <= 160) {
361 if ($this->inputFormat ==
"pdf") {
$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.
dimensions($width, $height)
Scales image dimensions.
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
bypass()
Runs bypass (copies file)
$optimize
Optimize output images.
getBackground()
Generates background command.
thumbfill($width, $height, $centerX=50, $centerY=50)
Thumb action.
getQuality()
Generates quality command.
$executable
Imagemagick executable path.
getPageNumber()
getPageNumber
$timeout
timeout after which the image conversion will be canceled
getImageSize()
Determine size of input image.
render($input, $output=null)
Main method for image handling.
$command
Imagemagick command string.
getResizeAction($width, $height)
Gets the resize action depending on target size.
resize($width, $height)
Resize action.
__construct($options=array())
graphics_graphicsmagick class constructor
crop($width, $height, $x=0, $y=0)
Crop action.
thumb($width, $height)
Thumb action.
getOptimize()
Generates optimization parameters.
execCommand()
Executes ImageMagick command.