首先调整大小时无法裁剪图像

时间:2012-12-04 22:58:25

标签: php codeigniter

我正在使用Codeigniter中的图像处理库,我需要将图像的大小调整为最大宽度为278像素,同时保持比率。我还需要确保图像不超过400px。

我尝试使用$this->image_lib->resize()然后使用$this->image_lib->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()调用时,我得到错误Your server does not support the GD function required to process this type of image.因为我之前能够裁剪图像并且根据phpinfo()安装了GD,所以我很困惑为什么我会收到此错误。< / p>

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

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

1 个答案:

答案 0 :(得分:1)

试试这个: -

            //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();

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