mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 16:50:07 +02:00
ResizeModifier & FitModifier relation
This commit is contained in:
@@ -171,7 +171,7 @@ abstract class AbstractImage
|
|||||||
$size = new Size($width, $height);
|
$size = new Size($width, $height);
|
||||||
|
|
||||||
return $this->modify(
|
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;
|
use CanResizeGeometrically;
|
||||||
|
|
||||||
@@ -67,62 +67,4 @@ class CropResizeModifier implements ModifierInterface
|
|||||||
->toHeight($this->target->getHeight())
|
->toHeight($this->target->getHeight())
|
||||||
->scale();
|
->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
|
class ResizeModifier implements ModifierInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Target size
|
|
||||||
*
|
|
||||||
* @var SizeInterface
|
|
||||||
*/
|
|
||||||
protected $target;
|
protected $target;
|
||||||
|
|
||||||
public function __construct(SizeInterface $target)
|
public function __construct(SizeInterface $target)
|
||||||
@@ -24,41 +19,19 @@ class ResizeModifier implements ModifierInterface
|
|||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
foreach ($image as $frame) {
|
foreach ($image as $frame) {
|
||||||
$framesize = $frame->getSize();
|
$this->modify($frame, $frame->getSize(), $this->target);
|
||||||
$this->modify(
|
|
||||||
$frame,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
$this->target->getWidth(),
|
|
||||||
$this->target->getHeight(),
|
|
||||||
$framesize->getWidth(),
|
|
||||||
$framesize->getHeight()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function modify(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
|
||||||
* 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)
|
|
||||||
{
|
{
|
||||||
// create new image
|
// create new image
|
||||||
$modified = imagecreatetruecolor($dst_w, $dst_h);
|
$modified = imagecreatetruecolor(
|
||||||
|
$resize->getWidth(),
|
||||||
|
$resize->getHeight()
|
||||||
|
);
|
||||||
|
|
||||||
// get current image
|
// get current image
|
||||||
$gd = $frame->getCore();
|
$gd = $frame->getCore();
|
||||||
@@ -80,14 +53,14 @@ class ResizeModifier implements ModifierInterface
|
|||||||
$result = imagecopyresampled(
|
$result = imagecopyresampled(
|
||||||
$modified,
|
$modified,
|
||||||
$gd,
|
$gd,
|
||||||
$dst_x,
|
$resize->getPivot()->getX(),
|
||||||
$dst_y,
|
$resize->getPivot()->getY(),
|
||||||
$src_x,
|
$crop->getPivot()->getX(),
|
||||||
$src_y,
|
$crop->getPivot()->getY(),
|
||||||
$dst_w,
|
$resize->getWidth(),
|
||||||
$dst_h,
|
$resize->getHeight(),
|
||||||
$src_w,
|
$crop->getWidth(),
|
||||||
$src_h
|
$crop->getHeight()
|
||||||
);
|
);
|
||||||
|
|
||||||
imagedestroy($gd);
|
imagedestroy($gd);
|
||||||
|
Reference in New Issue
Block a user