Part of the EllisLab Network
   
1 of 2
1
image_lib - resizing transparent png24
Posted: 25 April 2008 03:56 AM   [ Ignore ]  
Summer Student
Total Posts:  6
Joined  04-25-2008

Hi,

I tried to resize a png-24 file with the codeigniter-image_lib, using GD2. The result looses its transparency and shows a black background.

I couldn’t find any solutions, fixing this within the image_lib.

Can someone help me with this problem?


Thanks.

Profile
 
 
Posted: 25 April 2008 04:47 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

Look at:
http://codeigniter.com/forums/viewthread/62955/#311374

Is this helping?

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 25 April 2008 04:59 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  6
Joined  04-25-2008

Thanks, elite but I already tried this. It helps fine with gif and png-8 but my problem seems to be special with GD2 and png-24.

Profile
 
 
Posted: 25 April 2008 05:06 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

I posted the issue some time ago:
http://codeigniter.com/forums/viewthread/71962/

It is a CI bug and it looks like nobody has found a solution yet. You’ll have to dive into the CI Image lib code and rework the GD part that make the transparency working. Sorry I can’t help much more.

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 25 April 2008 05:26 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  6
Joined  04-25-2008

Ok. Thanks anyway. I’ll try my best grin

Profile
 
 
Posted: 25 April 2008 05:48 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

If you find a solution please post it.

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 25 April 2008 11:45 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  57
Joined  03-11-2008

I had this same problem solved in PHP a while ago. Can you send me a link to a sample image that fails to work. If it keeps transparency in my code I’ll port the hack to CI library.

Profile
 
 
Posted: 25 April 2008 12:08 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  6
Joined  04-25-2008

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.

Profile
 
 
Posted: 25 April 2008 12:42 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

Hey that’s really cool, please report that + solution to the bug tracker of CI or to the forums bugs category

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 25 April 2008 12:47 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  57
Joined  03-11-2008

That’s pretty similar to my PHP code, except that I refactored the ImageSetAlpha function:

function ResizeImage($newWidth$newHeight)
    
{
        $this
->newWidth $newWidth;
        
$this->newHeight $newHeight;
        
$this->dest_image imagecreatetruecolor($this->newWidth$this->newHeight);
        
$this->ImageSetAlpha();
        
imagecopyresampled($this->dest_image$this->src_image0000,
          
$this->newWidth$this->newHeightimagesx($this->src_image), imagesy($this->src_image));
        
$this->src_image $this->dest_image;
    
}

    
function ImageSetAlpha ()
    
{
        $background 
imagecolorallocate($this->dest_image000);
        
ImageColorTransparent($this->dest_image$background);
        
imagealphablending($this->dest_imagefalse);
    
Profile
 
 
Posted: 11 May 2008 06:03 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  116
Joined  10-02-2002

BorisK,

Your solution looks exactly like something I need. Pardon the newbie question, but where do these functions you’ve posted live? Are they in a model or controller?

-John Morton

 Signature 

———-
work: jmx2.com
blog: SuperGeekery
twitter: johnmorton
email: .(JavaScript must be enabled to view this email address)

Profile
 
 
Posted: 14 May 2008 05:27 PM   [ Ignore ]   [ # 11 ]  
Grad Student
Rank
Total Posts:  57
Joined  03-11-2008

John, those functions were not built for CodeIgniter but for general PHP code. Let me know if you problems with porting it to CI.

Profile
 
 
Posted: 14 May 2008 07:34 PM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  118
Joined  02-19-2008

I always get this message, what is that supposed to mean ? Thankx

Message: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file

Filename: libraries/Image_lib.php

Profile
 
 
Posted: 14 May 2008 07:39 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  116
Joined  10-02-2002

I’m still fighting with it actually. The way I’ve currently been resizing images is using CI’s built in framework and it’s working except when I have a PNG (or GIF?) that has a transparency. Here’s what I’ve got which lives in my model.

You’ll see I’ve got my resizing happening in this one big function. I like the idea of using ResizeImage as in your post, but need some advice. Any advice is greatly appreciated. -John

function updateHomeImage($myImageFromForm
    
{
        $shrinkdimensions 
250;
        
$enlargedimensions 500;
        
$config['upload_path''./uploads/home/original/'
        
$config['allowed_types''gif|jpg|png'
        
$config['max_size''200'
        
$config['max_width''1000'
        
$config['max_height''1000'
        
$config['encrypt_name'false
        
$config['remove_spaces'true
        
$this->load->library('upload'$config);
        
        if (
$this->upload->do_upload($myImageFromForm))
            
foreach ($this->upload->data() as $item => $v)
            
# print_r("item=".$item." v=".$v."<br />"); 
            # returns lots of info about the uploaded file which is stored in the data variable belowe
            
$data[$item] $v
        
}
        
# storing file details in toupdatedb to be inserted into database
        
$toupdatedb->primaryoriginal $data['file_name'];
        
        
$this->orig_width $data['image_width'];
        
$this->orig_height $data['image_height'];
        
        
// Start image_lib to allow resizing
        
$this->load->library('image_lib'); 
        
$imgfile $data['full_path'];
        
$imgpath $data['file_path'];
        
$filename $data['file_name'];
        
$config['image_library''GD2'
        
$config['source_image'$imgfile;
        
$config['maintain_ratio'TRUE
        
$config['create_thumb'false;
        
# need to find base_path so of uploaded file to store resized images in sibling directories
        
$base_path substr($imgpath0strripos($imgpath'original/'));
    
        
#// Make Small resize
        
if (($this->orig_width >= $shrinkdimensions) || ($this->orig_height >= $shrinkdimensions))
              
{
            
//print_r("small sizeifier fired <br />");
              # $this-ResizeImage(250, 250); <- didn't work, so commented out
        
            
$config['new_image'$base_path'small/'.$filename;
            
$config['width'$shrinkdimensions
            
$config['height'$shrinkdimensions
            
$config['master_dim''width'// this sets the resizer to default to height when preserving the ratio
            
$this->image_lib->initialize($config); 
            if (
$this->image_lib->resize())
                
{        
                
//print_r("success small  <br />");
                
$toupdatedb->primarysmall $filename;
                
else 
                $toupdatedb
->primarysmall NULL;
                  
//print_r("small sizeifier DIDN'T fire <br />");
            
};

        
// Make Large resize            
        
if (($this->orig_width >= $enlargedimensions) || ($this->orig_height >= $enlargedimensions))
            
{
            
//print_r("large sizeifier fired");
            
$config['new_image'$base_path'large/'.$filename;
            
$config['width'$enlargedimensions
            
$config['height'$enlargedimensions
            
$this->image_lib->initialize($config); 
            if (
$this->image_lib->resize())
            
{
            
//print_r("success large /n <br />");
            
$toupdatedb->primarylarge $filename;
            
else {
            $toupdatedb
->primarylarge NULL;
            
//print_r("large sizeifier DIDN'T fire <br />");
        
}
        
        $this
->db->update('home'$toupdatedb); 
        
        return 
$data
        
}    else    
            
return FALSE;
        

    }
# not sure how to call the following functions to make them work with updateHomeImage using img_lib above
function ResizeImage($newWidth$newHeight)
    
{
        $this
->newWidth $newWidth;
        
$this->newHeight $newHeight;
        
$this->dest_image imagecreatetruecolor($this->newWidth$this->newHeight);
        
$this->ImageSetAlpha();
        
imagecopyresampled($this->dest_image$this->src_image0000,    $this->newWidth$this->newHeightimagesx($this->src_image), imagesy($this->src_image));
        
$this->src_image $this->dest_image;
    
}

function ImageSetAlpha ()
    
{
        $background 
imagecolorallocate($this->dest_image000);
        
ImageColorTransparent($this->dest_image$background);
        
imagealphablending($this->dest_imagefalse);
    

It seems like I need a Resize function that returns a reference to the new resized file so I can store it in my database. Does this mean I need to have a resize function that not only takes the x and y dimensions, but a reference to the file that needs to be resized already sitting on my server?

 Signature 

———-
work: jmx2.com
blog: SuperGeekery
twitter: johnmorton
email: .(JavaScript must be enabled to view this email address)

Profile
 
 
Posted: 13 June 2008 07:22 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  2
Joined  06-13-2008

I believe I have the 24bit PNG transparency issue solved. It requires first creating a 100% transparent color, and then filling the new image before the imagecopyresampled.

I have made a custom library that replaces the image_lib->image_process_gd function and adds the relevant code. Just put it in your system/application/libraries directory and it should be good to go.

My_Image_lib.zip

Unless there is some big performance problem with doing this, it seems like this could easily be integrated into the next CI update.

For those interested, here is the relevant code:

imageAlphaBlending($dst_imgfalse);
imageSaveAlpha($dst_imgtrue);
$trans_colour imagecolorallocatealpha($dst_img000127);
imagefill($dst_img00$trans_colour); 

In my Image_lib it would go right after line 520.

Profile
 
 
Posted: 13 June 2008 07:45 PM   [ Ignore ]   [ # 15 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  116
Joined  10-02-2002

Ornoc,

I appreciate the reply. I’m in the middle of another project right now, but will try to implement your fix as soon as I get back on my CI project. I’ll post about my results then. Thanks.

-John

 Signature 

———-
work: jmx2.com
blog: SuperGeekery
twitter: johnmorton
email: .(JavaScript must be enabled to view this email address)

Profile
 
 
   
1 of 2
1