Jump to content

Crop JPG to PNG


etrader

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.