Welcome Guest, Not a member yet? Register   Sign In
Image_lib resize transparent gif hack
#1

[eluser]Unknown[/eluser]
Hello everybody,
I'm using CI for lots of my projects and encountered recently a problem of transparency with my GIF images when using image_lib to resize it. The transparent background was turned into black. After a quick seach in the forum I found a solution here :

http://ellislab.com/forums/viewthread/62955/

The only problem I found among all posts was that guys (and girls ^^) where speaking about hacking CI Core to change the behavior of the image_lib library.

May I suggest to Extend the native library instead of hacking it :

Create in "libraries" a file called MY_Image_lib.php and paste the following code into it.

Then try and enjoy ^^

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

// Extends the native Image_lib library
class MY_Image_lib extends CI_Image_lib {

    function MY_Image_lib()
    {
      parent::CI_Image_lib(); // Extends the image_lib library                
    }

  // Hack allowing to keep the transparency of GIF images
    function image_process_gd($action = 'resize')
    {    
        $v2_override = FALSE;
            
        if ($action == 'crop')
        {
            // If the target width/height match the source then it's pointless to crop, right?
            // So if dynamic output isn't on, then we'll return true so the user thinks
            // the process succeeded. It'll be our little secret...

            if ($this->width >= $this->orig_width AND $this->height >= $this->orig_height AND $this->dynamic_output !== TRUE)
            {
                return TRUE;
            }

            //  Reassign the source width/height if cropping
            $this->orig_width  = $this->width;
            $this->orig_height = $this->height;    
                
            // GD 2.0 has a cropping bug so we'll test for it
            if ($this->gd_version() !== FALSE)
            {
                $gd_version = str_replace('0', '', $this->gd_version());            
                $v2_override = ($gd_version == 2) ? TRUE : FALSE;
            }
        }
        else
        {
            // If the target width/height match the source, AND if
            // the new file name is not equal to the old file name
            // we'll simply make a copy of the original with the new name        
            if (($this->orig_width == $this->width AND $this->orig_height == $this->height) AND ($this->source_image != $this->new_image))            
            {
                if ( ! @copy($this->full_src_path, $this->full_dst_path))
                {
                    $this->set_error('imglib_copy_failed');
                    return FALSE;
                }
            
                @chmod($this->full_dst_path, DIR_WRITE_MODE);
                return TRUE;
            }
            
            // If resizing the x/y axis must be zero
            $this->x_axis = 0;
            $this->y_axis = 0;
        }
        
        //  Create the image handle
        if ( ! ($src_img = $this->image_create_gd()))
        {        
            return FALSE;
        }

         //  Create The Image
        //
        //  old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
        //  it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
        //  below should that ever prove inaccurate.
        //
        //  if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
         if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))        
        {
            $create    = 'imagecreatetruecolor';
            $copy    = 'imagecopyresampled';
        }
        else
        {
            $create    = 'imagecreate';    
            $copy    = 'imagecopyresized';
        }
            
        $dst_img = $create($this->width, $this->height);

    // HERE IS THE HACK http://ellislab.com/forums/viewthread/92355/
    // keeping transparency
    $transparent_index = imagecolortransparent($src_img);
    if ($transparent_index >= 0) {
        imagepalettecopy($src_img, $dst_img);
        imagefill($dst_img, 0, 0, $transparent_index);
        imagecolortransparent($dst_img, $transparent_index);
        imagetruecolortopalette($dst_img, true, 256);
    }
        
        $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);

        //  Show the image    
        if ($this->dynamic_output == TRUE)
        {
            $this->image_display_gd($dst_img);
        }
        else
        {
            // Or save it
            if ( ! $this->image_save_gd($dst_img))
            {
                return FALSE;
            }
        }

        //  Kill the file handles
        imagedestroy($dst_img);
        imagedestroy($src_img);
        
        // Set the file to 777
        @chmod($this->full_dst_path, DIR_WRITE_MODE);
        
        return TRUE;
    }
    
}

?>




Theme © iAndrew 2016 - Forum software by © MyBB