Part of the EllisLab Network
   
2 of 2
2
image_lib - resizing transparent png24
Posted: 24 June 2010 04:26 PM   [ Ignore ]   [ # 16 ]  
Summer Student
Total Posts:  12
Joined  10-14-2008
mandelkern - 25 April 2008 04:08 PM

I found a solution at http://www.webdeveloper.com/forum/showthread.php?t=178210.

Nearly at line

514 - $dst_img = $create($this->width, $this->height);

I changed the code this way:

//$dst_img = $create($this->width, $this->height);

/* TRANSPARENT PNG */
$dst_img = imagecreatetruecolor($this->width, $this->height);
$transparent = imagecolorallocatealpha($dst_img,0,255,0,127);
imagefill($dst_img,0,0,$transparent);

$copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);

imageAlphaBlending($dst_img, false);
imageSaveAlpha($dst_img, true);

..and now it works.

This was very helpful, thank you.
Why this is not the normal behavior of the image_lib is beyond me.

Profile
 
 
Posted: 24 December 2010 09:01 PM   [ Ignore ]   [ # 17 ]  
Grad Student
Avatar
Rank
Total Posts:  65
Joined  01-11-2010

Great!
here’s how someone can use this without modifying the core function (easiest for updating)

Do not edit /system/librairies/Image_lib.php
Instead, create a new file in your /application/libraries/ folder
And call it MY_Image_lib.php

Copy and paste this code :

/application/libraries/MY_Image_lib.php

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

/**
 * CodeIgniter Image Manipulation Class fixed
 * fixed method "image_process_gd" to keep png transparency when resizing
 * more info : http://codeigniter.com/forums/newreply/77836/
 */
class MY_Image_lib extends CI_Image_lib {

  
function MY_Image_lib() {
    parent
::CI_Image_lib();
  
}

  
/**
   * FIXED function to keep transparency
   * Image Process Using GD/GD2
   *
   * This function will resize or crop
   *
   * @access    public
   * @param    string
   * @return    bool
   */
  
function image_process_gd($action 'resize'{
    $v2_override 
FALSE;

    
// 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... assuming dynamic rendering is off.
    
if ($this->dynamic_output === FALSE{
      
if ($this->orig_width == $this->width AND $this->orig_height == $this->height{
        
if ($this->source_image != $this->new_image{
          
if (@copy($this->full_src_path$this->full_dst_path)) {
            
@chmod($this->full_dst_pathDIR_WRITE_MODE);
          
}
        }

        
return TRUE;
      
}
    }

    
// Let's set up our values based on the action
    
if ($action == 'crop'{
      
//  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 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';
    
}

    
// CODE:
    //        $dst_img = $create($this->width, $this->height);
    //        $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
    // REPLACED WITH: start
    // TRANSPARENT PNG
    
$dst_img imagecreatetruecolor($this->width$this->height);
    
$transparent imagecolorallocatealpha($dst_img02550127);
    
imagefill($dst_img00$transparent);
    
$copy($dst_img$src_img00$this->x_axis$this->y_axis$this->width$this->height$this->orig_width$this->orig_height);
    
imageAlphaBlending($dst_imgfalse);
    
imageSaveAlpha($dst_imgtrue);
    
// REPLACED WITH: end

    //  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_pathDIR_WRITE_MODE);

    return 
TRUE;
  
}

}

/* End of file MY_Image_lib.php */
/* Location: ./application/libraries/MY_Image_lib.php */ 


Now when you’ll call Image_lib as usual, this file be auto loaded and override the core functions, using the ones defined in this new file.

read more about extending CI :http://codeigniter.com/user_guide/general/creating_libraries.html

I hope i helped a bit because this fix helped me a lot !

 Signature 

PAMPSERVER64: http://www.pampserver.com/
NETBEANS 7: http://netbeans.org
MAXTHON 3: http://www.maxthon.com
Win7 64bits

Profile
 
 
   
2 of 2
2