CodeIgniter Forums
Problem with image manipulation (resizing) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with image manipulation (resizing) (/thread-24654.html)



Problem with image manipulation (resizing) - El Forum - 11-16-2009

[eluser]tommizzle[/eluser]
Hi all,

I'm currently having a problem with resizing an image using CI's image manipulation class. Here is my controller:

Code:
<?php

class Photo extends Controller {
    
    function _remap($username)
    {    
        
        $data = array();
        
        $this->load->helper('file');
        $this->load->library('image_lib');
        
        $data['userid'] = $this->Autoload_model->get_userid_from_username($username);
        $data['upload_success'] = false;
        $data['is_my_profile'] = false;
        
        if($this->input->post('upload_image')):
            
            $upload_image['file_name'] = $data['userid'];
            $upload_image['upload_path'] = './images/profile/';
            $upload_image['allowed_types'] = 'gif|jpg|png';
            $upload_image['max_width']  = '1024';
            $upload_image['max_height']  = '768';
            $upload_image['overwrite']  = TRUE;
            
            $this->load->library('upload', $upload_image);
        
            if ( ! $this->upload->do_upload()):
                $data['error'] = array('error' => $this->upload->display_errors());
            else:
                $data['upload_success'] = true;
            endif;
            
            $config['image_library'] = 'gd2';
            $config['source_image'] = './images/profile/' . $data['userid'] . '.jpg';
            $config['new_image'] = './images/profile/new_image.jpg';
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 50;
            $config['height'] = 50;
            
            $this->load->library('image_lib', $config);
            
            if ( ! $this->image_lib->resize()):
                echo $this->image_lib->display_errors();
            else:
                echo 'Resize Success';
            endif;
            
        endif;
    
        
        
        if($this->input->post('delete_image')):
            unlink('./images/profile/' . $data['userid'] . '.jpg');
        endif;
        
        
        $data['has_photo'] = read_file('./images/profile/' . $data['userid'] . '.jpg');
        
        if($this->authlib->getUserName() == $username) $data['is_my_profile'] = true;
            
        $this->load->view('photo',$data);
    }

}
    
?>

and my view:
Code:
<?php
if(isset($error)) echo $error;

if($has_photo != false):
        echo '<img src="/images/profile/' . $userid . '.jpg" alt="My Profile Image" />';
else:
        echo '<img src="/images/profile/placeholder.png" alt="My Profile Image" />';
endif;

if($is_my_profile === true):
?&gt;
    &lt;form action="" method="post" enctype="multipart/form-data"&gt;
        &lt;input type="file" name="userfile" size="20" /&gt;
        &lt;input type="submit" name="upload_image" value="upload" /&gt;
        &lt;?php
        if($has_photo != false):
        ?&gt;
            &lt;input type="submit" name="delete_image" value="delete" /&gt;
        &lt;?php
        endif;
        ?&gt;
        
        
    &lt;/form&gt;
&lt;?php
endif;

if($upload_success === true) echo 'Success!';
?&gt;

The image is uploading, and then I'm even getting 'Resize Success', but it's not resizing or creating a new thumb in my chosen dir. I tried the normal 'GD' library too, and that didn't work either.

Has anybody got any ideas what's going wrong here? Any help would be appreciated Smile

Thanks guys!

Tom


Problem with image manipulation (resizing) - El Forum - 11-16-2009

[eluser]hugle[/eluser]
hm...
I've been using this lib a while ago with no problems...
Is the directory you are writing thumb writeable?

echo the $config['source_image'];

what does it show?


Problem with image manipulation (resizing) - El Forum - 11-16-2009

[eluser]tommizzle[/eluser]
Hi hugle,

source_image shows: ./images/profile/11.jpg (Correct path to my image, not sure about the dot though?)

The directory and the files in it are 777'd

Thanks,

Tom

Edit: Just checked my log, and I'm getting this error:
Code:
ERROR - 2009-11-16 04:23:30 --&gt; The session cookie data did not match what was expected. This could be a possible hacking attempt.
Not sure if it's related?


Problem with image manipulation (resizing) - El Forum - 11-16-2009

[eluser]tommizzle[/eluser]
Solved the problem:

There is an error in the documentation on: http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

Quote:$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;

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

$this->image_lib->resize();

Pretty frustrating stuff. Especially as I've spent ~2 days on it :/


Problem with image manipulation (resizing) - El Forum - 11-16-2009

[eluser]hugle[/eluser]
[quote author="tommizzle" date="1258389238"]Solved the problem:

There is an error in the documentation on: http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

Quote:$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;

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

$this->image_lib->resize();

Pretty frustrating stuff. Especially as I've spent ~2 days on it :/[/quote]


bah... so bad Sad
sometimes it's good to leave of the pc for 20 minutes, and come back and start from the beginning, but not searching for errors...
sometimes happen!Smile))


Problem with image manipulation (resizing) - El Forum - 11-24-2009

[eluser]Unknown[/eluser]
aw snap. just spent 3 hours on this same one. i really could have used the fixed docs.


Problem with image manipulation (resizing) - El Forum - 02-17-2010

[eluser]stef25[/eluser]
Same here, documentation really needs to be fixed.


Problem with image manipulation (resizing) - El Forum - 02-17-2010

[eluser]Craig300[/eluser]
There isn't really a problem with the documentation it all depends if you have already initialised the library already in the file. If you have, then you just need to initialise it with the array values you pass to it. If you haven't initialised the library, then you can do both at the same time by calling $this->load->library('image_lib', $config);


Problem with image manipulation (resizing) - El Forum - 02-17-2010

[eluser]stef25[/eluser]
That's true, my bad. It's just that in the docs it first says

Code:
$this->load->library('image_lib');

and then right below that again

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

That's what's confusing ...


Problem with image manipulation (resizing) - El Forum - 02-17-2010

[eluser]Craig300[/eluser]
That got me for a while as well, maybe it could be a bit clearer but once you see whats happening, it kind of makes sense Smile

Glad I could help Smile