第一次调整大小时无法裁剪图像 [英] Can't crop image when resizing first

查看:31
本文介绍了第一次调整大小时无法裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter中的图像处理库,并且需要在保持比例的同时将图像的最大尺寸调整为最大278px.我还需要确保图像不超过400像素.

I am using the Image Manipulation library in Codeigniter and I need to resize an image to be a max of 278px in width while maintaining ratio. I also need to make sure the image does not exceed 400px.

我正在尝试通过使用 $ this-> image_lib-> resize()来执行此操作,然后使用 $ this-> image_lib-> crop(),但是我在调​​整大小时遇到​​麻烦.

I am attempting to do this by using $this->image_lib->resize() and then running it again using $this->image_lib->crop(), but I am having trouble with the resize interfering with the crop.

这是两个模型:

public function create_thumb($path) {

    $data = $this->upload->data();

    if ($data['image_width'] >= 278):

        $config['image_library'] = 'gd2';
        $config['source_image'] = $path;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 278;
        $config['height'] = 400;
        $config['quality'] = '90%';
        $config['master_dim'] = 'width';

        $this->load->library('image_lib', $config);

        if ($this->image_lib->resize()):

            $this->image_lib->clear();

        endif;

    endif;

    $this->crop_image($path);

    return false;
}

// Make max image size 278x400
public function crop_image($path) {

    list($width, $height) = getimagesize($path);

    $config['image_library'] = 'gd2';
    $config['source_image'] = $path;
    $config['x_axis'] = '0';
    $config['y_axis'] = '0';
    $config['maintain_ratio'] = FALSE;
    $config['width'] = $width;
    $config['height'] = 400;
    $config['quality'] = '100%';

    $this->load->library('image_lib', $config);

    if ($this->image_lib->crop())
    {
        return true;
    }
    return false;
}

如果我直接从控制器调用crop_image(),它将按预期进行裁剪.但是,当从create_thumb()调用它时,出现错误您的服务器不支持处理此类图像所需的GD功能.由于我以前可以裁剪图像并且已安装GD根据phpinfo(),我对为什么会收到此错误感到困惑.

If I call crop_image() directly from a controller, it crops as expected. However, when it's called from create_thumb(), I get the error Your server does not support the GD function required to process this type of image. Since I am able to crop the image previously and GD is installed according to phpinfo(), I am confused on why I am getting this error.

我认为问题与两次加载image_lib有关,但是我认为 $ this-> image_lib-> clear(); 可以解决该问题?

I think the problem is related to loading the image_lib twice, but I thought that $this->image_lib->clear(); would solve that problem?

我做错了什么?我有更好的方法将图像调整为最大278px的宽度和最大400px的高度吗?

What am I doing wrong? Is there a better way for me to resize an image to a maximum of 278px width and a maximum of 400px in height?

推荐答案

尝试一下:-

            //Resize Image
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/uploaded_files/gallery/original/'.$image_name;
            $config['new_image'] = './assets/uploaded_files/gallery/banner/'.$image_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['master_dim']= 'width';
            $config['quality']  = '100';
            $config['width'] = 1260;
            $config['height']= 645;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            //Crop image
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/uploaded_files/gallery/banner/'.$image_name;
            $config['new_image'] = './assets/uploaded_files/gallery/banner/'.$image_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['quality']  = '100';
            $config['x_axis'] = 0;
            $config['y_axis'] = 0;
            $config['width'] = 1260;
            $config['height']= 645;
            $this->image_lib->initialize($config);
            $this->image_lib->crop();

根据需要调整宽度和高度.

Adjust width and height according to your need.

这篇关于第一次调整大小时无法裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆