PHP 图像调整大小和裁剪功能

发布于 2024-09-25 15:35:19 字数 359 浏览 4 评论 0原文

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

alt text

上面的图像是 2592 * 1944

我想裁剪 159 * 129 的图像

alt text

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

有人可以帮我找到一个图像裁剪函数来执行此操作吗或者帮助我用算法来做同样的事情。

I want a function which when i upload a photo it should crop the image irrespective of the ration of the image from the centre making sure the crop is well inside the image.

alt text

the above image is 2592 * 1944

i want to crop an image of 159 * 129

alt text

and this is what i get when using a plugin for cakephp (Miles Johnsons Upload Plugin)

can some one help me find a image crop function to do this or help me with the algorithm in doing the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤檠 2024-10-02 15:35:19

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

This problem is solved, checkout the latest version of cake-uploader pluign.
https://github.com/milesj/cake-uploader/commit/2be63f32730755cffbace17ee8fa2d686785964d

习ぎ惯性依靠 2024-10-02 15:35:19

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

函数crop 替换

将plugin/uploader/vendor/uploader.php 中第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;
}

Kind Regards。

I must say i have not thoroughly tested this piece of code, I modified it for personal use and this should help out your problem.

Replace the function crop in plugin/uploader/vendor/uploader.php

near line 368

with the following function

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

Kind Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文