Merge Two Png Images with PHP Gd Library

Merge two PNG images with PHP GD library

$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150
imagecopymerge($image1, $image2, 0, 0, 75, 75, 150, 150, 50);

this should be all you need. $image1 should hold the merged image where image2 has been overlayed with 50% opacity. the last argument is the alpha of the merged copy.

http://php.net/manual/en/function.imagecopymerge.php

PHP GD library, turn 2 images into 1 side by side

<?php

$img1_path = 'images_1.png';
$img2_path = 'images_2.png';

list($img1_width, $img1_height) = getimagesize($img1_path);
list($img2_width, $img2_height) = getimagesize($img2_path);

$merged_width = $img1_width + $img2_width;
//get highest
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height;

$merged_image = imagecreatetruecolor($merged_width, $merged_height);

imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

$img1 = imagecreatefrompng($img1_path);
$img2 = imagecreatefrompng($img2_path);

imagecopy($merged_image, $img1, 0, 0, 0, 0, $img1_width, $img1_height);
//place at right side of $img1
imagecopy($merged_image, $img2, $img1_width, 0, 0, 0, $img2_width, $img2_height);

//save file or output to broswer
$SAVE_AS_FILE = TRUE;
if( $SAVE_AS_FILE ){
$save_path = "your target path";
imagepng($merged_image,$save_path);
}else{
header('Content-Type: image/png');
imagepng($merged_image);
}

//release memory
imagedestroy($merged_image);

?>

try it

Merge two images with GD Library, have 1 repeat in background

$overlay = imagecreatefrompng('/path/to/transparent/image.png');
$repeating = imagecreatefrompng('/path/to/repeating/image.png');

// create a new image matching overlay size
$w = imagesx($overlay);
$h = imagesy($overlay);
$output = imagecreatetruecolor($w, $h);
imagealphablending($output, true);
imagesavealpha($output, true);

// tile repeating image on it
imagesettile($output, $repeating);
imagefill($output, 0, 0, IMG_COLOR_TILED);
imagedestroy($repeating);

// now add overlay on top
imagecopy($output, $overlay, 0, 0, 0, 0, $w, $h);
imagedestroy($overlay);

// send to screen
header('Content-Type: image/png');
imagepng($output);
imagedestroy($output);

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).

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');


Related Topics



Leave a reply



Submit