PHP操作图像

php中文网
发布: 2016-06-23 13:41:13
原创
1216人浏览过

php确实是一个简单的语言,php能够那么流行也是因为该语言能够从简单出发,用最简单的语句就实现功能,摒弃了过多的繁琐配置,寥寥几句就可以实现java需要写一堆才能实现的功能;这个既是有点也是缺点,不过无论如何,php引领了一种潮流,就是简单,simple is all;

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):这个关键方法有三个参数,第一个是图像对象,第二个是图像保存路径,第三个是图像质量;

立即学习PHP免费学习笔记(深入)”;

如果直接显示,只填第一个参数;如果保存成文件,填上第二个参数,比如D:/img/i.jpg,就会保存下来;

图像转图像AI
图像转图像AI

利用AI轻松变形、风格化和重绘任何图像

图像转图像AI 65
查看详情 图像转图像AI

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&deg;位于三点钟位置,以顺时针方向绘画。* 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速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号