10

我想在 php 站点中的字母(另存为 jpg 文件)底部插入一个签名(另存为 png 文件)。我用过imagecopymerge,但它会创建一个黑色图像文件而不是我的请求。我也使用了这段代码,但没有结果。

function merge($filename_x, $filename_y, $filename_result) {

    list($width_x, $height_x) = getimagesize($filename_x);
    list($width_y, $height_y) = getimagesize($filename_y);

    $image = imagecreatetruecolor($width_x + $width_y, $height_x);

    $image_x = imagecreatefromjpeg($filename_x);
    $image_y = imagecreatefromgif($filename_y);

    imagecopy($image, $image_x, 0, 20, 30, 50, $width_x, $height_x);
    imagecopy($image, $image_y, $width_x, 0, 10, 0, $width_y, $height_y);

    imagejpeg($image, $filename_result);

    imagedestroy($image);
    imagedestroy($image_x);
    imagedestroy($image_y);
}

merge('myimg.jpeg', 'first.gif', 'merged.jpg');
4

4 回答 4

1

这个功能对我有用。由于我没有看过你的图片,我可以告诉你我用什么来测试它。

  • bg.jpg = 400X400 jpg
  • fg.gif = 200X200 gif (透明背景)

function merge($filename_x, $filename_y, $filename_result) {
  list($width_x, $height_x) = getimagesize($filename_x);
  list($width_y, $height_y) = getimagesize($filename_y);

  $image = imagecreatetruecolor($width_x, $height_x);

  $image_x = imagecreatefromjpeg($filename_x);
  $image_y = imagecreatefromgif($filename_y);

  imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
  imagecopy($image, $image_y, 0, 0, 0, 0, $width_y, $height_y);

  imagejpeg($image, $filename_result);

  imagedestroy($image);
  imagedestroy($image_x);
  imagedestroy($image_y);
}

merge('bg.jpg', 'Untitled.gif', 'merged.jpg');

这似乎工作正常。我假设您可能遇到了一些定位问题。在起始位置 0 尝试一切,然后开始移动,直到获得所需的效果。

于 2013-09-18T21:32:52.827 回答
1

请试试这个功能,我已经定制了你的。

function merge($filename_x, $filename_y, $filename_result) {
    $source = imagecreatefromjpeg($filename_x);
    $tobeMerged = imagecreatefromgif($filename_y);

    //add signature on bottom right
    imagecopymerge($source, $tobeMerged, imagesx($source) - imagesx($tobeMerged), imagesy($source) - imagesy($tobeMerged), 0, 0, imagesx($tobeMerged), imagesy($tobeMerged), 100);
    //save your merged image
    imagejpeg($source, $filename_result);

    //destroy image resources to free memory
    imagedestroy($source);
imagedestroy($tobeMerged);
}
merge('myimg.jpeg', 'first.gif', 'merged.jpg');
于 2013-09-05T23:23:50.263 回答
0

您是否能够运行命令行工具(例如通过 exec)?如果是这样,imagemagick命令行工具几乎可以完成您需要的任何图像处理。分层功能听起来像您所追求的:

echo exec('composite -geometry  +5+10 image1.jpg image2.png image2.png');
于 2013-05-21T20:28:14.740 回答
0

您的 gif 可能有一个调色板,并且不是真彩色图像。如果您的 php 版本是 5+,请检查 imageistruecolor,以防万一使用 imagepalettetotruecolor。

于 2013-08-13T16:43:00.930 回答