-2

I have a transparent logo in a png file. I'm trying to create a transparent box image which would be the background for the logo. So the final image would be a merge of the programmatically-created, transparent png and the transparent logo from the file. The code that I'm using works, however the results show the logo with fuzzy black edges. Any ideas on how to get a sharper image?

Here is my code:

$source=imagecreatefrompng($logoFile);
imagealphablending($source, false);
imagesavealpha($source, true);   
$newImage = imagecreatetruecolor(450,280);
$black = imagecolorallocate($newImage, 0, 0, 0);
imagecolortransparent($newImage, $black);
imagecopyresampled($newImage,$source,0,0,$x,$y,450,280,$width,$height);
imagepng($newImage,$imageDirectory,0); 
imagedestroy($newImage);

Edited Sorry, I guess I should've been more clear. The image file that is coming in is from a file upload form field. And most of the files are organization logos. Some of the .png files include a transparency alpha channel(they're not completely transparent). The reason that I have to recreate it is because I'm also using cropper.js, which is a tool that lets you zoom in or crop the file being uploaded, and I need the final file to be 450x280 regardless of how big or small the file uploaded was. So in my code, $x, $y, $width, $height are sent by cropper.js after the user has manipulated the original image.

As I said, the image manipulation is working correctly. The only problem is that when I user submits a png with transparency, the logo is real fuzzy, with black edges. When I change black to another color in $black = imagecolorallocate($newImage, 0, 0, 0); I loose all of the transparency.

Here is a sample input and output file. The one on the left is the original image, the one on the right is the output. The output of this test image isn't as bad as real images. There's way more fuzziness on real images.

enter image description here

chavab_1
  • 237
  • 2
  • 10
  • 2
    An example input and output file would probably be useful. – Sammitch Apr 19 '18 at 01:18
  • Also, try `imagecopy()` rather than `imagecopyresampled()` seeing as you're not resizing anything there's no need to resample it, and I could see that causing fuzziness. – Sammitch Apr 19 '18 at 01:20
  • 1
    what is a transparent logo? I have never heard of a transparent logo. well if it's transparent, it's nothing in alpha channel. – Wils Apr 19 '18 at 01:44
  • 1
    If you change `$black` to something else, do you still get "fuzzy *black* edges"? Or, if you change it to purple, do you get 'fuzzy purple edges'? – Jongware Apr 19 '18 at 08:34
  • @Sammitch I tried using `imagecopy()` but I still got the same results. – chavab_1 Apr 19 '18 at 16:59
  • @Wils Sorry, I wasn't aware I was dealing with 2nd graders. The logo isn't fully transparent; parts of it are transparent as it has an alpha channel. Funny, how everyone else new what I meant. – chavab_1 Apr 19 '18 at 17:01
  • @usr2564301 If I change black to any other color the background is no longer transparent. Thanx – chavab_1 Apr 19 '18 at 17:03
  • 2
    FYI: the first part that second-last comment is the precise moment you decided your question wasn't going to get answered. – Sammitch Apr 19 '18 at 17:48
  • I found the problem. First off I tried to use `imagecopymerge` to see if I got a better result. I read the [documentation](http://php.net/manual/en/function.imagecopymerge.php) and Rodrigo's post on it really helped out. I tried to use his function but then the entire transparency was gone. That's when I realized that my server's GD version is preventing `imagecreatetruecolor` from working correctly. No matter what color I use I always end up with black. I updated GD and everything looks way better. – chavab_1 Apr 19 '18 at 21:59
  • FYI: @Sammitch Thanx for your help and advise. But I thought these forums were for people trying to learn or teach others. What's the point of coming here just to be a "Grammar Police". If someone doesn't have something productive or helpful to post why come on here just to be a smart ass? And I don't mean you, I mean the other person. Once again, thanx!! – chavab_1 Apr 19 '18 at 22:08
  • Can someone please explain why my "question" is getting downvoted? Several other people have asked similar questions when they couldn't get any transparency at all. My problem was that ,although I was getting transparency, I was getting fuzziness around the edges of characters or images. It would help out to know why my "question" is not up to par? – chavab_1 Apr 23 '18 at 17:28

1 Answers1

0

I found the problem. First off I tried to use imagecopymerge to see if I got a better result. I read the documentation and Rodrigo's post on it really helped out. I tried to use his function but then the entire transparency was gone. That's when I realized that my server's GD version is preventing imagecreatetruecolor from working correctly. No matter what color I use I always end up with black. I updated GD and everything looks way better.

chavab_1
  • 237
  • 2
  • 10