homepage Welcome to WebmasterWorld Guest from 54.161.246.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Resize PNG and convert to jpeg - PNG32 gets a black backgroun
everything but png32s seem to work
nigelt74

5+ Year Member



 
Msg#: 4267662 posted 11:33 pm on Feb 15, 2011 (gmt 0)

Hi all

I have a project where i need to grab various images from a directory, resize them to a specific width and convert to jpeg then crop them to another size.

I have a script I have mashed together, which works with everything i have tried, except PNG32.

Unfortunately the person who this script is for uploads whatevr images their supplier sends them, and recently is seems to be PNG32s

When this runs through my script, it comes out with a black background.
Png8s and gifs all come through fine, this seems to be the part of the script where png32s are dealt with
or should I say the bit after the "elseif"


if ( ($this->mime == 'image/gif') || ($this->mime == 'image/png') ) {
$trnprt_indx = imagecolortransparent($this->src);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
echo 'tosspot7';// this works for gifs and png 8
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($this->src, $trnprt_indx);

// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($this->tmp, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

// Completely fill the background of the new image with allocated color.
imagefill($this->tmp, 0, 0, $trnprt_indx);

// Set the background color for new image to transparent
imagecolortransparent($this->tmp, $trnprt_indx);
}


// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($this->mime == 'image/png') {
echo 'tosspot3'; // PNG32 comes here bit fails

// Turn off transparency blending (temporarily)
imagealphablending($this->tmp, false);

// Create a new transparent color for image
// $color = imagecolorallocatealpha($this->tmp, 0, 0, 0, 127);
$color = imagecolorallocatealpha($this->tmp, 255, 255, 255, 127);

// Completely fill the background of the new image with allocated color.
imagefill($this->tmp, 0, 0, $color);

// Restore transparency blending
imagesavealpha($this->tmp, true);
}
}



as always anyhelp would be appreciated

 

nigelt74

5+ Year Member



 
Msg#: 4267662 posted 12:26 am on Feb 16, 2011 (gmt 0)

Ok got it working, one line was missing from a script I copied
basically the last 4 lines above

// Restore transparency blending
imagesavealpha($this->tmp, true);
}
}

should be

// Restore transparency blending
imagesavealpha($this->tmp, true);
ImageAlphaBlending($this->tmp, true);
}
}

The comment mentioned it but somehow it didn't copy across.

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4267662 posted 1:43 pm on Feb 16, 2011 (gmt 0)

Yep, that certainly makes it more difficult to troubleshoot. Glad you caught it!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved