Jump to content

Crop JPG to PNG


etrader

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.