PHP操作图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP操作图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5657字,纯文字阅读大概需要9分钟。
内容图文
![PHP操作图像](/upload/InfoBanner/zyjiaocheng/202/b0607d0b83844a98969ef571841f7057.jpg)
php操作图像最简单的,就是给图像加水印和加文字;
1,加水印:
<?php $one_path = 'be.jpg';$two_path = 's.png';//创建图片的实例$one = imagecreatefromstring(file_get_contents($one_path));$two = imagecreatefromstring(file_get_contents($two_path));//获取水印图片的宽高list($two_w, $two_h) = getimagesize($two_path);// 将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果// imagecopymerge($one, $two, 800, 300, 0, 0, $two_w, $two_h, 100);// 如果水印图片本身带透明色,则使用imagecopy方法imagecopy($one, $two, 800, 300, 0, 0, $two_w, $two_h);//输出图片list($one_w, $one_h, $one_type) = getimagesize($one_path);switch ($one_type) { case 1://GIF header('Content-Type: image/gif'); imagegif($one); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($one); //$filename='mess.jpg'; //imagejpeg($one,$filename); break; case 3://PNG header('Content-Type: image/png'); imagepng($one); break; default: break;}imagedestroy($one);imagedestroy($two);
imagejpeg($img,$filename,qulity):这个关键方法有三个参数,第一个是图像对象,第二个是图像保存路径,第三个是图像质量;
如果直接显示,只填第一个参数;如果保存成文件,填上第二个参数,比如D:/img/i.jpg,就会保存下来;
2,加文字:
<?php$dst_path = 'be.jpg';//创建图片的实例$dst = imagecreatefromstring(file_get_contents($dst_path));//打上文字$font = './tt.ttf';//字体$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色imagefttext($dst, 100, 0, 100, 500, $black, $font, '快乐编程');//输出图片list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);switch ($dst_type) { case 1://GIF header('Content-Type: image/gif'); imagegif($dst); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($dst); break; case 3://PNG header('Content-Type: image/png'); imagepng($dst); break; default: break;}imagedestroy($dst);
imagefttext ( resource $image , float $size , float $angle , int $x , int $y ,int $col , string $font_file , string $text [, array $extrainfo ] ):这个函数是关键
3,生成注册码:
<?php//echo PHP_VERSION;输出php版本/**imagecreatetruecolor -- 新建一个真彩色图像 imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。 * imagecolorallocate -- 为一幅图像分配颜色 imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。* imagefilledrectangle -- 画一矩形并填充 imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。* imagepng -- 以 PNG 格式将图像输出到浏览器或文件 imagepng ( resource image [, string filename] )* imagedestroy -- 销毁一图像 imagedestroy ( resource image )释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符.* header 1.更改网页头部信息2.跳转3.改成字符串* imagerectangle -- 画一个矩形 imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col ) imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。* imagesetpixel -- 画一个单一像素 imagesetpixel ( resource image, int x, int y, int color ) imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。 * imagearc -- 画椭圆弧 imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color ) imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。* imagefttext -- 使用 FreeType 2 字体将文本写入图像 imagefttext ( resource image, float size尺寸, float angle角度, int x, int y, int col颜色, string font_file字体文件, string text [, array extrainfo]字符 )* strlen -- Get string length strlen ( string string ) 获取字符串长度****///imagecreatetruecolor -- 新建一个真彩色图像$im = imagecreatetruecolor(300,200);//定义背景色imagecolorallocate(图像资源,红,绿,蓝)0??255 。255.255.255$back_color =imagecolorallocate($im,100,150,200);//imagefilledrectangle -- 画一矩形并填充imagefilledrectangle($im,0,0,300,200,$back_color);//$back_colors = imagecolorallocate($im,255,255,255);//imagerectangle -- 画一个矩形 作为边框//imagerectangle($im, 2,2,298,198,$back_colors);for ($i=0;$i<800;$i++){$point_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//imagesetpixel -- 画一个单一像素 画点$point = imagesetpixel($im,mt_rand(0,300),mt_rand(0,200),$point_color);}//imagearc-- 画椭圆弧 画线for($i=0;$i<300;$i++){$line_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));imagearc($im, mt_rand(0,500),mt_rand(0,500), mt_rand(0,500),mt_rand(0,500) ,mt_rand(0,360), mt_rand(0,360), $line_color);}//imagefttext -- 使用 FreeType 2 字体将文本写入图像$str ="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";$code = '';for($i=1;$i<5;$i++){$code.=$str{mt_rand(0, strlen($str)-1)};}$text_color = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//$str ='eng';imagefttext($im,50, mt_rand(0,45),120,120, $text_color,'c:\\WINDOWS\\Fonts\\simsun.ttc',$code);//输出图像必须更改头部信息header('Cache-Control:max-age=1,s-maxage=1,no-cache,must-revalidate');header('Content-type:image/png;charset=utf8');//imagepng -- 以 PNG 格式将图像输出到浏览器或文件imagepng($im);//imagedestroy -- 销毁一图像imagedestroy($im);
内容总结
以上是互联网集市为您收集整理的PHP操作图像全部内容,希望文章能够帮你解决PHP操作图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。