1

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

替代文字

上图为 2592 * 1944

我想裁剪 159 * 129 的图像

替代文字

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

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

4

3 回答 3

1

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

替换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;
}

亲切的问候。

于 2012-02-15T13:04:29.020 回答
1

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

于 2010-10-01T05:43:47.340 回答
1

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

于 2012-04-15T16:37:14.587 回答