1

Borrowed this code to circle crop an image output.

// create the transparent circle image
$filename         = APP_WEB_PATH."img/user/".$_GET['img'].".jpg";
$imagefilenamepng = APP_WEB_PATH."img/user/".$_GET['img']."_c.png";

$image_s    = imagecreatefromstring(file_get_contents($filename));
$width      = imagesx($image_s);
$height     = imagesy($image_s);
$newwidth   = 300;
$newheight  = 300;

$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);

$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);

$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);

imagepng($image,$imagefilenamepng);
imagedestroy($image);
imagedestroy($mask);

The image looks good and transparent and even when opened with a photo editor

enter image description here

but when used inside an image using php GDI i get a black border instead of transparent

enter image description here

what am I missing here.. (if I use paint.net image editor and re-save the image, then it works fine..) the below image works fine after saving in the photo editor

enter image description here

I use the code to call it as

self::$im = imagecreatefromjpeg($imgname1); 
$im2 = imagecreatefrompng($imgname2); 
$sx = imagesx($im2)*$scale; 
$sy = imagesy($im2)*$scale; 
$stamp = imagescale($im2, $sx); 
imagecopy(self::$im, $stamp, $xcord, $ycord, 0, 0, $sx, $sy); 

here self::$im is the larger image, and $im2 is the one that is cropped

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • every layer needs a imagealphablending call https://stackoverflow.com/questions/6109832/php-gd-create-a-transparent-png-image/6110100#6110100 – Lawrence Cherone Dec 06 '22 at 12:47
  • You probably mean that merging the two images in www.getpaint.net works, whereas the merging in PHP doesn't. Transparancy is trickly. You need to tell PHP that's the image is transparent, and you can't save the image as a JPG, it has to be a PNG. In your code you don't set the transparency of `self::$im`. You also don't show how you render the output. – KIKO Software Dec 06 '22 at 12:48
  • self::im doesnt need to be transparent. its the mother image, where the cropped image is being inserted. – Vinod Devasia Dec 06 '22 at 12:57
  • I doubt it...more likely the inserted image has no proper transparency, and therefore you can't see through it to the background "mother" image. – ADyson Dec 06 '22 at 13:11
  • and that is why i have posted here to fix this transparency issue.. :) – Vinod Devasia Dec 06 '22 at 13:19
  • I checked your code, by actually executing it, and you're right: The image is not saved with an alpha channel. Let me see if I can find out why. Hmm, that code looks weird. I also [found the page](https://thedebuggers.com/transparent-circular-crop-using-php-gd/) which uses your code. – KIKO Software Dec 06 '22 at 13:52
  • yes thats the code.. https://thedebuggers.com/transparent-circular-crop-using-php-gd/ – Vinod Devasia Dec 06 '22 at 14:24
  • OK, I tried several things, and several other examples, which are supposed to work, and I can't get it to work either. I can't spent more time on this now, it is a mystery. Transparency works for me in other code, just not with a mask like this. Somebody will have a bright idea, hopefully, and come up with a solution. – KIKO Software Dec 06 '22 at 14:36
  • okay found one more thing here.. this only happens when i try to scale the object.. if i use the same size it works without any problem – Vinod Devasia Dec 06 '22 at 18:59

0 Answers0