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 17, 2012, 22:16
Post: #2
RE: Resizing a .png graphic and losing transparancy
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);"

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Aug 18, 2012, 06:48
Post: #3
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
  change the default export setting of Untitled.png ezz9 2 222 Jun 9, 2017 16:31
Last Post: ezz9
  Losing image quality Futant 2 278 Oct 10, 2014 16:07
Last Post: rich2005
  Exporting Multiple Layers Into Images (PNG, JPG, etc) SavingMultipleImages 22 10,762 Oct 6, 2014 10:59
Last Post: rich2005
  Question for all graphic veterans with 8/16-bit roots VictorJP8 7 498 Aug 16, 2014 07:32
Last Post: VictorJP8
  Losing Mouse control no matter what version. Dead2Fall 9 554 Jul 23, 2014 09:22
Last Post: Mont33

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS