php - PNG - preserving transparency -
i'm trying redesign site original square, tile-based rendering of images can more of cutout of image... rid of grid pattern.
here's how looked originally...
here's rough mock-up of i'm going for:
so resaved image thumbnail transparent background... want dog show, , square transparent show site's background underneath.
yet when render on page, has black background.
i've checked css see if there sort of img class, or class rendered comics... or bootstrap see there may background-color being assigned black (and searched hex code 000000), didn't find one...
then found had way thumbnailing script resampling png... since i'm getting black background supposedly transparent image, blame imagecreatetruecolor()
returns image identifier representing black image of specified size.
.
i tried following cheekysoft's question here preserving transparency after resampling... didn't work... 2 main points being:
imagealphablending( $targetimage, false ); imagesavealpha( $targetimage, true );
i found if put $img = imagecreatefrompng($image_file);
before resize/resample it, displays transparently... want... not after resample it.
thumbnailer.php code:
<?php #appreciation goes digifuzz (http://www.digifuzz.net) on $image_file = $_get['img']; //takes in full path of image $max_width = $_get['mw']; $max_height = $_get['mh']; global $img; //check image if(!$image_file || $image_file == "") { die("no file."); } //if no max width, set 1 if(!$max_width || $max_width == "") { $max_width="100"; } //if no max height, set 1 if(!$max_height || $max_height == "") { $max_height = "100"; } $img = null; //create image file 'img' parameter string if( preg_match('/\.jpg$/',$image_file) || preg_match('/\.jpeg$/',$image_file) ){ $img = imagecreatefromjpeg($image_file); } else { $img = imagecreatefrompng($image_file); } //if image loaded... if($img) { //get image size , scale ratio $width = imagesx($img); $height = imagesy($img); //takes min value of these 2 $scale = min($max_width/$width, $max_height/$height); //if desired new image size less original, output new image if($scale < 1) { $new_width = floor($scale * $width); $new_height = floor($scale * $height); $tmp_img = imagecreatetruecolor($new_width, $new_height); //copy , resize old image new image imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //replace actual image new image $img = $tmp_img; } } //set content type header header('content-type: image/png', true); imagealphablending( $img, false ); imagesavealpha( $img, true ); imagepng($img); imagedestroy($img); ?>
can help?
thanks!
you need call imagealphablending() on destination image before resample/resize image:
//copy , resize old image new image imagealphablending($tmp_img, false); imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // ...
Comments
Post a Comment