Link to home
Start Free TrialLog in
Avatar of kbit
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
	
$('#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;
});

Open in new window


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);

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbit
kbit

ASKER

Nice code Ray, thanks very much for that, this works beautifully!
Great!  Thanks for the points and thanks for using E-E!