Welcome to WebmasterWorld Guest from 54.147.10.72

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Resize PNG and convert to jpeg - PNG32 gets a black backgroun

everything but png32s seem to work

     

nigelt74

11:33 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



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

12:26 am on Feb 16, 2011 (gmt 0)

10+ Year Member



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

1:43 pm on Feb 16, 2011 (gmt 0)

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



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

Featured Threads

Hot Threads This Week

Hot Threads This Month