ChrisBratherton
ChrisBratherton

Reputation: 1590

Generating image thumb in codeigniter

I am pretty new to codeigniter and just building my first application with it, but I am abit stumped when it comes to generating a thumbnail from an image.

The image uploads correctly but the thumb isnt generating and I am getting no errors :(

I hope someone can give me a helping hand, chances are I am just being a tit and its something really simple like mis spelling var.

Heres the code for my image model:

<?php

class Image_model extends CI_Model {

        var $image_path;

        function Image_model(){

            parent::__construct();

            $this->image_path = realpath(APPPATH.'../'.$this->config->item('dir_dynamic_images'));

        }

        function do_upload(){

            $config = array(
                    'allowed_types' => "jpeg|gif|jpg|png",
                    'upload_path' => $this->image_path,
                    'max_size' => 2000
            );

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

            $this->upload->do_upload();
            $image_data = $this->upload->data();



            $config = array(
                'source_image' => $image_data['full_path'],
                'new_image' => $this->image_path . '/thumbs',
                'maintain_ratio' => true,
                'width' => 200,
                'height' => 200
            );

            echo $config['new_image'];

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

        }               

    }
?>

Upvotes: 0

Views: 1449

Answers (2)

Pramod Kumar Sharma
Pramod Kumar Sharma

Reputation: 8012

This piece of code is working for me. Hopefully it will works for you . you can also check the image manipulation helper in codeigniter. Don'f forget to initialize the config.

 $config['image_library'] = 'imagemagick';
    $config['library_path'] = '/usr/X11R6/bin/';
    $config['source_image'] = '/path/to/image/mypic.jpg';
    $config['x_axis'] = '100';
    $config['y_axis'] = '60';

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

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

Upvotes: 0

Mukesh Soni
Mukesh Soni

Reputation: 6668

I think you need to set the create_thumb parameter to true and specify the image library-

 $config = array(
                'image_library' => 'gd2',
                'source_image' => $image_data['full_path'],
                'create_thumb' => true,
                'new_image' => $this->image_path . '/thumbs',
                'maintain_ratio' => true,
                'width' => 200,
                'height' => 200
            );

try finding the error with -

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

Upvotes: 1

Related Questions