Skip to content

Commit 71e3f01

Browse files
committed
feat: Add crop image
1 parent 044ed77 commit 71e3f01

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

Model/Resize.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @package Web200\ImageResize\Model
2323
* @author Web200 <[email protected]>
24-
* @copyright 2019 Web200
24+
* @copyright 2024 Web200
2525
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
2626
* @link https://www.web200.fr/
2727
*/
@@ -114,6 +114,7 @@ class Resize
114114
* - backgroundColor[null]: Default white
115115
*/
116116
protected $defaultSettings = [
117+
'crop' => true,
117118
'constrainOnly' => true,
118119
'keepAspectRatio' => true,
119120
'keepTransparency' => true,
@@ -127,6 +128,7 @@ class Resize
127128
* @var string[] $subPathSettingsMapping
128129
*/
129130
protected $subPathSettingsMapping = [
131+
'crop' => 'cr',
130132
'constrainOnly' => 'co',
131133
'keepAspectRatio' => 'ar',
132134
'keepTransparency' => 'tr',
@@ -289,6 +291,7 @@ protected function initResizeSettings(array $resizeSettings): void
289291
{
290292
// Init resize settings with default
291293
$this->resizeSettings = [
294+
'crop' => false,
292295
'constrainOnly' => $this->config->getDefaultConstrainOnly(),
293296
'keepAspectRatio' => $this->config->getDefaultKeepAspectRatio(),
294297
'keepTransparency' => $this->config->getDefaultKeepTransparency(),
@@ -467,7 +470,13 @@ protected function resizeAndSaveImage(): bool
467470
$imageAdapter->keepFrame($this->resizeSettings['keepFrame']);
468471
$imageAdapter->backgroundColor($this->resizeSettings['backgroundColor']);
469472
$imageAdapter->quality($this->resizeSettings['quality']);
470-
$imageAdapter->resize($this->width, $this->height);
473+
474+
if ($this->resizeSettings['crop'] === true) {
475+
$imageAdapter->resizeToSquare($this->width, $this->height);
476+
} else {
477+
$imageAdapter->resize($this->width, $this->height);
478+
}
479+
471480
$imageAdapter->save($this->getAbsolutePathResized());
472481

473482
return true;

Preference/Gd2.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Web200\ImageResize\Preference;
6+
7+
/**
8+
* Class Gd2
9+
*
10+
* @package Web200\Preference
11+
*/
12+
class Gd2 extends \Magento\Framework\Image\Adapter\Gd2
13+
{
14+
/**
15+
* @param $new_width
16+
* @param $new_height
17+
*
18+
* @return void
19+
*/
20+
public function resizeToSquare($new_width, $new_height)
21+
{
22+
$origWidth = $this->_imageSrcWidth;
23+
$origHeight = $this->_imageSrcHeight;
24+
25+
$newRatio = $new_width / $new_height;
26+
$origRatio = $origWidth / $origHeight;
27+
28+
if ($origRatio > $newRatio) {
29+
$tempWidth = round($origRatio * $new_height);
30+
31+
$this->resize($tempWidth, $new_height);
32+
33+
$cropAmount = floor(($tempWidth - $new_width) / 2);
34+
$cropRemainder = ($tempWidth - $new_width) % 2;
35+
36+
$this->crop(0, $cropAmount + $cropRemainder, $cropAmount, 0);
37+
} else {
38+
$tempHeight = round((1 / $origRatio) * $new_width);
39+
40+
$this->resize($new_width, $tempHeight);
41+
42+
$cropAmount = floor(($tempHeight - $new_height) / 2);
43+
$cropRemainder = ($tempHeight - $new_height) % 2;
44+
45+
$this->crop($cropAmount + $cropRemainder, 0, 0, $cropAmount);
46+
}
47+
}
48+
}

etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@
4545
<plugin name="web200_imageadd_default_placeholder_image"
4646
type="Web200\ImageResize\Plugin\AddDefaultPlaceholderImage"/>
4747
</type>
48+
49+
<!-- Add crop -->
50+
<preference for="Magento\Framework\Image\Adapter\Gd2" type="Web200\ImageResize\Preference\Gd2"/>
4851
</config>

0 commit comments

Comments
 (0)