【问题标题】:Adding one image at the bottom of another in PHP在 PHP 中将一个图像添加到另一个图像的底部
【发布时间】:2010-11-27 23:32:02
【问题描述】:

我想在 php 中将一张图片添加到另一张图片的底部

我有这个来加载图像:

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

现在我想将它们添加到一张图片中并一起显示顶部和底部。

我该怎么做?

谢谢!

【问题讨论】:

    标签: php gd


    【解决方案1】:

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

    【讨论】:

    • 但是如果我想将图像与 alpha 合并?现在第二张具有相同尺寸的图像与第一张图像重叠
    【解决方案2】:

    要实现这一点,您必须 a) 合并图像并将结果存储在文件中 b) 生成一个合适的标签来指向它。 c) 避免再次使用该文件名,直到该人离开。

    如果您只想合并两张图片,请使用图片魔法。

    如果您经常想在一张下方显示两张图片,请使用合适的 html 来执行此操作,并让浏览器执行此操作。

    例如把图片放在一个

    您以正常方式使用 php 生成的。 (这比让标签出现在这里更容易:)

    【讨论】:

    • 这听起来根本不像他所要求的......在不知道他的用例的情况下,这可能不是一个可行的解决方案。我猜他想为 CSS sprites 做这件事。
    【解决方案3】:
    $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);
    

    【讨论】:

    • 可能不是这个问题的答案,但这不值得一票否决,因为如果您希望两个图像彼此重叠,这就是答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2012-06-01
    相关资源
    最近更新 更多