Post Reply 

Resizing a .png graphic and losing transparancy

Aug 17, 2012, 21:24 (This post was last modified: Aug 17, 2012 21:28 by JiminSA.)
Post: #1
Resizing a .png graphic and losing transparancy
I am using the following code to upload and resize graphic files.
PHP Code:
function ResizeGraphicWidth($srcext$srcfile$destfile$destwidth 360
    { 
        global 
$allowedexts

        
$srcext strtolower($srcext); 
        
$destext ExtractFileExtension($destfile); 
        
$destext strtolower($destext); 
        if (!isset(
$allowedexts[$srcext]) || !isset($allowedexts[$destext]))  return false

        
// Load file. 
        
if ($srcext == "jpg" || $srcext == "jpeg")  $src imagecreatefromjpeg($srcfile); 
        else if (
$srcext == "png")  $src imagecreatefrompng($srcfile); 
        else  
$src imagecreatefromgif($srcfile); 
        
$info getimagesize($srcfile); 
        
$srcwidth $info[0]; 
        
$srcheight $info[1]; 
        if (
$srcwidth <= $destwidth
        { 
            
$destwidth $srcwidth
        } 
        
// Create and copy into destination. 
        
$destheight = ($srcheight $srcwidth) * $destwidth
        
$tmp imagecreatetruecolor($destwidth$destheight); 
        
imagecopyresampled($tmp$src0000$destwidth$destheight$srcwidth$srcheight); 
        switch (
$destext
        { 
        case 
"jpg"
            
// JPEG doesn't improve much beyond 80%. 
            
imagejpeg($tmp$destfile80); 
            break; 
        case 
"jpeg"
            
// JPEG doesn't improve much beyond 80%. 
            
imagejpeg($tmp$destfile80); 
            break; 
        case 
"png"
            
imagepng($tmp$destfile9); 
            break; 
        default: 
            
// Must be .gif 
            
imagegif($tmp$destfile); 
            break; 
        } 

        
// Cleanup. 
        
imagedestroy($src); 
        
imagedestroy($tmp); 

        return 
true
    } 

Using this php function I wrote to upload and resize .png files which have transparency they lose that transparency, probably because the image mode moves from indexed to RGB!
Can anyone offer advice as to what I can do to prevent this, or is it another incurable?
I have asked this in your forum because when it comes to graphics questions, where better?
Find all posts by this user
Quote this message in a reply
Aug 18, 2012, 06:48
Post: #2
RE: Resizing a .png graphic and losing transparancy
(Aug 17, 2012 22:16)ofnuts Wrote:  Normally, this forum is for Gimp questions...

This said, the first link you get by googling on "php imagecreatetruecolor transparent" hints that you need to add "imagesavealpha($tmp, true);"

Thank you for pointing me in the right direction and yes, I realise that this forum is intended for Gimp orientated discussion, but as previously stated
Quote:I have asked this in your forum because when it comes to graphics questions, where better?
No offence intendedBlush
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  PNG HELP ajbuccieri 1 168 Nov 8, 2018 09:03
Last Post: paynekj
  The Right XP-Pen graphic tablet for Gimp/windows 10 ruijin 1 269 Sep 11, 2018 08:07
Last Post: paynekj
  After exporting into png-white area/background petweaver 1 343 Feb 5, 2018 09:02
Last Post: paynekj
  RAW to PNG issues with levels grandkodiak 1 415 Nov 4, 2017 23:45
Last Post: paynekj
  change the default export setting of Untitled.png ezz9 2 647 Jun 9, 2017 16:31
Last Post: ezz9

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS