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
  After exporting into png-white area/background petweaver 1 176 Feb 5, 2018 09:02
Last Post: paynekj
  RAW to PNG issues with levels grandkodiak 1 291 Nov 4, 2017 23:45
Last Post: paynekj
  change the default export setting of Untitled.png ezz9 2 441 Jun 9, 2017 16:31
Last Post: ezz9
  Losing image quality Futant 1 439 Oct 10, 2014 16:07
Last Post: rich2005
  Exporting Multiple Layers Into Images (PNG, JPG, etc) SavingMultipleImages 17 11,495 Oct 6, 2014 10:59
Last Post: rich2005

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS