Skip to content

Commit c7bf390

Browse files
committed
add canvas crop
1 parent 3af4829 commit c7bf390

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/ImageStack/ImageManipulator/ThumbnailRule/PatternThumbnailRule.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ protected function _thumbnailImage(ImageWithImagineInterface $image, ImagePathIn
115115
$handlers = [
116116
[$this, 'handlerCrop'],
117117
[$this, 'handlerZoom'],
118+
[$this, 'handlerCropCanvas'],
118119
];
119120

120121
foreach ($formats as $format) {
@@ -188,6 +189,49 @@ protected function handlerZoom(ImageWithImagineInterface $image, $format)
188189
}
189190
return false;
190191
}
192+
193+
protected function handlerCropCanvas(ImageWithImagineInterface $image, $format)
194+
{
195+
$size = null;
196+
$width = $height = 0;
197+
$pp = false;
198+
if (preg_match('/^\=([0-9]+)([%]?)x([0-9]+)([%]?)$/', $format, $matches)) {
199+
$width = $matches[1];
200+
$height = $matches[3];
201+
if ($matches[2] == '%') {
202+
$width = $image->getImagineImage()->getSize()->getWidth() * $width / 100;
203+
}
204+
if ($matches[4] == '%') {
205+
$height = $image->getImagineImage()->getSize()->getHeight() * $height / 100;
206+
}
207+
} elseif (preg_match('/^\=([0-9]+)([%]?)$/', $format, $matches)) {
208+
$width = $height = $matches[1];
209+
if ($matches[2] == '%') {
210+
$width = $image->getImagineImage()->getSize()->getWidth() * $width / 100;
211+
$height = $image->getImagineImage()->getSize()->getHeight() * $height / 100;
212+
}
213+
}
214+
215+
if ($width > 0 && $height > 0) {
216+
$size = new \Imagine\Image\Box($width, $height);
217+
// print_r([
218+
// 'ori_width' => $image->getImagineImage()->getSize()->getWidth(),
219+
// 'ori_height' => $image->getImagineImage()->getSize()->getHeight(),
220+
// 'width' => $width,
221+
// 'height' => $height,
222+
// 'x' => ($image->getImagineImage()->getSize()->getWidth() - $size->getWidth()) / 2,
223+
// 'y' => ($image->getImagineImage()->getSize()->getHeight() - $size->getHeight()) / 2,
224+
// ]); die();
225+
$image->setImagineImage($image->getImagineImage()->crop(new \Imagine\Image\Point(
226+
($image->getImagineImage()->getSize()->getWidth() - $size->getWidth()) / 2,
227+
($image->getImagineImage()->getSize()->getHeight() - $size->getHeight()) / 2
228+
), $size));
229+
$image->deprecateBinaryContent();
230+
return true;
231+
}
232+
233+
return false;
234+
}
191235

192236
/**
193237
* @throws ImageException

0 commit comments

Comments
 (0)