Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中在图像底部插入签名?_Php - Fatal编程技术网

如何在php中在图像底部插入签名?

如何在php中在图像底部插入签名?,php,Php,我想在php站点中的一封信(另存为jpg文件)的底部插入一个签名(另存为png文件)。 我使用了imagecopymerge,但它创建了一个黑色图像文件,而不是我的请求。 我也使用了这个代码,但没有结果 function merge($filename_x, $filename_y, $filename_result) { list($width_x, $height_x) = getimagesize($filename_x); list($width_y, $height_

我想在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');

您是否能够运行命令行工具(如通过exec)?如果是这样的话,命令行工具可以执行您需要的任何图像操作。这听起来像是你想要的:

echo exec('composite -geometry  +5+10 image1.jpg image2.png image2.png');

您的gif可能有调色板,而不是真彩色图像。如果您的php版本为5+请使用imageistruecolor检查,如果是,请使用ImagePaletteTorueColor

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

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

这个函数适合我。因为我没有看到你的图片,我可以告诉你我用什么来测试它

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


这似乎很有效。我想你可能有一些定位问题。在起始位置0尝试所有操作,然后开始移动,直到获得所需效果。

结果如何?图像是否存在于当前路径(源文件所在的路径)中?您是否确保路径正确且文件可访问?您是否尝试过如示例所示设置标题?这不是答案,不是吗?请对此使用注释。更新的答案可实际提供答案。:-)
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');