Welcome Guest, Not a member yet? Register   Sign In
Image Manipulation - Resize and create thumbnail
#1

[eluser]jedre[/eluser]
Hello!

After when I upload image file i need resize it and create thumbnail, but I want have 2 files.

For example:

I upload image file (image.jpg) 2000x1500px so I resize it to 500x500 and I want create thumb (70x50) from the same file. So I want have 2 files image.jpg (500x500) and image_thumb.jpg (70x50).

How Can I do it? I know how resize original file but I don't know how resize and create thumb.
#2

[eluser]Ebot Ndip-Agbor[/eluser]
This is what I use in my controller to resize my pictures, hope it helps
Code:
//some code here
                $picData=$this->upload->data();
                $picFilePath=$picData['full_path'];
                $picName=$picData['file_name'];
                $picRawName=$picData['raw_name'];
                $picsFolder=$picData['file_path'];
                $picThumbNail=$picsFolder."/thumb_".$picRawName.$picData['file_ext'];
                //load image library used by resize method below
                $this->load->library('image_lib');
                //resize the original to 500x500
                $this->resizePicture($picFilePath,$picFilePath,500,500);
                //resize the original pic again to get a thumbnail
                $this->resizePicture($picFilePath,$picThumbNail,70,50);
                
                $pictureData['picName']=$picName;
                $pictureData['thumbName']="thumb_".$picName;
                //save picture info in database
                $this->my_model->savePicture($pictureData);
#3

[eluser]jedre[/eluser]
Do you use a resize method from image_lib? Can you show me resizePicture method?

What is wrong on my code?

in my constructor:
Code:
$this->imgmanrules = array(
            'image_library'    => 'GD2',
            'maintain_ratio'    => TRUE,
            'width'                => 500,
            'height'                => 500,
            'quality'            => 85,
            'master_dim'        => 'width',
        );
        $this->load->library('image_lib');

code to resize image:
Code:
function _img()
    {
        //operacje na wgranym pliku
        $fileInfo = $this->upload->data();
        $this->saveData['nazwa'] = $fileInfo['file_name'];
        
        $this->imgmanrules['source_image'] = $fileInfo['full_path']; //pobieranie sciezki do pliku
        
        $width = getimagesize($fileInfo['full_path']);
        
        if($width[0] > $this->imgmanrules['width'] || $width[1] > $this->imgmanrules['height'])
        {
            //skalowanie obrazka
            $this->image_lib->initialize($this->imgmanrules);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
        
        $imgx = array(
            'image_library'    => 'GD2',
            'maintain_ratio'    => TRUE,
            'create_thumb'        => TRUE,
            'width'                => 100,
            'height'                => 100,
            'quality'            => 85,
            'master_dim'        => 'width',
            'source_image'        => $fileInfo['full_path']
        );
            //tworzenie miniatur
            $this->image_lib->initialize($imgx);
            $this->image_lib->resize();
    }

This code doesn't work how I write in first post :/

When image is smaller then 500x500px all is ok.
But when file is larger then 500x500 I take only one file with original name but size is thumbnail.

What is wrong?!
#4

[eluser]Ebot Ndip-Agbor[/eluser]
Sorry I completely forgot to post the resize method. Here it is
Code:
//use this to resize a picture to given specification and name
    function resizePicture($path,$newpath,$width,$height){
        $config['image_library'] = 'GD2';
        $config['source_image'] = $path;
        $config['new_image']=$newpath;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;
    
        //$this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
    }
#5

[eluser]jedre[/eluser]
Thank YOU. I'll also use "new_image" property.
#6

[eluser]Ebot Ndip-Agbor[/eluser]
Glad I could help Smile




Theme © iAndrew 2016 - Forum software by © MyBB