4

嗨,根据 ci 文档,您可以使用 image_lib 调整图像大小,并且有一些选项建议我们可以从该图像创建额外的缩略图

create_thumb    FALSE   TRUE/FALSE (boolean)    Tells the image processing function to create a thumb.  R
thumb_marker    _thumb  None    Specifies the thumbnail indicator. It will be inserted just before the file extension, so mypic.jpg would become mypic_thumb.jpg    R

所以这是我的代码

        $config_manip = array(
            'image_library' => 'gd2',
            'source_image'  => "./uploads/avatar/tmp/{$this->input->post('new_val')}",
            'new_image'     => "./uploads/avatar/{$this->input->post('new_val')}",
            'maintain_ratio'=> TRUE ,
            'create_thumb'  => TRUE ,
            'thumb_marker'  => '_thumb' ,
            'width'         => 150,
            'height'        => 150 
        );

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

我会假设这段代码调整了我的图像大小并创建了一个缩略图,但我只得到一个具有指定尺寸和 _tump 后缀的图像

我也尝试添加此代码以手动创建第二张图片,但仍然无法正常工作,我只得到一张图片

            $this->image_lib->clear();

$config_manip['new_image'] = 
"./uploads/avatar/thumbnail_{$this->input->post('new_val')}";

            $config_manip['width']     = 30 ;
            $config_manip['height']    = 30 ;
            $this->load->library('image_lib', $config_manip);
            $this->image_lib->resize();
4

4 回答 4

19

似乎路径是您代码中的问题。我自己修改并测试了它的工作原理。

public function do_resize()
{
    $filename = $this->input->post('new_val');
    $source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/tmp/' . $filename;
    $target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/';
    $config_manip = array(
        'image_library' => 'gd2',
        'source_image' => $source_path,
        'new_image' => $target_path,
        'maintain_ratio' => TRUE,
        'create_thumb' => TRUE,
        'thumb_marker' => '_thumb',
        'width' => 150,
        'height' => 150
    );
    $this->load->library('image_lib', $config_manip);
    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    }
    // clear //
    $this->image_lib->clear();
}

希望这对您有所帮助。谢谢!!

于 2012-07-16T13:21:38.187 回答
2

创建缩略图的简单方法。

function _create_thumbnail($fileName, $width, $height) 
{
    $this->load->library('image_lib');
    $config['image_library']  = 'gd2';
    $config['source_image']   = $_SERVER['DOCUMENT_ROOT']. $fileName;       
    $config['create_thumb']   = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']          = $width;
    $config['height']         = $height;
    $config['new_image']      = $_SERVER['DOCUMENT_ROOT']. $fileName;               
    $this->image_lib->initialize($config);
    if (! $this->image_lib->resize()) { 
        echo $this->image_lib->display_errors();
    }        
}
于 2014-12-29T09:44:11.600 回答
2

您的代码没问题,但您需要做一些小改动。

 $this->load->library('image_lib');
 $this->image_lib->initialize($config_manip);
于 2015-12-31T21:18:17.087 回答
0

如果要使用方法创建多个图像,则每次尝试调整大小时都resize()需要调用。$this->image_lib->initialize($config);

本教程为我解决了上传图像并在 CodeIgniter 中创建多个缩略图大小

于 2015-05-12T15:37:04.340 回答