Adding one image at the bottom of another in PHP

后端 未结 3 1721
终归单人心
终归单人心 2021-01-01 07:20

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         


        
相关标签:
3条回答
  • 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');
    
    0 讨论(0)
  • 2021-01-01 08:09

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

    0 讨论(0)
  • 2021-01-01 08:15
    $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 讨论(0)
提交回复
热议问题