1

I am using PHP GD library to create an image over on mockup image first i take mockup image and created a canvas in the center with a width of 250 and a height of 300 and i insert an image in that canvas, it is working fine when a big image comes in because it covers all of the canvas space but problem comes in when a small image comes in like less than canvas size like width of 210 and height of 280 so because it is less than a canvas size it leaves a black space around the image. and also i don't want to reduce canvas size because it is a fixed size. i just want this black color to be transparent. enter image description here

this is my code:

define("WIDTH", 600);
define("HEIGHT", 600);

$dest_image = imagecreatetruecolor(WIDTH, HEIGHT);

$trans_background = imagecolorallocate($dest_image, 255, 255, 255);

imagecolortransparent($dest_image, $trans_background);

imagefill($dest_image, 0, 0, $trans_background);

$design_image = 'captain.png';

$mockup_image = 't-shirt.png';

$a = imagecreatefrompng($design_image);

$b = imagecreatefrompng($mockup_image);


$pos_left = -19.75;

$pos_top = 37.075;

imagecopy($dest_image, $a, 168, 155, $pos_left, $pos_top, 250, 300);


imagecopy($dest_image, $b, 0, 0, 0, 0, WIDTH, HEIGHT);

imagepng($dest_image, 'final_image.jpg');
treyBake
  • 6,440
  • 6
  • 26
  • 57
tabyf5
  • 172
  • 14
  • 2
    Possible duplicate of [imagecreatefrompng() Makes a black background instead of transparent?](https://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent) – j08691 Apr 05 '19 at 13:25
  • 1
    First comment on the associated PHP manual page: https://www.php.net/manual/function.imagecreatetruecolor.php#75298… – Blackhole Apr 05 '19 at 13:28
  • @j08691 no it is not helping! – tabyf5 Apr 05 '19 at 13:42
  • Why are you showing server-side code, and then mention the client-side canvas element? Do these black bars come from the server-side creation of the image, or from how you paint the image onto the canvas afterwards? I would assume the former, seeing that your PHP code always creates a 600*600px image … but then you’d have to _explain_ how the solution from the mentioned possible duplicate does not work, please. – 04FS Apr 05 '19 at 13:52
  • 1
    `imagecopy($dest_image, $a, 168, 155, $pos_left, $pos_top, 250, 300);` this is the line where this canvas is creating. – tabyf5 Apr 05 '19 at 14:00
  • @ShaikhTayab Thank you – Said Erraoudy Nov 16 '21 at 20:47

0 Answers0