codeigniter resize image and create thumbnail

江枫思渺然 提交于 2019-12-01 07:35:21

It seems path is the issue in your code. I modified and tested myself it works.

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

Hope this helps you. Thanks!!

Your code is Okay but you need to do a small change.

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

A simple way to create a thumbnail.

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

If you want to create more than one image using resize() method, you need to call $this->image_lib->initialize($config); each time you attempt a resize.

This tutorial solved it for me Upload Image and Create Multiple Thumbnail Sizes in CodeIgniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!