php组合图片代码,php gd库 文字与图片组合(类似水印)

最近需要实现一个类似与美图秀秀 测试个人超能力的功能 网址:8.app.meitu.com

其中有2个难点,1 新浪微博api(见上文) 2 文字图片结合

文字图片结合:

文字to图片:

1 乱码问题

a.php版本问题 5.3.0 5.3.1 都有这方面bug , 5.3.2 没问题  在编译是要把日文编译的去掉 (没有亲自时间过,不过有道理,一样的程序,家里执行不了,单位里就能用)

b. 字符串没有设置utf-8。

c. 字体问题,下载 simhei.ttf 黑体 font type file

操作方法:1 可以直接另存为文件为utf-8

2 ansi文件 用函数转 iconv('gb2312','utf-8',$words)  gb2312 -》 utf-8

2文字图片叠加,推荐一个很不错的php函数

header("Content-type: text/html; charset=gb2312");

/*

参数:

1.$bgimg : 背景图片

2.$x : 距离左边的距离

3.$topy : 距离顶部的距离

4.$font : 要添加的字符串

5.$size : 字体的大小

6.$fonttype : 字体

7.$color : 字体颜色

8.$tofile: 生成的图片

9.$nameyycolor : 字符串的阴影颜色

10.$nameyysize : 阴影的宽度

11.$fontfile : 字体文件

*/

function addchartopic( $bgimg, $x, $topy, $font, $size, $fonttype, $color, $tofile, $nameyycolor, $nameyysize, $fontfile, $type )

{

$r = hexdec( substr( $color, 0, 2 ) );

/*hexdec()函数把十六进制转换为十进制。*/

$g = hexdec( substr( $color, 2, 2 ) );

$b = hexdec( substr( $color, 4, 2 ) );

if($type == 'jpg'){

$im = imagecreatefromjpeg( $bgimg );

}elseif($type == 'gif'){

$im = imagecreatefromgif( $bgimg );

}else{

return false;

}

/*ImageCreateFromPNG本函数用来取出一张 PNG格式图形,通当取出当背景或者基本的画布样本使用。参数 filename 可以是本地端的文件,也可以是网络的 URL位址。ImageCreateFromPNG返回值为 PNG 的文件代码,可供其它的函数使用。本函数在 PHP 3.0.13 版之后才支持。

*/

$fontcolor = imagecolorallocate( $im, $r, $g, $b );

/*int imagecolorallocate ( resource image, int red, int green, int blue)

imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。image 参数是 imagecreate() 函数的返回值。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。 */

$sy = imagesy( $im ) / $topy;

/*imagesx与imagesy-- 分别取得图像宽度与高度。*/

$array = imagettfbbox( $size, 0, $fontfile, $font );

/*array imagettfbbox (int size, int angle, string fontfile, string text)

说明 :

此函式计算并传回TrueType文字的区块坐标。

text : 要被测量的字串。

size : 字体大小。

fontfile : TrueType字体档的名称(也可以是URL)。

angle : text将要测量的角度度数。

ImageTTFBBox( )传回的阵列有8个元素,代表文字区块的四个顶点。

0 左下角X坐标

1 左下角Y坐标

2 右下角X坐标

3 右下角Y坐标

4 右上角X坐标

5 右上角Y坐标

6 左上角X坐标

7 左上角Y坐标

顶点是相对于text,所以不管角度。"左上角"的意思是,以水平的方向看文字时的左上角。

*/

$fontsy = ( $array[0] - $array[5] ) / 2;

$jiaozhen = $fontsy * 0.3;

$y = $sy + $fontsy - $jiaozhen;

if ( $x == "-1" )

{

$sx = imagesx( $im ) / 2;

$array = imagettfbbox( $size, 0, $fontfile, $font );

$fontsx = ( $array[0] - $array[2] ) / 2;

$x = $sx + $fontsx;

}

$r1 = hexdec( substr( $nameyycolor, 0, 2 ) );

$g1 = hexdec( substr( $nameyycolor, 2, 2 ) );

$b1 = hexdec( substr( $nameyycolor, 4, 2 ) );

$yycolor = imagecolorallocate( $im, $r1, $g1, $b1 );

if ( 2 < $nameyysize )

{

imagettftext( $im, $size, 0, $x + 3, $y + 3, $yycolor, $fontfile, $font );

/*imagettftext (image,size,angle, x, y,color,fontfile,text)

意思是 imagettftext() 将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,角度为 angle,颜色为 color,使用 fontfile 所指定的 TrueType 字体文件。根据 PHP 所使用的 GD 库的不同,如果 fontfile 没有以 '/'开头,则 '.ttf' 将被加到文件名之后并且会搜索库定义字体路径。

由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的右上角。

angle 以角度表示,0 度为从左向右阅读文本(3 点钟方向),更高的值表示逆时针方向(即如果值为 90 则表示从下向上阅读文本)。

fontfile 是想要使用的 TrueType 字体的文件名。

text 是文本字符串,可以包含 UTF-8 字符序列(形式为:{)来访问字体中超过前 255 个的字符。

color 是颜色的索引值。使用某颜色索引值的负值具有关闭防混色的效果*/

}

if ( 1 < $nameyysize )

{

imagettftext( $im, $size, 0, $x + 2, $y + 2, $yycolor, $fontfile, $font );

}

if ( 0 < $nameyysize )

{

imagettftext( $im, $size, 0, $x + 1, $y + 1, $yycolor, $fontfile, $font );

}

imagettftext( $im, $size, 0, $x, $y, $fontcolor, $fontfile, $font );

imagejpeg( $im );

/*imagejpeg( $im, $tofile );本函数用来建立一张 PNG 格式图形。参数 im 为使用 ImageCreate() 所建立的图片代码。参数 filename 可省略,若无本参数 filename,则会将图片指接送到浏览器端,记得在送出图片之前要先送出使用 Content-type: image/png 的标头字符串 (header) 到浏览器端,以顺利传输图片。本函数在 PHP 3.0.13 版之后才支持。 */

imagedestroy( $im );

/*bool imagedestroy ( resource image )

imagedestroy() 释放与 image 关联的内存

*/

//chmod( $tofile, 438 );

}

$words = $_GET['n'];

$len = strlen($words);

$leftx = ((16 - $len) / 2) * 8;

$pic = $_GET['p'];

$type = substr($pic,-3);

addchartopic( $pic,$leftx,20,iconv('gb2312','utf-8',$words),55, "simhei", "999999", "test1.jpg", "cccccc",0, "simhei.ttf", $type );

?>

在linux下 imagettftext()函数报错

ImageTTFText can't find/open ttf fonts

字体ttf文件用绝对路径就可以了,

define( APP_ROOT , dirname(__FILE__) ); //APP_ROOT."/simhei.ttf"

当然还可能是其他问题,我把问题的解决方法列出来

I'm attempting to use ImageTTFText to render text to an image, I can get

it to work if I

a) specify the full font path//用绝对路径

b) switch off anti-aliasing//关闭anti-aliasing(抗锯齿)

c) specify an OpenType font (e.g. arial.ttf) //指定一种OpenType字体(例如arial.ttf)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值