Welcome Guest, Not a member yet? Register   Sign In
Customer resize helper - in a loop, works once, second generates a GD error
#1

[eluser]Met[/eluser]
Here's my code (feel free to tear it apart if it's all wrong).

Code:
<?php
if (!function_exists('resize_img'))
{
    function resize_img($img="", $width=null, $height=null)
    {
        $CI =& get_instance();
        $config['image_library'] = 'gd2';
        $config['source_image'] = $img;
        
        $file_name_no_ext = pathinfo($tile, PATHINFO_FILENAME);
        $file_extension = pathinfo($tile, PATHINFO_EXTENSION);
        $new_image = './sized/' . $file_name_no_ext . '-' . $width . 'x' . $height . '.' . $file_extension;
        
        $config['new_image'] = $new_image;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;
        
        $CI->load->library('image_lib', $config);
        if(!$CI->image_lib->resize())
        {
            return $CI->image_lib->display_errors();
        }
        
  
        $CI->image_lib->clear();
        
        return $new_image; // returns something like /path/img-200x500.jpg
    }
}

I had hoped to be able to do this in my views:

Code:
<img src="&lt;?php echo resize_img('path/img.jpg', 200, 500); ?&gt; " />

I've also put this in a forloop, and the first instance works fine, but then the second one errors:

Quote:<p>Your server does not support the GD function required to process this type of image.</p>

Which doesn't make much sense, as it's just processed the first one. I have tried different images, and the "working" image but with a different name to the same effect.

If I remove $CI->image_lib->clear();, there is no error, but the image is the same as the first one for each iteration (due to not clearing it).

Any ideas?

thank you !
#2

[eluser]Met[/eluser]
http://ellislab.com/forums/viewthread/142592/#701077

Solved the problem, by placing image_lib->clear(); before the config, and initializing it manually.

Don't quite understand why this has an effect, but it's working now at least.




Theme © iAndrew 2016 - Forum software by © MyBB