1

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.

Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51
  • Does your server has GD/GD2, NetPBM, or ImageMagick installed? – alloyking Sep 19 '16 at 11:57
  • I don't know. How can I check that. I am currently working on my localhost AppServ – Ajmal Razeel Sep 19 '16 at 11:57
  • Are you on Windows, Mac or Linux? – alloyking Sep 19 '16 at 11:58
  • I am on Windows 8 – Ajmal Razeel Sep 19 '16 at 12:00
  • Hello Ajama, to check GD library : http://www.zoopable.com/check-php-gd-library-installed-or-not/ – Hardik Patel Sep 19 '16 at 12:01
  • You are mixing two libraries: `upload` and `image_lib`. You have first to [upload an image](https://codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences), then [manipulate that file](https://codeigniter.com/userguide3/libraries/image_lib.html#processing-an-image). Use `$config_upl` and `$config_man` names for respective arrays of preferences or clear array after file is uploaded and before is used by other library. If you don't want to keep original file, use `unlink('/uploaded/original/image/absolute/path/name.jpg')` PHP function to delete it after whole process. – Tpojka Sep 19 '16 at 12:02
  • I'm not super into windows. But you might start with something like this. http://stackoverflow.com/questions/33336327/how-to-install-imagemagick-for-wamp-2-5. I'm assuming you're running WAMP. – alloyking Sep 19 '16 at 12:02

2 Answers2

0
$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

Community
  • 1
  • 1
Sam B
  • 80
  • 1
  • 11
0

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());
    }
Saji
  • 1,374
  • 1
  • 12
  • 19