Graphicsmagick.php
Go to the documentation of this file.
1<?php
11
19{
31 protected function crop($width, $height, $x = 0, $y = 0)
32 {
33 if (!$this->bypassTest($width, $height, $x, $y)) {
34 // '+' for positive offset (the '-' is already there)
35 $x = ($x < 0) ? $x : '+' . $x;
36 $y = ($y < 0) ? $y : '+' . $y;
37
38 $xExtent = ($x > 0) ? "+0" : $x;
39 $yExtent = ($y > 0) ? "+0" : $y;
40 $this->command .= " -gravity NorthWest -crop {$width}x{$height}{$x}{$y}! -gravity NorthWest -extent {$width}x{$height}{$xExtent}{$yExtent}";
41 $this->size = array($width, $height);
42 }
43 }
44
50 protected function getImageSize()
51 {
52 $imageSize = false;
53 if (is_callable('getimagesize')) {
54 $imageSize = getimagesize($this->input);
55 }
56 if (!$imageSize) {
57 $pageNumber = $this->getPageNumber();
58 exec("{$this->executable} identify -ping -format \"%wx%h\" " . escapeshellarg($this->input) . $pageNumber . ' 2>&1', $commandOutput, $returnStatus);
59 if ($returnStatus === 0) {
60 $imageSize = explode('x', $commandOutput[0]);
61 } else {
62 $this->unlock();
63
64 throw new \Depage\Graphics\Exceptions\Exception(implode("\n", $commandOutput));
65 }
66 }
67
68 return $imageSize;
69 }
70
80 public function render($input, $output = null)
81 {
83
84 $pageNumber = $this->getPageNumber();
85
86 $this->command = $this->executable . " convert " . escapeshellarg($this->input) . "{$pageNumber} -background none";
87 $this->processQueue();
88
89 if ($this->otherRender && file_exists($this->output)) {
90 // do nothing file is already generated
91 } else if (
92 $this->bypass
93 && $this->inputFormat == $this->outputFormat
94 ) {
95 $this->bypass();
96 } else {
97 $quality = $this->getQuality();
98 $optimize = $this->getOptimize();
99
100 if ($this->background === 'checkerboard') {
101 $tempFile = tempnam(sys_get_temp_dir(), 'depage-graphics-');
102 $this->command .= " miff:{$tempFile}";
103
104 $this->execCommand();
105
106 $canvasSize = $this->size[0] . "x" . $this->size[1];
107
108 $this->command = $this->executable . " convert";
109 $this->command .= " -page {$canvasSize} -size {$canvasSize} pattern:checkerboard";
110 $this->command .= " -page {$canvasSize} miff:{$tempFile} -colorspace rgb -flatten {$quality}{$optimize} +page {$this->outputFormat}:" . escapeshellarg($this->output);
111
112 $this->execCommand();
113 unlink($tempFile);
114 } else {
115 $background = $this->getBackground();
116 $this->command .= "{$background} -colorspace rgb {$quality}{$optimize} +page {$this->outputFormat}:" . escapeshellarg($this->output);
117
118 $this->execCommand();
119
120 if ($this->optimize) {
121 $this->optimizeImage($this->output);
122 }
123 }
124 }
125
126 parent::renderFinished();
127 }
128
134 protected function getBackground()
135 {
136 if ($this->background[0] === '#') {
137 $background = " -flatten -background {$this->background}";
138 } elseif ($this->outputFormat == 'jpg') {
139 $background = " -flatten -background #FFF";
140 } else {
141 $background = '';
142 }
143
144 return $background;
145 }
146}
147
148/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
$background
Image background string.
Definition: Graphics.php:73
$quality
Image quality string.
Definition: Graphics.php:77
processQueue()
Process action queue.
Definition: Graphics.php:299
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
Definition: Graphics.php:512
$input
Input filename.
Definition: Graphics.php:45
$output
Output filename.
Definition: Graphics.php:49
render($input, $output=null)
Main method for image handling.
Definition: Graphics.php:317
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
Definition: Graphics.php:350
bypass()
Runs bypass (copies file)
Definition: Graphics.php:540
$optimize
Optimize output images.
Definition: Graphics.php:81
getBackground()
Generates background command.
getImageSize()
Determine size of input image.
render($input, $output=null)
Main method for image handling.
crop($width, $height, $x=0, $y=0)
Crop action.
getQuality()
Generates quality command.
getOptimize()
Generates optimization parameters.
execCommand()
Executes ImageMagick command.