我正在尝试在php中合并我的两个图像。一张图片正在上传到我的系统中,另一张是我创建的透明背景图片。这是我的代码。我的代码只显示了一个非图像图标。我不明白我哪里错了。
<?php
//Set the Content Type
header("Content-type: image/png");
#dispaly the image
$file=$_GET['file'];
// echo file_get_contents($file);
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
imagecolortransparent($im, $black);
//text to draw
$text="hello world";
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, $blue, $font, $text);
$dest=imagecreatefrompng($file);
$src=imagecreatefrompng($im);
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 250, 200);
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>发布于 2015-09-01 21:15:07
使用$im而不是$src -正如赛义德指出的那样,imagecreatefrompng以文件名(字符串)作为参数-而不是GD资源。如果$im已经包含可以使用的GD资源,为什么还要设置$src?
imagettftext有一个很重要的部分。如果GD在给定的路径中找不到字体,我可以重现空图标的效果。检查您的位置、权限和字母大小写。此外,如果您决定直接将.ttf文件复制到脚本位置,请参考imagettftext() documentation,因为".ttf“扩展名有重要的警告。
此外,要创建完全透明的图像,请使用:( George Edison在PHP doc for imagefill中
$im = imagecreatetruecolor(317, 196);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);另外,来自Sina Salek的PHP doc for imagecopymerge():imagecopymerge_alpha函数在imagecopymerge()中提供真正的透明性。
所以,我的解决方案是:
<?php
//Set the Content Type
header("Content-type: image/png");
#dispaly the image
$file='test.png';
$im = imagecreatetruecolor(317, 196);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
$blue = imagecolorallocatealpha($im, 0, 0, 255, 0);
//text to draw
$text="hello world";
putenv('GDFONTPATH=' . realpath('.'));
$font = 'lucida';
imagettftext($im, 20, 0, 10, 50, $blue, $font, $text);
$dest=imagecreatefrompng($file);
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge_alpha($dest, $im, 10, 10, 0, 0, 200, 180, 100);
imagepng($dest);
imagedestroy($dest);
imagedestroy($im);
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
?>发布于 2015-09-02 16:02:04
为此,您应该使用imagecopymerge()函数。
查找链接
http://php.net/manual/en/function.imagecopymerge.phphttps://stackoverflow.com/questions/31772036
复制相似问题