php生成文字图片效果
2007-07-08 16:34
357 查看
最近看到php的GD功能,试着做了一个基本生成文字图片效果的代码:
测试环境:windows+apache+php
显示文字图片页面:demo.php
<?php
$str = $_REQUEST['str'] ? $_REQUEST['str']:"暂无输入";
//$str = "中华人民共和国";
$im = imagecreate(200,200);
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);
imagecolortransparent($im,$white); //imagecolortransparent() 设置具体某种颜色为透明色,若注释
$black = imagecolorallocate($im,0x00,0x00,0x00);
imagefilledrectangle($im,50,50,150,150,$black);
imagestring($im,5,50,160,"happy every day",$black);
imagettftext($im,15,0,50,40,$black,"D:\windows\Fonts\simhei.ttf",$str); //字体设置部分linux和windows的路径可能不同
header("Content-type:image/png");
imagepng($im);
?>
调用图片页面:demo2.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php $var = "中华人民共和国"; ?>
<img src="demo.php?str=<?php echo $var?>">
文字图片进阶:
环境:Red Hat AS4.0+GD2.0.28+FreeType 2.1.9
目的:生成类似链接的即具有下划线的蓝色文字图像
//获取文字并将其转化成图片
//$str = urldecode($_REQUEST['str']) ? urldecode($_REQUEST['str']):"暂无输入";
//$str = "中华人民共和国";
$font_file = "/usr/share/fonts/zh_CN/TrueType/gbsn00lp.ttf";//字体设置部分linux的路径
$text = $str; //要显示的字符串
$font_size = 14; //字体大小
$arr = imagettfbbox($font_size,0,$font_file,$text); //确定会变化的字符串的位置
$text_width = $arr[2]-$arr[0]; //字符串文本框长度
$text_height = $arr[3]-$arr[5]; ////字符串文本框高度
$im = imagecreate($text_width,$text_height);
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);
imagecolortransparent($im,$white); //imagecolortransparent() 设置具体某种颜色为透明色,若注释
$blue = imagecolorallocate($im,0,0,255);
$arr = imagettftext($im,$font_size,0,0,$text_height-5,$blue,$font_file,$text);
imageline($im,$arr[0],$arr[1],$arr[2],$arr[3],$blue);
//imagettftext($im,12,0,0,20,$black,"c:\windows\Fonts\simhei.ttf",$url);//字体设置windows的路径
header("Content-type:image/png");
imagepng($im);
imagedestroy($im);
文字水印:
<?php
$pic=imagecreate(250,30);
$black=imagecolorallocate($pic,0,0,0);
$white=imagecolorallocate($pic,255,255,255);
$font="C://WINDOWS//Fonts//simhei.ttf";
$str ='php'.iconv('gb2312','utf-8','面对对象')." ovliverlin.cnblogs.com";
imagettftext($pic,10,0,10,20,$white,$font,$str);
header("Content-type: image/jpeg");
$filename='../src/images/photo.jpg';
$im=imagecreatefromjpeg($filename);
imagecopymerge($im,$pic,0,0,0,0,250,30,50);
imagejpeg($im);
?>
测试环境:windows+apache+php
显示文字图片页面:demo.php
<?php
$str = $_REQUEST['str'] ? $_REQUEST['str']:"暂无输入";
//$str = "中华人民共和国";
$im = imagecreate(200,200);
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);
imagecolortransparent($im,$white); //imagecolortransparent() 设置具体某种颜色为透明色,若注释
$black = imagecolorallocate($im,0x00,0x00,0x00);
imagefilledrectangle($im,50,50,150,150,$black);
imagestring($im,5,50,160,"happy every day",$black);
imagettftext($im,15,0,50,40,$black,"D:\windows\Fonts\simhei.ttf",$str); //字体设置部分linux和windows的路径可能不同
header("Content-type:image/png");
imagepng($im);
?>
调用图片页面:demo2.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php $var = "中华人民共和国"; ?>
<img src="demo.php?str=<?php echo $var?>">
文字图片进阶:
环境:Red Hat AS4.0+GD2.0.28+FreeType 2.1.9
目的:生成类似链接的即具有下划线的蓝色文字图像
//获取文字并将其转化成图片
//$str = urldecode($_REQUEST['str']) ? urldecode($_REQUEST['str']):"暂无输入";
//$str = "中华人民共和国";
$font_file = "/usr/share/fonts/zh_CN/TrueType/gbsn00lp.ttf";//字体设置部分linux的路径
$text = $str; //要显示的字符串
$font_size = 14; //字体大小
$arr = imagettfbbox($font_size,0,$font_file,$text); //确定会变化的字符串的位置
$text_width = $arr[2]-$arr[0]; //字符串文本框长度
$text_height = $arr[3]-$arr[5]; ////字符串文本框高度
$im = imagecreate($text_width,$text_height);
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);
imagecolortransparent($im,$white); //imagecolortransparent() 设置具体某种颜色为透明色,若注释
$blue = imagecolorallocate($im,0,0,255);
$arr = imagettftext($im,$font_size,0,0,$text_height-5,$blue,$font_file,$text);
imageline($im,$arr[0],$arr[1],$arr[2],$arr[3],$blue);
//imagettftext($im,12,0,0,20,$black,"c:\windows\Fonts\simhei.ttf",$url);//字体设置windows的路径
header("Content-type:image/png");
imagepng($im);
imagedestroy($im);
文字水印:
<?php
$pic=imagecreate(250,30);
$black=imagecolorallocate($pic,0,0,0);
$white=imagecolorallocate($pic,255,255,255);
$font="C://WINDOWS//Fonts//simhei.ttf";
$str ='php'.iconv('gb2312','utf-8','面对对象')." ovliverlin.cnblogs.com";
imagettftext($pic,10,0,10,20,$white,$font,$str);
header("Content-type: image/jpeg");
$filename='../src/images/photo.jpg';
$im=imagecreatefromjpeg($filename);
imagecopymerge($im,$pic,0,0,0,0,250,30,50);
imagejpeg($im);
?>
相关文章推荐
- php生成文字图片效果
- php生成文字图片效果
- php使用Imagick给图片写文字加阴影效果
- php生成图片文字水印
- php生成 文字水印 图片水印
- 用AspJpeg调整文字水印透明,生成图片水印的效果
- PHP 上传图片,生成水印,支持文字, gif, png
- php 文本文字创建生成图片
- phpqrcode生成带logo的二维码图片及带文字的二维码图片
- 用AspJpeg调整文字水印透明,生成图片水印的效果
- php中文验证码不显示文字的原因-终端下可正常生成图片,在浏览器中没有文字只有底图
- php使用Imagick给图片写文字加阴影效果
- PHP的生成图片或文字水印的类
- php GD2生成文字图片
- PHP实现水印效果(文字、图片)
- PHP图片上传并且加文字水印效果
- php文字生成图片以及给图片加水印流程及代码详解
- PHP实现图片上添加文字(证书生成)
- PHP 生成文字及图片水印