douba9776
douba9776
2012-07-18 07:10
浏览 47
已采纳

将图像合并在一起并没有真正发挥作用

I can't seem to merge 2 images for some reason and I have no clue what I'm doing wrong. This is still a bit new to me. here is the code I have:

$source = 'http://localhost:8888/develop/trunk/develop/wp-content/uploads/2012/07/card01-80x80.jpg';
$im = imagecreatetruecolor(200, 200);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
$text_color = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagejpeg($im, WP_UPLOADS_PATH . 'post-image-generator/1.jpg');


//merge images
$thumb = imagecreatefromjpeg($source);
$destination = imagecreatefromjpeg(WP_UPLOADS_PATH . 'post-image-generator/1.jpg');


imagecopymerge($destination, $thumb, 0, 0, 0, 0, 0, 0, 100);


imagejpeg($destination, WP_UPLOADS_PATH . 'post-image-generator/1.jpg');

echo "
    <style></style>
    <img src='" . WP_UPLOADS_URL . 'post-image-generator/1.jpg' . "' />
        ";


imagedestroy($thumb);
imagedestroy($destination);
imagedestroy($im);

It generates the following:

generated image

but should also include the following image:

missing image

Any help is appreciated

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqin1075
    dongqin1075 2012-07-18 08:07
    已采纳

    As far as I can tell without actually testing it, it's this:

    imagecopymerge($destination, $thumb, 0, 0, 0, 0, 0, 0, 100);

    The area you're copying from starts at 0,0 in $thumb, and the width and height of the area to be copied is 0 and 0. Since the source picture is 80x80 and you want the whole thing to be copied to the top left corner of the first image, you'd need to change it to:

    imagecopymerge($destination, $thumb, 0, 0, 0, 0, 80, 80, 100);

    This copies the whole 80-pixel image to the top left corner of $destination.

    点赞 评论

相关推荐