1

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

我试图通过使用$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,我对为什么会收到此错误感到困惑。

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

我究竟做错了什么?有没有更好的方法可以将图像大小调整为最大 278 像素宽度和最大 400 像素高度?

4

1 回答 1

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

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

于 2012-12-05T11:32:49.523 回答