Php 裁剪和调整大小理论

Php 裁剪和调整大小理论,php,image,laravel,theory,intervention,Php,Image,Laravel,Theory,Intervention,我需要一些关于在焦点周围使用边界框裁剪和调整图像大小的理论指导 在我的情况下,在不同的定义(1x、2x、3x等)下,我有各种不同的图像大小要求(例如100x100、500x200、1200x50) 这些定义有效地将50x50裁剪图像转换为100x100裁剪图像,从而为更高屏幕清晰度的设备提供2倍的清晰度 我得到了一张用户上传的图像,其中有一个x,y焦点,以及一个带有两个x,y坐标(左上[x,y],右下[x,y])的边界框 将用户提供的图像转换成不同大小和分辨率的各种图像的原理是什么?研究让我找到

我需要一些关于在焦点周围使用边界框裁剪和调整图像大小的理论指导

在我的情况下,在不同的定义(1x、2x、3x等)下,我有各种不同的图像大小要求(例如100x100、500x200、1200x50)

这些定义有效地将50x50裁剪图像转换为100x100裁剪图像,从而为更高屏幕清晰度的设备提供2倍的清晰度

我得到了一张用户上传的图像,其中有一个x,y焦点,以及一个带有两个x,y坐标(左上[x,y],右下[x,y])的边界框

将用户提供的图像转换成不同大小和分辨率的各种图像的原理是什么?研究让我找到了其中一个,但不是所有的需求都在一起


在我的特定环境中,我使用的是PHP、Laravel和图像干预库,尽管由于这个问题的性质,这有点不相关。

我以前写过一个图像类,它使用GD库

我没有编写基于焦点调整大小和裁剪的代码,但我有一个
resizeAndCrop()
方法,该方法在焦点位于中心的前提下工作:

public function resizeAndCrop($width,$height)
    {
        $target_ratio = $width / $height;
        $actual_ratio = $this->getWidth() / $this->getHeight();

        if($target_ratio == $actual_ratio){
            // Scale to size
            $this->resize($width,$height);

        } elseif($target_ratio > $actual_ratio) {
            // Resize to width, crop extra height
            $this->resizeToWidth($width);
            $this->crop($width,$height,true);

        } else {
            // Resize to height, crop additional width
            $this->resizeToHeight($height);
            $this->crop($width,$height,true);
        }
    }
以下是
crop()
方法,您可以将焦点设置为左、中或右:

/**
 * @param $width
 * @param $height
 * @param string $trim
 */
public function crop($width,$height, $trim = 'center')
{
    $offset_x = 0;
    $offset_y = 0;
    $current_width = $this->getWidth();
    $current_height = $this->getHeight();
    if($trim != 'left')
    {
        if($current_width > $width) {
            $diff = $current_width - $width;
            $offset_x = ($trim == 'center') ? $diff / 2 : $diff; //full diff for trim right
        }
        if($current_height > $height) {
            $diff = $current_height - $height;
            $offset_y = ($trim = 'center') ? $diff / 2 : $diff;
        }
    }
    $new_image = imagecreatetruecolor($width,$height);
    imagecopyresampled($new_image,$this->_image,0,0,$offset_x,$offset_y,$width,$height,$width,$height);
    $this->_image = $new_image;
}
我不会费心解释
imagecopyresampled()
,因为您只是在寻找裁剪背后的理论,但文档在这里

请记住,使用PHP的GD库调整图像大小需要大量内存,具体取决于图像的大小。我喜欢使用
imagemagick
,PHP有一个名为
Imagick
的包装类,如果遇到麻烦,值得一看

我希望这对你有帮助,祝你好运