James James - 2 months ago 9
PHP Question

PHP imagecreatetruecolor fix antiailas issues

I am having issue overlaying images. The issue im having is that there is the code damages the edges of the devices with a poor black pixelated border. Below is my code. Is there anything I can do to my code to improve the quality.

$src1 = imagecreatefromstring( file_get_contents( $_FILES['image']['tmp_name'] ) );
$size1 = getimagesize( $_FILES['image']['tmp_name'] );
$dst1 = imagecreatetruecolor( 696, 400 );

imagecopyresampled( $dst1, $src1, 0, 0, 0, 0, 696, 399, $size1[0], $size1[1] );
imagedestroy( $src1 );

$width = 1200;
$height = 687;

$image = imagecreatetruecolor($width, $height);

imagecopyresampled($image, imagecreatefrompng('images/main.png'),0,0,0,0,$width,$height,$width,$height);
imagecopy($image, $dst1, 333, 103, 0, 0, 696, 399);

$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagesavealpha($image, true);
imagefill($image, 0, 0, $transparency);

imagedestroy( $dst1 );
imagedestroy( $dst2 );
imagedestroy( $dst3 );
imagedestroy( $dst4 );

ob_start();
imagepng($image, NULL, 0);
$size = ob_get_length();
header("Content-Length: " . $size);
header("Cache-Control: private");
header("Content-Type: image/png");
header("Content-Disposition: attachment; filename=test.png");

Answer

Move this ...

$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagesavealpha($image, true);
imagefill($image, 0, 0, $transparency);

To right below ...

$image = imagecreatetruecolor($width, $height);