19 require_once(BASE.
"wcmf/3rdparty/PHPImageWorkshop/ImageWorkshop.php");
20 require_once(BASE.
"wcmf/3rdparty/PHPImageWorkshop/GifFrameExtractor.php");
21 require_once(BASE.
"wcmf/3rdparty/PHPImageWorkshop/GifCreator.php");
51 ImageWorkshop::initFromPath($imgname);
53 }
catch (Exception $ex) {
70 return ($widthOk && $heightOk);
84 $image = ImageWorkshop::initFromPath($imgname);
85 }
catch (Exception $ex) {
88 $imgWitdh = $image->getWidth();
89 $dimOk = ($exact && $imgWitdh == $width) || (!$exact && $imgWitdh <= $width);
92 $this->_errorMsg .=
Message::get(
"Wrong image width. Image width must be %1% %2%px - actual image width is %3%px.",
93 array($constraint, $width, $imgWitdh));
94 $this->_errorMsg .=
"\n";
110 $image = ImageWorkshop::initFromPath($imgname);
111 }
catch (Exception $ex) {
114 $imgHeight = $image->getHeight();
115 $dimOk = ($exact && $imgHeight == $height) || (!$exact && $imgHeight <= $height);
118 $this->_errorMsg .=
Message::get(
"Wrong image width. Image height must be %1% %2%px - actual image height is %3%px.",
119 array($constraint, $height, $imgHeight));
120 $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);
151 }
catch (Exception $ex) {
152 $this->_errorMsg = $ex->getMessage();
170 $keepAspect = $width === null || $height === null;
171 self::processImageFunction($srcName, $destName,
"resizeInPixel", array($width, $height, $keepAspect));
173 }
catch (Exception $ex) {
174 $this->_errorMsg = $ex->getMessage();
191 function cropImage($srcName, $destName, $width, $height, $x=null, $y=null)
195 $image = ImageWorkshop::initFromPath($srcName);
196 list($width, $height) = self::calculateSizeParams($width, $height, $image->getWidth(), $image->getHeight());
197 $x = ($x === null) ? $image->getWidth()/2 : $x;
198 $y = ($y === null) ? $image->getHeight()/2 : $y;
200 self::processImageFunction($srcName, $destName,
"cropInPixel", array($width, $height, $x, $y,
'LT'));
202 }
catch (Exception $ex) {
203 $this->_errorMsg = $ex->getMessage();
216 self::processImageFunction($srcName, $destName,
"applyFilter", array(IMG_FILTER_GRAYSCALE));
218 }
catch (Exception $ex) {
219 $this->_errorMsg = $ex->getMessage();
233 if (GifFrameExtractor::isAnimatedGif($srcName)) {
235 $gfe =
new GifFrameExtractor();
236 $frames = $gfe->extract($srcName);
237 $retouchedFrames = array();
238 foreach ($frames as $frame) {
239 $frameLayer = ImageWorkshop::initFromResourceVar($frame[
'image']);
240 call_user_func_array(array($frameLayer, $function), $params);
241 $retouchedFrames[] = $frameLayer->getResult();
243 $gc =
new GifCreator();
244 $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0);
245 file_put_contents($destName, $gc->getGif());
249 $image = ImageWorkshop::initFromPath($srcName);
250 call_user_func_array(array($image, $function), $params);
251 $image->save(dirname($destName), basename($destName),
true, null, 100);
253 chmod($destName, 0644);
275 function renderTextTTF($text, $fontfile, $fontsize, $color, $bgcolor, $filename,
276 $width=null, $height=null, $x=null, $y=null, $angle=0)
281 $lineheight = $height;
284 if ($width != null) {
286 $text = str_replace(
"\r",
'', $text);
288 $srcLines = split (
"\n", $text);
289 foreach ($srcLines as $currentL) {
292 $wordsTmp = split(
" ", $currentL);
295 foreach ($wordsTmp as $word) {
296 $wordParts = split(
' ', str_replace(array(
'-',
'/'), array(
'- ',
'/ '), $word));
297 foreach ($wordParts as $wordPart) {
298 $words[] = $wordPart;
301 for ($i=0, $count=
sizeof($words); $i<$count; $i++) {
305 list($linewidth, $lineheight) = self::getTextDimension($fontsize, $angle, $fontfile, $text);
308 if ($linewidth > $width && !empty($line)) {
310 $dstLines[] = trim($line);
315 if (preg_match(
'/-$/', $word) || $i==
sizeof($words)-1) {
323 $dstLines[] = trim($line);
327 if ($height != null) {
328 $lineheight = $height;
330 $textheight =
sizeof($dstLines)*$lineheight;
331 $height = $textheight;
336 list($textwidth, $textheight) = self::getTextDimension($fontsize, $angle, $fontfile, $text);
343 list($width, $height, $x, $y) = self::calculateTextParams($width, $height, $x, $y, $textwidth, $textheight);
347 $image = ImageWorkshop::initVirginLayer($width, $height, $bgcolor);
350 foreach ($dstLines as $nr => $line) {
352 list($width, $height, $x, $y) = self::calculateTextParams($width, $height, $x, $y, $textwidth, $textheight);
354 $image->write(
"$line", $fontfile, $fontsize, $color, $x, $y, $angle);
359 $image->save(dirname($filename), basename($filename),
true, null, 100);
360 chmod($filename, 0644);
362 }
catch (Exception $ex) {
363 $this->_errorMsg = $ex->getMessage();
378 list($x2, $y2, $x3, $y3, $x1, $y1, $x0, $y0) = imagettfbbox($fontsize, $angle, $fontfile, $text);
379 return array($x1-$x2, $y2-$y1);
395 if ($width === null) $width = $textwidth;
396 if ($height === null) $height = $textheight;
399 if ($x === null) $x = ($width-$textwidth)/2;
400 if ($y === null) $y = $height;
402 return array($width, $height, $x, $y);
416 if ($width == null) {
417 $width = $imageWidth/$imageHeight*$height;
419 elseif ($height == null) {
420 $height = $imageHeight/$imageWidth*$width;
422 return array(intval($width), intval($height));
isValidImageHeight($imgname, $height, $exact=true)
GraphicsUtil provides support for graphic manipulation.
getTextDimension($fontsize, $angle, $fontfile, $text)
fitIntoSquare($srcName, $maxDimension)
get($message, $parameters=null, $domain='', $lang='')
createBlackWhiteImage($srcName, $destName)
createThumbnail($srcName, $destName, $width, $height)
isValidImageDimension($imgname, $width, $height, $exact=true)
calculateSizeParams($width, $height, $imageWidth, $imageHeight)
calculateTextParams($width, $height, $x, $y, $textwidth, $textheight)
isValidImageWidth($imgname, $width, $exact=true)
cropImage($srcName, $destName, $width, $height, $x=null, $y=null)
renderTextTTF($text, $fontfile, $fontsize, $color, $bgcolor, $filename, $width=null, $height=null, $x=null, $y=null, $angle=0)
processImageFunction($srcName, $destName, $function, $params)