Adding one image at the bottom of another in PHP

10,008

Solution 1

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

Solution 2

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

Share:
10,008
baklap
Author by

baklap

Yeah!

Updated on June 14, 2022

Comments

  • baklap
    baklap almost 2 years

    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!