开发者

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

开发者_StackOverflow中文版Now I'd like to add them to one picture and display top and bottom together.

What way can I do this?

Thanks!


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


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


$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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜