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函数

 

 

 

在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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
GDPHP的一个图像处理扩展,可以用于创建、修改和处理各种图像,例如生成缩略图、添加水印、绘制形状和文字等。下面是一些常用的GD代码示例: 1. 创建一个空白的图片 ```php $width = 300; $height = 200; $image = imagecreatetruecolor($width, $height); ``` 2. 从文件中读取一张图片并进行缩放 ```php $filename = 'image.jpg'; $width = 300; $height = 200; // 获取图片信息 list($old_width, $old_height, $type) = getimagesize($filename); // 根据图片类型创建一个新的图像 switch ($type) { case IMAGETYPE_JPEG: $old_image = imagecreatefromjpeg($filename); break; case IMAGETYPE_PNG: $old_image = imagecreatefrompng($filename); break; case IMAGETYPE_GIF: $old_image = imagecreatefromgif($filename); break; default: die('Unsupported image type'); } // 创建一个新的缩放后的图像 $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $old_width, $old_height); // 输出图像到浏览器 header('Content-Type: image/jpeg'); imagejpeg($new_image); // 释放内存 imagedestroy($old_image); imagedestroy($new_image); ``` 3. 添加文字水印 ```php $filename = 'image.jpg'; $text = 'Hello, world!'; // 获取图片信息 list($width, $height, $type) = getimagesize($filename); // 根据图片类型创建一个新的图像 switch ($type) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($filename); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($filename); break; case IMAGETYPE_GIF: $image = imagecreatefromgif($filename); break; default: die('Unsupported image type'); } // 设置字体和颜色 $font = 'arial.ttf'; $color = imagecolorallocate($image, 255, 255, 255); // 添加水印 imagettftext($image, 20, 0, 10, $height - 30, $color, $font, $text); // 输出图像到浏览器 header('Content-Type: image/jpeg'); imagejpeg($image); // 释放内存 imagedestroy($image); ``` 这些只是GD的一些基本用法,还有很多其他功能和选项可供使用。如果你想深入了解GD的更多细节和用法,可以查看PHP官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值