php在图片上生成文字代码
如果想利用PHP在图片上生成文字,就必须在php.ini里面将gd库开启,php在图片上生成文字代码如下:
//发送头文件 header("content-type: image/png"); //创建图像,如果失败输出内容 $im=@imagecreate(150,50) or die("cannot initialize new gd image stream"); //定义背景颜色 $background_color=imagecolorallocate($im,255,255,255); //定义文字颜色 $text_color=imagecolorallocate($im,233,14,91); //在图像上画出文件 imagestring($im,3,5,5,"hello world",$text_color); //输出图像文件 imagepng($im); //销毁图像 imagedestroy($im); /* 该代码的执行结果如图22.5所示: */ // 2图片等比例缩小 //定义一个文件 $filename='1.jpg'; //定义缩放百分比 $percent=0.5; //输出头文件 header('content-type: image/jpeg'); //获取新的大小 list($width,$height)=getimagesize($filename); $newwidth=$width * $percent; $newheight=$height * $percent; //创建图形区域,并载入图像 $thumb=imagecreatetruecolor($newwidth,$newheight); $source=imagecreatefromjpeg($filename); //重新调整大小 imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //输出图像 imagejpeg($thumb); /* 执行该代码,将把原图像缩放50%,并以新图像输出 */ //在图片上写文字 //定义内容 $data='ivborw0kggoaaaansuheugaaabwaaaascamaaab/2u7waaaabl'. 'bmveuaaad///+l2z/daaaasuleqvr4xqwquqoaiaxc2/0vxzdr'. 'ex4ijtrkb7lobnustxsb0jixiamssqnwlsv+wulf4avk9flq2r'. '8a5hse35q3eo2xp1a1wqkzsgetvdtkdqaaaabjru5erkjggg=='; //对内容进行base64编码 $data=base64_decode($data); //根据字符串新建图像 $im=imagecreatefromstring($data); if($im!== false) { //如果成功创建,则输出图像 header('content-type: image/png'); imagepng($im); } else { //如果创建失败,则输出内容 echo 'an error occured.'; } //在图片上写文字 header("content-type: image/png"); //创建图像,如果失败输出内容 $im=@imagecreate(100,50) or die("cannot initialize new gd image stream"); //定义背景颜色 $background_color=imagecolorallocate($im,255,255,255); //定义文字颜色 $text_color=imagecolorallocate($im,233,14,91); //在图像上画出文件 imagestring($im,1,5,5,"a simple text string",$text_color); //输出图像文件 imagepng($im); //销毁图像 imagedestroy($im); /* 执行该代码将生成一个jpeg图像。 并输出指定字符串 */
本文地址:http://www.phprm.com/tuxiang/fs4445.html
转载随意,但请附上文章地址:-)