Welcome Guest, Not a member yet? Register   Sign In
Upload image --> resize and create thumb
#1

[eluser]hsl[/eluser]
Hello,

I'm trying to upload an image. So far it works.
Now with the uploaded image i want to resize it to a new directory and make a thumb at the same time.
So this is what i do:
Code:
public function img_resize($upload,$post)
    {    
        $newpath = dirname(FCPATH).'/assets/shop/'.$post['title'].$upload['file_ext'];
        
        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 480;
        $config['height']    = 480;
        
        
        $this->load->library('image_lib', $config);
        
        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        
        $this->make_thumb($newpath);

        //remove temp_image here
    }

    public function make_thumb($path)
    {
        $config['image_library'] = 'gd2';
        $config['source_image']    = $path;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 150;
        $config['height']    = 150;

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

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

The second function doesn't work and there doesn't seem to be a way to
do it in one image resize action.

This is what is in my controller:
Code:
$config_upload['upload_path'] = './assets/temp/';
$config_upload['allowed_types'] = 'gif|jpg|png';
$config_upload['max_size']    = '5000';
$config_upload['encrypt_name'] = TRUE;

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

if (!$this->upload->do_upload())
   {
     $upload = $this->upload->display_errors();
   }    
else
   {
     $upload = $this->upload->data();
   }
$this->hsl_shop->img_resize($upload,$_POST);

How would you guys solve this?

Regards,

HSL
#2

[eluser]Sumon[/eluser]
Try by add
Code:
$this->image_lib->clear();
before the line
Code:
$this->make_thumb($newpath);
in function img_resize($upload,$post)


if you still getting same problem then try with
Code:
unset($config);
$this->image_lib->clear();
#3

[eluser]hsl[/eluser]
Thx!

But it still doesn't work, with both options I get this error:
Code:
Your server does not support the GD function required to process this type of image.

any other ideas?
#4

[eluser]manilodisan[/eluser]
Since I started using verot's class I never looked back... Smile. I can't remember very well but I had problems with CI's integrated upload class at that time and I started looking for a serious class to perform uploads and image manipulation tasks.
#5

[eluser]Sumon[/eluser]
Do your server have GD library?
Code:
echo phpinfo(); // now see whether GD is installed in your server or not.
#6

[eluser]hsl[/eluser]
[quote author="manilodisan" date="1221970290"]Since I started using verot's class I never looked back... Smile. I can't remember very well but I had problems with CI's integrated upload class at that time and I started looking for a serious class to perform uploads and image manipulation tasks.[/quote]
Mhmmm,.. i'm not really planning to use an extern class within a CI project.

[quote author="Sumon" date="1221996672"]Do your server have GD library?
Code:
echo phpinfo(); // now see whether GD is installed in your server or not.
[/quote]

Yes, It has, the first action: resizing and copying work fine.
It's the second function create_thumb() that's giving the error.
#7

[eluser]hsl[/eluser]
the first function is now this:
Code:
public function img_resize($upload,$post)
    {    
        $newpath = dirname(FCPATH).'/assets/shop/'.$post['title'].$upload['file_ext'];
        
        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 480;
        $config['height']    = 480;
        
        
        $this->load->library('image_lib', $config);

        //$this->image_lib->resize();
        
        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        
        unset($config);
        $this->image_lib->clear();
        $this->make_thumb($newpath);
        
        //remove temp_image
    }
and this is the second function (the one that doesn't work):
Code:
public function make_thumb($path)
    {
        $config['image_library'] = 'gd2';
        $config['source_image']    = $path;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 150;
        $config['height']    = 150;

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

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

[eluser]hsl[/eluser]
fixed it,..

now I load the library only once, and initialize it with the new config in each function.

Code:
public function img_resize($upload,$post)
    {    
        $this->load->library('image_lib');
        $newpath = dirname(FCPATH).'/assets/shop/'.$post['title'].$upload['file_ext'];
        
        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 480;
        $config['height']    = 480;
        
        $this->image_lib->initialize($config);
        
        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        
        $newpath_thumb = dirname(FCPATH).'/assets/shop/thumbs/'.$post['title'].$upload['file_ext'];
        
        unset($config);
        $this->image_lib->clear();
        $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {
        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 150;
        $config['height']    = 150;

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

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

[eluser]vvva_avvv[/eluser]
Somebody help me :cheese: I try to upload. It work but I can't resize my image. I guess my source_image not detect because if I close this code $config['source-image'], there isn't influence. Please help me how to resize.
This the code :
function do_upload()
{ $this->load->library('Image_lib');
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_path'] = realpath(APPPATH.'../uploads/avatar');
$config['overwrite'] = TRUE;
$config['max_size'] = '200';
$config['max_width'] = '500';
$config['max_height'] = '500';

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

$image = $this->upload->data();

$config['image_library'] = 'gd';
$config['source_image'] = realpath(APPPATH.'../uploads/avatar').$image['file_name'];
$config['new_image'] = realpath(APPPATH.'../uploads/avatar').'/after'.$image['file_name'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height']=75;

$this->upload->initialize($config);//if we need
$this->load->library('image_lib', $config);

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

$this->load->helper('url');
$data['file']=$image['file_name'];
$this->load->view('sss', $data);
}
}

thank for your help ^^
#10

[eluser]Samuel Martins[/eluser]
[quote author="hsl" date="1222026819"]fixed it,..

now I load the library only once, and initialize it with the new config in each function.

Code:
public function img_resize($upload,$post)
    {    
        $this->load->library('image_lib');
        $newpath = dirname(FCPATH).'/assets/shop/'.$post['title'].$upload['file_ext'];
        
        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 480;
        $config['height']    = 480;
        
        $this->image_lib->initialize($config);
        
        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        
        $newpath_thumb = dirname(FCPATH).'/assets/shop/thumbs/'.$post['title'].$upload['file_ext'];
        
        unset($config);
        $this->image_lib->clear();
        $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {
        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 150;
        $config['height']    = 150;

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

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }
[/quote]

I am doing exactly the same that you with a diferent directories, but only do img_resize() and when is to do the thumb i receive this message error

Quote:Unable to save the image. Please make sure the image and file directory are writable.

My directory is OK with the permissions.. any help please.




Theme © iAndrew 2016 - Forum software by © MyBB