mirror of
https://github.com/Intervention/image.git
synced 2025-08-28 08:09:54 +02:00
ResizeModifier & FitModifier relation
This commit is contained in:
@@ -171,7 +171,7 @@ abstract class AbstractImage
|
||||
$size = new Size($width, $height);
|
||||
|
||||
return $this->modify(
|
||||
$this->resolveDriverClass('Modifiers\CropResizeModifier', $size, $position)
|
||||
$this->resolveDriverClass('Modifiers\FitModifier', $size, $position)
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -22,7 +22,7 @@ use Intervention\Image\Traits\CanResizeGeometrically;
|
||||
|
||||
*/
|
||||
|
||||
class CropResizeModifier implements ModifierInterface
|
||||
class FitModifier extends ResizeModifier implements ModifierInterface
|
||||
{
|
||||
use CanResizeGeometrically;
|
||||
|
||||
@@ -67,62 +67,4 @@ class CropResizeModifier implements ModifierInterface
|
||||
->toHeight($this->target->getHeight())
|
||||
->scale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper function for 'imagecopyresampled'
|
||||
*
|
||||
* @param FrameInterface $frame
|
||||
* @param int $dst_x
|
||||
* @param int $dst_y
|
||||
* @param int $src_x
|
||||
* @param int $src_y
|
||||
* @param int $dst_w
|
||||
* @param int $dst_h
|
||||
* @param int $src_w
|
||||
* @param int $src_h
|
||||
* @return void
|
||||
*/
|
||||
protected function modify(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
|
||||
{
|
||||
// create new image
|
||||
$modified = imagecreatetruecolor(
|
||||
$resize->getWidth(),
|
||||
$resize->getHeight()
|
||||
);
|
||||
|
||||
// get current image
|
||||
$gd = $frame->getCore();
|
||||
|
||||
// preserve transparency
|
||||
$transIndex = imagecolortransparent($gd);
|
||||
|
||||
if ($transIndex != -1) {
|
||||
$rgba = imagecolorsforindex($modified, $transIndex);
|
||||
$transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127);
|
||||
imagefill($modified, 0, 0, $transColor);
|
||||
imagecolortransparent($modified, $transColor);
|
||||
} else {
|
||||
imagealphablending($modified, false);
|
||||
imagesavealpha($modified, true);
|
||||
}
|
||||
|
||||
// copy content from resource
|
||||
$result = imagecopyresampled(
|
||||
$modified,
|
||||
$gd,
|
||||
$resize->getPivot()->getX(),
|
||||
$resize->getPivot()->getY(),
|
||||
$crop->getPivot()->getX(),
|
||||
$crop->getPivot()->getY(),
|
||||
$resize->getWidth(),
|
||||
$resize->getHeight(),
|
||||
$crop->getWidth(),
|
||||
$crop->getHeight()
|
||||
);
|
||||
|
||||
imagedestroy($gd);
|
||||
|
||||
// set new content as recource
|
||||
$frame->setCore($modified);
|
||||
}
|
||||
}
|
@@ -9,11 +9,6 @@ use Intervention\Image\Interfaces\SizeInterface;
|
||||
|
||||
class ResizeModifier implements ModifierInterface
|
||||
{
|
||||
/**
|
||||
* Target size
|
||||
*
|
||||
* @var SizeInterface
|
||||
*/
|
||||
protected $target;
|
||||
|
||||
public function __construct(SizeInterface $target)
|
||||
@@ -24,41 +19,19 @@ class ResizeModifier implements ModifierInterface
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
foreach ($image as $frame) {
|
||||
$framesize = $frame->getSize();
|
||||
$this->modify(
|
||||
$frame,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
$this->target->getWidth(),
|
||||
$this->target->getHeight(),
|
||||
$framesize->getWidth(),
|
||||
$framesize->getHeight()
|
||||
);
|
||||
$this->modify($frame, $frame->getSize(), $this->target);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper function for 'imagecopyresampled'
|
||||
*
|
||||
* @param Image $image
|
||||
* @param int $dst_x
|
||||
* @param int $dst_y
|
||||
* @param int $src_x
|
||||
* @param int $src_y
|
||||
* @param int $dst_w
|
||||
* @param int $dst_h
|
||||
* @param int $src_w
|
||||
* @param int $src_h
|
||||
* @return boolean
|
||||
*/
|
||||
protected function modify(FrameInterface $frame, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
|
||||
protected function modify(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
|
||||
{
|
||||
// create new image
|
||||
$modified = imagecreatetruecolor($dst_w, $dst_h);
|
||||
$modified = imagecreatetruecolor(
|
||||
$resize->getWidth(),
|
||||
$resize->getHeight()
|
||||
);
|
||||
|
||||
// get current image
|
||||
$gd = $frame->getCore();
|
||||
@@ -80,14 +53,14 @@ class ResizeModifier implements ModifierInterface
|
||||
$result = imagecopyresampled(
|
||||
$modified,
|
||||
$gd,
|
||||
$dst_x,
|
||||
$dst_y,
|
||||
$src_x,
|
||||
$src_y,
|
||||
$dst_w,
|
||||
$dst_h,
|
||||
$src_w,
|
||||
$src_h
|
||||
$resize->getPivot()->getX(),
|
||||
$resize->getPivot()->getY(),
|
||||
$crop->getPivot()->getX(),
|
||||
$crop->getPivot()->getY(),
|
||||
$resize->getWidth(),
|
||||
$resize->getHeight(),
|
||||
$crop->getWidth(),
|
||||
$crop->getHeight()
|
||||
);
|
||||
|
||||
imagedestroy($gd);
|
||||
|
Reference in New Issue
Block a user