17 if (!class_exists(
'PHPImageWorkshop\ImageWorkshop') ||
18 !class_exists(
'GifFrameExtractor\GifFrameExtractor') ||
19 !class_exists(
'GifCreator\GifCreator')) {
20 throw new \wcmf\lib\config\ConfigurationException(
21 'wcmf\lib\util\GraphicsUtil requires '.
22 'ImageWorkshop, GifFrameExtractor and GifCreator. If you are using composer, '.
23 'add sybio/image-workshop, sybio/gif-frame-extractor and sybio/gif-creator '.
24 'as dependency to your project');
36 private $_errorMsg =
'';
43 return $this->_errorMsg;
53 ImageWorkshop::initFromPath($imgname);
71 return ($widthOk && $heightOk);
84 $image = ImageWorkshop::initFromPath($imgname);
88 $imgWitdh = $image->getWidth();
89 $dimOk = ($exact && $imgWitdh == $width) || (!$exact && $imgWitdh <= $width);
92 $constraint = $exact ? $message->getText(
"exactly") : $message->getText(
"smaller than");
93 $this->_errorMsg = $message->getText(
"Wrong image width. Image width must be %1% %2%px - actual image width is %3%px.",
94 array($constraint, $width, $imgWitdh));
95 $this->_errorMsg .=
"\n";
110 $image = ImageWorkshop::initFromPath($imgname);
114 $imgHeight = $image->getHeight();
115 $dimOk = ($exact && $imgHeight == $height) || (!$exact && $imgHeight <= $height);
118 $constraint = $exact ? $message->getText(
"exactly") : $message->getText(
"smaller than");
119 $this->_errorMsg .= $message->getText(
"Wrong image height. Image height must be %1% %2%px - actual image height is %3%px.",
120 array($constraint, $height, $imgHeight));
121 $this->_errorMsg .=
"\n";
134 $image = ImageWorkshop::initFromPath($srcName);
135 $sourceWidth = $image->getWidth();
136 $sourceHeight = $image->getHeight();
137 if ($sourceWidth < $sourceHeight) {
138 $height = $maxDimension;
139 $width = floor($sourceWidth*$height/$sourceHeight);
142 $width = $maxDimension;
143 $height = floor($sourceHeight*$width/$sourceWidth);
146 if ($width > $sourceWidth && $height > $sourceHeigth) {
147 $width = $sourceWidth;
148 $height = $sourceHeight;
150 return array($width, $height);
152 $this->_errorMsg = $ex->getMessage();
169 $keepAspect = $width === null || $height === null;
170 $this->
processImageFunction($srcName, $destName,
"resizeInPixel", array($width, $height, $keepAspect));
173 $this->_errorMsg = $ex->getMessage();
190 public function cropImage($srcName, $destName, $width, $height, $x=null, $y=null) {
193 $image = ImageWorkshop::initFromPath($srcName);
194 list($width, $height) = $this->calculateSizeParams($width, $height, $image->getWidth(), $image->getHeight());
195 $x = ($x === null) ? $image->getWidth()/2 : $x;
196 $y = ($y === null) ? $image->getHeight()/2 : $y;
198 $this->
processImageFunction($srcName, $destName,
"cropInPixel", array($width, $height, $x, $y,
'LT'));
201 $this->_errorMsg = $ex->getMessage();
216 $this->_errorMsg = $ex->getMessage();
230 if (GifFrameExtractor::isAnimatedGif($srcName)) {
232 $gfe =
new GifFrameExtractor();
233 $frames = $gfe->extract($srcName);
234 $retouchedFrames = array();
235 foreach ($frames as $frame) {
236 $frameLayer = ImageWorkshop::initFromResourceVar($frame[
'image']);
237 call_user_func_array(array($frameLayer, $function), $params);
238 $retouchedFrames[] = $frameLayer->getResult();
240 $gc =
new GifCreator();
241 $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0);
242 file_put_contents($destName, $gc->getGif());
246 $image = ImageWorkshop::initFromPath($srcName);
247 call_user_func_array(array($image, $function), $params);
248 $image->save(dirname($destName), basename($destName),
true, null, 100);
250 chmod($destName, 0644);
272 public function renderText($text, $fontfile, $fontsize, $color, $bgcolor, $filename,
273 $width=null, $height=null, $x=null, $y=null, $angle=0) {
277 $lineheight = $height;
280 if ($width != null) {
282 $text = str_replace(
"\r",
'', $text);
284 $srcLines = split (
"\n", $text);
285 foreach ($srcLines as $currentL) {
288 $wordsTmp = split(
" ", $currentL);
291 foreach ($wordsTmp as $word) {
292 $wordParts = split(
' ', str_replace(array(
'-',
'/'), array(
'- ',
'/ '), $word));
293 foreach ($wordParts as $wordPart) {
294 $words[] = $wordPart;
297 for ($i=0, $count=
sizeof($words); $i<$count; $i++) {
301 list($linewidth, $lineheight) = $this->getTextDimension($fontsize, $angle, $fontfile, $text);
304 if ($linewidth > $width && !empty($line)) {
306 $dstLines[] = trim($line);
311 if (preg_match(
'/-$/', $word) || $i==
sizeof($words)-1) {
319 $dstLines[] = trim($line);
323 if ($height != null) {
324 $lineheight = $height;
326 $textheight =
sizeof($dstLines)*$lineheight;
327 $height = $textheight;
332 list($textwidth, $textheight) = $this->getTextDimension($fontsize, $angle, $fontfile, $text);
339 list($width, $height, $x, $y) = $this->calculateTextParams($width, $height, $x, $y, $textwidth, $textheight);
343 $image = ImageWorkshop::initVirginLayer($width, $height, $bgcolor);
346 foreach ($dstLines as $nr => $line) {
348 list($width, $height, $x, $y) = $this->calculateTextParams($width, $height, $x, $y, $textwidth, $textheight);
350 $image->write(
"$line", $fontfile, $fontsize, $color, $x, $y, $angle);
355 $image->save(dirname($filename), basename($filename),
true, null, 100);
356 chmod($filename, 0644);
359 $this->_errorMsg = $ex->getMessage();
372 private function getTextDimension($fontsize, $angle, $fontfile, $text) {
373 list($x2, $y2, $x3, $y3, $x1, $y1, $x0, $y0) = imagettfbbox($fontsize, $angle, $fontfile, $text);
374 return array($x1-$x2, $y2-$y1);
387 private function calculateTextParams($width, $height, $x, $y, $textwidth, $textheight) {
389 if ($width === null) {
392 if ($height === null) {
393 $height = $textheight;
397 $x = ($width-$textwidth)/2;
402 return array($width, $height, $x, $y);
414 private function calculateSizeParams($width, $height, $imageWidth, $imageHeight) {
415 if ($width == null) {
416 $width = $imageWidth/$imageHeight*$height;
418 elseif ($height == null) {
419 $height = $imageHeight/$imageWidth*$width;
421 return array(intval($width), intval($height));
isValidImageHeight($imgname, $height, $exact=true)
Check image height.
createBlackWhiteImage($srcName, $destName)
Create a black and white copy of an image.
createThumbnail($srcName, $destName, $width, $height)
Create a thumbnail of an image file.
fitIntoSquare($srcName, $maxDimension)
Calculate image dimension to fit into a square, preserving the aspect ratio.
cropImage($srcName, $destName, $width, $height, $x=null, $y=null)
Crop an image to the given size starting from the middle of a given start point.
static getInstance($name, $dynamicConfiguration=array())
isImage($imgname)
Check if a given file is an image.
isValidImageDimension($imgname, $width, $height, $exact=true)
Check image dimensions.
getErrorMsg()
Get last error message.
GraphicsUtil provides support for graphic manipulation.
processImageFunction($srcName, $destName, $function, $params)
Process the given function on the given source image (supports animated gifs) and save the result in ...
isValidImageWidth($imgname, $width, $exact=true)
Check image width.
renderText($text, $fontfile, $fontsize, $color, $bgcolor, $filename, $width=null, $height=null, $x=null, $y=null, $angle=0)
Render a text to an image.