How to Merge Transparent Png with Image Using PHP

Combine 2-3 transparent PNG images on top of each other with PHP

$image_1 = imagecreatefrompng('image_1.png');
$image_2 = imagecreatefrompng('image_2.png');
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 100, 100);
imagepng($image_1, 'image_3.png');

How to merge transparent PNG with image using PHP?

You can merge the two images together using the PHP GD2 library.

Example:

<?php
# If you don't know the type of image you are using as your originals.
$image = imagecreatefromstring(file_get_contents($your_original_image));
$frame = imagecreatefromstring(file_get_contents($your_frame_image));

# If you know your originals are of type PNG.
$image = imagecreatefrompng($your_original_image);
$frame = imagecreatefrompng($your_frame_image);

imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100);

# Save the image to a file
imagepng($image, '/path/to/save/image.png');

# Output straight to the browser.
imagepng($image);
?>

Merging multiple transparent PNG images with php

I think you have to use imagesavealpha(): http://www.php.net/manual/en/function.imagesavealpha.php

Info: "You have to unset alphablending (imagealphablending($im, false)), to use it."

php merge transparent png image to jpeg preventing the white background

Use imagecopy

imagecopy($dest_image, $src, ($offset + 250), $offset, 0, 0, imagesx($src),imagesy($src));

How merge image on background png image in php

You are copying a JPEG image over the background image.

JPEG doesn't support transparency.

What you could do with the gd library is:

  • Create a new result image of the desired size, then
  • Copy the JPEG (user picture) to its center, then
  • Copy the partially-transparent PNG background (actually foreground) over result image. The PNG background must have a "transparent window" in the middle so that the user picture doesn't get hidden behind the background (in other words, the white circle part of the background must be transparent).


Related Topics



Leave a reply



Submit