baklap
baklap

Reputation: 2173

Adding one image at the bottom of another in PHP

I'd like to add one image to the bottom of another in php

I've this to load the images:

//load top
$top = @imagecreatefrompng($templateTop);
//load bottom
$bottom = @imagecreatefrompng($templateBottom);

Now I'd like to add them to one picture and display top and bottom together.

What way can I do this?

Thanks!

Upvotes: 3

Views: 10717

Answers (3)

Muhammad Azeem
Muhammad Azeem

Reputation: 1159

$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";

$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);


// Place "photo_to_paste.png" on "white_image.png"
imagecopy($im, $im2, 20, 10, 0, 0, imagesx($im2), imagesy($im2));

// Save output image.
imagepng($im, "output.png", 0);

Upvotes: 1

Ian
Ian

Reputation: 2021

To achieve this you would have to a) Combine the image and store the result in a file b) generate a suitable tag to point to it. c) Avoid using that filename again, until that person had left.

If you want to combine two images just once, then use image magic.

If you frequently want to display two images one under the other, do so using suitable html, and let the browser do it.

E.g. Put the images in a

<div><div><img.../></div><div><img .../></div></div> 

which you generate with php in the normal way. (Which is easier than getting tags to appear here :)

Upvotes: 1

netcoder
netcoder

Reputation: 67695

Use imagecopy:

$top_file = 'image1.png';
$bottom_file = 'image2.png';

$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);

// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);

// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;

// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);

// save to file
imagepng($new, 'merged_image.png');

Upvotes: 19

Related Questions