简体   繁体   中英

make image thumbnail before upload - php codeigniter

I want to resize the image to 160 x 160 and make thumbnail of it and then store that thumbnail in the folder. I don't want to store the real image, but the thumbnail of it only. Below is my code:

  $this->load->library('upload');

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

    $blog_image = $_FILES['blog_image']['name'];

    $config = array ('upload_path' => './blogs/',
                     'allowed_types' => "jpeg|jpg|png",
                     'overwrite' => TRUE,
                     'image_library' => 'gd2',
                     'source_image' => $blog_image,
                     'create_thumb' => TRUE,
                     'maintain_ratio' => TRUE,
                     'width' => 160,
                     'height' => 160
                     );

$this->upload->initialize($config);

$this->upload->do_upload('blog_image');

$this->image_lib->resize();

This code does not work. It uploads the image without resizing it. Please Help.

$config['image_library'] = 'gd2';
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['new_image'] = $target_path
$config['maintain_ratio'] = TRUE;
$config['width']    = 160;
$config['height']   = 160;

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

if (!$this->image_lib->resize()) {
    echo $this->image_lib->display_errors();
}

check this link for more info... https://stackoverflow.com/a/13154916/6588826

Try below sample code.

    $config = array(
        'upload_path' => './blogs/', 
        'allowed_types' => 'jpg|png|gif',
        'max_filename' => '255',
        'encrypt_name' => TRUE,

    );


    $this->load->library('upload', $config);
    //check file successfully uploaded. 'blog_image' is the name of the input
    if ($this->upload->do_upload('blog_image')) {
        //Now go to resize
        $image_data = $this->upload->data();
        $config_resize = array(
                'image_library' => 'gd2',
                'source_image' => $image_data['full_path'], //get original image
                'maintain_ratio' => TRUE,
                'width' => 160,
                'height' => 160
            );

        $this->load->library('image_lib', $config_resize);
        if (!$this->image_lib->resize()) {

            print_r($this->image_lib->display_errors());
        }
    }else{
        print_r($this->upload->display_errors());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM