codeigniter调整图像大小并创建缩略图

时间:2012-07-16 12:29:32

标签: php codeigniter resize gd

根据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 个答案:

答案 0 :(得分: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();
}

希望这会对你有所帮助。谢谢!

答案 1 :(得分: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();
    }        
}

答案 2 :(得分:2)

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

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

答案 3 :(得分:0)

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

本教程为我解决了Upload Image and Create Multiple Thumbnail Sizes in CodeIgniter