etrader Posted July 21, 2011 Share Posted July 21, 2011 I want to crop a circle in the middle of a photo. To have transparency of the image around the main circle, I need to save it as PNG. Is it possible to write a php code to 1. crop a full circle in the middle of a JPG file 2. then save it with png transparency to show the main circle only? Link to comment https://forums.phpfreaks.com/topic/242540-crop-jpg-to-png/ Share on other sites More sharing options...
WebStyles Posted July 21, 2011 Share Posted July 21, 2011 I have done that before, a very long time ago, so I can't remember the specifics. I do remember that I had to have a png file with just the circle and the transparency to use as a template, and then I would open the jpeg, merge it with the png and save again. I'll have a look around my hard drives to see if I can find my code. Link to comment https://forums.phpfreaks.com/topic/242540-crop-jpg-to-png/#findComment-1245603 Share on other sites More sharing options...
WebStyles Posted July 21, 2011 Share Posted July 21, 2011 FOUND IT. //open image **************************************** $image = imagecreatefrompng("client_piece".$i.".png"); // open piece with pixel mapping $piece = imagecreatefrompng("piece".$i.".png"); // try merging pixel by pixel and list all colors: $fuschia = imagecolorallocate($piece,255,0,255); for($x=0;$x<$width[$i];$x++){ for($y=0;$y<$height[$i];$y++){ if(imagecolorat($piece,$x,$y)!=$fuschia){ imagecopy($image,$piece,$x,$y,$x,$y,1,1); } } } // Grab pink color just to be sure: $pink = imagecolorat($image,0,0); imagecolortransparent($image,$pink); // write to disk imagepng($image,"client_piece".$i.".png"); // free memory imagedestroy($image); what that is doing is that my template image has bright pink representing the transparency (imagecolorat($image,0,0);) grabs that color and uses it as a reference to set the new image's alpha channel. imagecolortransparent($image,$pink); hope this helps Link to comment https://forums.phpfreaks.com/topic/242540-crop-jpg-to-png/#findComment-1245605 Share on other sites More sharing options...
etrader Posted July 21, 2011 Author Share Posted July 21, 2011 Thanks WebStyles, you gave me a brilliant idea Link to comment https://forums.phpfreaks.com/topic/242540-crop-jpg-to-png/#findComment-1245743 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.