【问题标题】:PHP image Resize and Crop FunctionPHP图像调整大小和裁剪功能
【发布时间】:2010-10-01 04:44:34
【问题描述】:

我想要一个功能,当我上传照片时,它应该裁剪图像,而不管图像从中心的比例如何,确保裁剪在图像内部。

上图是2592 * 1944

我想裁剪 159 * 129 的图像

这就是我在使用 cakephp 插件(Miles Johnsons Upload Plugin)时得到的结果

谁能帮我找到一个图像裁剪功能来做到这一点,或者帮助我用算法来做同样的事情。

【问题讨论】:

  • 您的意思是“调整大小”而不是“裁剪”吗?
  • 调整大小、裁剪或调整大小并裁剪?
  • 我不确定如何以正确的方式进行操作。我希望最终图像具有图像的最大部分,但具有我需要的大小。所以我认为它可能是调整大小和裁剪或其他组合。我不确定。

标签: php cakephp image-processing crop


【解决方案1】:

这个问题解决了,查看最新版本的 cake-uploader pluign。 https://github.com/milesj/cake-uploader/commit/2be63f32730755cffbace17ee8fa2d686785964d

【讨论】:

    【解决方案2】:

    我用这个:http://shiftingpixel.com/2008/03/03/smart-image-resizer/ 来创建在这里找到的所有图像缩略图:http://www.patriciashin.com/painting.php

    【讨论】:

    • 对不起,我没有添加更多,我的笔记本电脑电池没电了。无论如何,这个脚本是非常可定制的。他们的文档非常好。我想我什至能够调整脚本以更好地满足我的需求。 Cakephp 使进入 3rd 方脚本变得相当容易,但您可能必须将此脚本与一个类协调才能最有效。让我知道您的想法,我愿意提供帮助。
    • 看起来很有希望。我正计划构建一个插件并将其作为开源发布。你会感兴趣吗 ?因为那里的脚本我没有发现很多他们做适当的调整大小和裁剪。
    • 我当然会感兴趣。只需在异地联系我进行协调。我并不总是有很多时间,但我会尽我所能提供帮助。
    【解决方案3】:

    我必须说我没有彻底测试这段代码,我修改了它供个人使用,这应该可以帮助你解决问题。

    替换plugin/uploader/vendor/uploader.php中的crop函数

    368 号线附近

    具有以下功能

     public function crop(array $options = array(), $explicit = false) {
        if ($this->_data[$this->_current]['group'] != 'image' || !$this->_enabled) {
            return false;
        }
    
        $options = $options + array('location' => self::LOC_CENTER, 'quality' => 100, 'width' => null, 'height' => null, 'append' => null, 'prepend' => null);
        $width = $this->_data[$this->_current]['width'];
        $height = $this->_data[$this->_current]['height'];
        $src_x = 0;
        $src_y = 0;
        $dest_w = $width;
        $dest_h = $height;
        $location = $options['location'];
    
        if (is_numeric($options['width']) && is_numeric($options['height'])) {
            $newWidth = $options['width'];
            $newHeight = $options['height'];
    
            if ($width / $newWidth > $height / $newHeight) {
                $dest_h = $options['height'];
                $dest_w = round($width / ($height / $newHeight));
            } else {
                $dest_w = $options['width'];
                $dest_h = round($height / ($width / $newWidth));
            }
        } else {
            if ($width > $height) {
                $newWidth = $height;
                $newHeight = $height;
            } else {
                $newWidth = $width;
                $newHeight = $width;
            }
    
            $dest_h = $newHeight;
            $dest_w = $newWidth;
        }
    
        $src_x = 0;
        $src_y = 0;
        if ($dest_w > $newWidth) {
                $src_x = ceil(( ($dest_w - $newWidth) / 2) * ($height / $newHeight));
        }
    
        if ($dest_h > $newHeight) {
                $src_y = ceil(( ($dest_h - $newHeight) / 2) * ($width / $newWidth));
        }
    
        $append = '_cropped_' . $newWidth . 'x' . $newHeight;
    
        if ($options['append'] !== false && empty($options['append'])) {
            $options['append'] = $append;
        }
    
        $transform = array(
            'width' => $newWidth,
            'height' => $newHeight,
            'source_x' => $src_x,
            'source_y' => $src_y,
            'source_w' => $width,
            'source_h' => $height,
            'dest_w' => $dest_w,
            'dest_h' => $dest_h,
            'target' => $this->setDestination($this->_data[$this->_current]['name'], true, $options, false),
            'quality' => $options['quality']
        );
        if ($this->transform($transform)) {
    
            return $this->_returnData($transform, $append, $explicit);
        }
    
        return false;
    }
    

    亲切的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      相关资源
      最近更新 更多