Hassan Ali Shahzad
Hassan Ali Shahzad

Reputation: 2724

CodeIgniter Image thumbnailing issue

I want to create two thumbnails withh different sizes with two functions. uploadImage() makes big thumbnail while uploadIhumb($name){ it calls inside first function} create small thumbnail. FIRST FUNCTION CREATE THUMBNAIL BUT SECOND DOES NOT WORK. please solve the problem and highlight where problem occured?

enter code here:
function uploadImage()
        {
                                    $imageName2=$_FILES['file']['tmp_name'];


                $config['image_library'] = 'gd2';
                //$config['source_image']   = $imageName2;// this is temporary folder where image place on uploading time
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = false;
                $config['width']     = 696;
                $config['height']   = 241;
                $config['new_image'] = 'images/uploads';
                $this->load->library('image_lib', $config); 
                $this->image_lib->resize();
                $file_name= basename( $imageName2, ".tmp");
                $filename=$file_name.'_thumb.tmp'; 


        ////////////////////Rename code///////////////////////////
                $name= $this->rand_string( 6 );

                $dir=$_SERVER['DOCUMENT_ROOT'].'/images/uploads/';

                rename($dir . $filename, $dir . $name);
                echo $name;
                $this->uploadIhumb($name); //FUNCTION CALL
}

function uploadIhumb($name)
    {

                $config['image_library'] = 'gd2';
                $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/'.$name;
                $config['create_thumb'] = TRUE;

                $config['width']     = 80;
                $config['height']   = 80;
                $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'images/uploads/thumbs/';
                $this->load->library('image_lib', $config); 
                $this->image_lib->resize();


}

Upvotes: 4

Views: 3304

Answers (3)

sachin
sachin

Reputation: 11

hey thanks for your answer

yes this works for making at the end $this->image_lib->clear();

and for the 2nd thambnail // $this->load->library('image_lib', $config);

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

comment out 1st line and replace with the 2nd one. your query will be solve.

Upvotes: 0

Hassan Ali Shahzad
Hassan Ali Shahzad

Reputation: 2724

No Need of any other library required :-( problem can be resolved if we load image library only once, in start of the function. and only initialized config for second thumb. successful code is as under for conveyance:-

{ function createThumb1($imageName) //file name passed {

        // this thumbnail created
    $config['image_library'] = 'gd2';
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$imageName;

    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = false;
    $config['width']     = 80;
    $config['height']   = 80;
    $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/thumbs/'.$imageName;
    $this->load->library('image_lib', $config);
    if ( ! $this->image_lib->resize()){echo $this->image_lib->display_errors();}

    $this->image_lib->clear();

    // unable to create this this thumbnail
    $config['image_library'] = 'gd2';
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$imageName;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = false;
    $config['width']     = 696;
    $config['height']   = 241;
    $config['new_image'] =  $_SERVER['DOCUMENT_ROOT'].'/images/uploads/'.$imageName;
    //$this->load->library('image_lib', $config);  // this line cause problem
    $this->image_lib->initialize($config); // with this problem resolved
    if ( ! $this->image_lib->resize()){echo $this->image_lib->display_errors();}


    $this->image_lib->clear();






    $this->load->view('admin/upload_form',array('error' => ' ' ));





}

}

Upvotes: 5

smilly92
smilly92

Reputation: 2443

Try clearing your config at the top of your function using:

$this->image_lib->clear();

Also you can also check for any errors using:

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

Hope this helps.

Upvotes: 2

Related Questions