codeigniter resize image and create thumbnail

2020-03-25 02:48发布

问题:

hi according to the ci document you can resize images with image_lib and there are options that suggest we can create additional thumbnail from that image

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

so here is my code

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

i would assume this code resizes my image and also creates a thumbnail , but i only get one image with specified dimensions and _tump postfix

i've also tried to add this code to create second image manually but still it doesn't work and i get only one image

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

回答1:

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!!



回答2:

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

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


回答3:

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


回答4:

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