Welcome Guest, Not a member yet? Register   Sign In
Weird problem concerning home made library
#1

[eluser]boudewijn[/eluser]
Hi all,

first off: I'm pretty new to CI so I hope im posting this in the right place and in the right way. Anyway, i'm having a strange problem concerning a resize library i wrote.

this is the code
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Resize {

    function resize_book_large($data)
    {
        
        $file_name = element('file_name', $data['upload_data']);
        $path = 'uploads/'.$file_name;
        
        $CI =& get_instance();
    
        $config['image_library'] = 'gd2';
        $config['source_image']    = $path;
        $config['new_image'] = 'uploads/thumbs/large_'.$file_name;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 300;
        $config['height']    = 450;
        
        $CI->load->library('image_lib', $config);
        
        echo $CI->image_lib->resize();
            
        if ( ! $CI->image_lib->resize())
        {
            echo $CI->image_lib->display_errors();
        }
    }
    
    function resize_book_small($data)
    {
        
        $file_name = element('file_name', $data['upload_data']);
        $path = 'uploads/'.$file_name;
        
        $CI =& get_instance();
        
        $config['image_library'] = 'gd2';
        $config['source_image']    = $path;
        $config['new_image'] = 'uploads/thumbs/small_'.$file_name;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 150;
        $config['height']    = 175;
        
        $CI->load->library('image_lib', $config);
        
        echo $CI->image_lib->resize();
            
        if ( ! $CI->image_lib->resize())
        {
            echo $CI->image_lib->display_errors();
        }
                
    }

    
}

?>

now i loaded $this->load->library('Resize'); in my controller, so far so good

the problem is however that
when i try to call upon

$this->resize->resize_book_small($data);
$this->resize->resize_book_large($data);

only the first one works (in this case _small, but if i shift large up, _large works).
By works i mean giving me the thumbnail in the directory i specified.

To make it even stranger:
Code:
echo $CI->image_lib->resize();
gives me TRUE for both functions

so it seems that they both work, but only the first one is giving me an output file...

I tried alot of things but im hopeless atm.
Hope someone can help and that this is clear enough!

cheers
#2

[eluser]GSV Sleeper Service[/eluser]
not sure what's going on there, but all that copypasta could lead to problems. looks like only the width/height and destination folder changes for each of those methods, you may be better off passing width/height/dest as parameters to a single method.
#3

[eluser]boudewijn[/eluser]
jup that was my initial construction:

Code:
class Resize {

    function resize_book_large($data)
    {
        
        $file_name = element('file_name', $data['upload_data']);
        $path = 'uploads/'.$file_name;
        
        $CI =& get_instance();
    
        $config['image_library'] = 'gd2';
        $config['source_image']    = $path;
        $config['new_image'] = 'uploads/thumbs/large_'.$file_name;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 300;
        $config['height']    = 450;
        
        $config2['new_image'] = 'uploads/thumbs/small_'.$file_name;
        $config2['width']     = 150;
        $config2['height']    = 175;
        
        $CI->load->library('image_lib', $config);
        $CI->load->library('image_lib', $config2);
        
        echo $CI->image_lib->resize();
            
        if ( ! $CI->image_lib->resize())
        {
            echo $CI->image_lib->display_errors();
        }
    }



however it doesnt work either...
still only one output file
#4

[eluser]xwero[/eluser]
You problem occurs because CI checks for loaded libraries. Load the library in the constructor and use the clear method before you use the initialize method to set the values.

Code:
class Resize
{
   var $CI;
  
   function Resize()
   {
     $this->CI =& get_instance();
     $this->CI->load->library('image_lib');
   }

   function resize_book_large($data)
    {
        
        $file_name = element('file_name', $data['upload_data']);
        $path = 'uploads/'.$file_name;
    
        $config['image_library'] = 'gd2';
        $config['source_image']    = $path;
        $config['new_image'] = 'uploads/thumbs/large_'.$file_name;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 300;
        $config['height']    = 450;
        $this->CI->image_lib->clear();
        $this->CI->image_lib->initialize($config);
        // and so on
The clear function is needed because you would like to run the two methods for the same file.
#5

[eluser]boudewijn[/eluser]
Thank you so much, that worked like a charm!




Theme © iAndrew 2016 - Forum software by © MyBB