kbit
asked on
JCrop black background on transparent PNG files
Hi,
I managed to get JCrop to work with PHP. All is good except that when cropping PNG files with transparent backgrounds, the cropped image has a black background. Can someone please advise how to fix this? Many thanks
In the code below, I've tried changing the bgcolor to white and transparent, I've also tried changing the bgopacity to 0
And the PHP:
I managed to get JCrop to work with PHP. All is good except that when cropping PNG files with transparent backgrounds, the cropped image has a black background. Can someone please advise how to fix this? Many thanks
In the code below, I've tried changing the bgcolor to white and transparent, I've also tried changing the bgopacity to 0
$('#thumbnail').Jcrop({
trueSize: [<?php echo $current_large_image_width; ?>,<?php echo $current_large_image_height; ?>],
onSelect: showCoords,
bgColor: '',
bgOpacity: .4,
setSelect: [ 125, 100, 650, 520 ],
aspectRatio: 550 / 420,
},function(){
jcrop_api = this;
});
And the PHP:
$src_img=imagecreatefrompng($img_name);
imagesavealpha($src_img, true); //saving transparency
// we create a new image with the new dimensions
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
//these 2 lines must be here in order to maintain transparency
imagealphablending( $dst_img, false );
imagesavealpha( $dst_img, true );
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); //seting transparent background
imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent);
// resize the big image to the new created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// output the created image to the file. Now we will have the thumbnail into the file named by $filename
imagepng($dst_img,$filename);
Please post a link to one of the images that is giving you a problem. I believe I have seen this before and solved the problem, but I'd like to test before I post the code. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice code Ray, thanks very much for that, this works beautifully!
Great! Thanks for the points and thanks for using E-E!