搜索
首页后端开发php教程photoshop cs2 v9.0 绿色中文版 php 图像函数大举例(非原创)

如下方式是一种方法:
if(!function_exists('imagecreate')) {
die('本服务器不支持GD模块');
}
如果不支持的话,如何配置 ? 下载gd模块的dll文件,修改php.ini,重启服务器即可.
以下简称PHP作图为PS.
当您打算 PS的话,应该完成如下如下步骤,这是必经的.
1:创建基本PS对象(我假设为$image),填充背景(默认黑),以后的全部ps操作都是基于这个背景图像的.
2:在$image上作图.
3:输出这个图像.
4:销毁对象,清除使用内存.
首先,我们来认识几个常用的函数,这些函数在php手册里面都有详细介绍,此处大体引用下.
resource imagecreate ( int x_size, int y_size )
imagecreate() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的空白图像。
此函数基本同imagetruecolor($width,$height).
---------------------------------------------------------
int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。image 参数是 imagecreatetruecolor() 函数的返回值。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
---------------------------------------------------------
bool imagefill ( resource image, int x, int y, int color )
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
---------------------------------------------------------
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
---------------------------------------------------------
bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
---------------------------------------------------------
array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
本函数比较重要,参数较多,此处不再列出,它主要是写字到图像上,和上面的函数类似,但必前者强大.
---------------------------------------------------------
bool imagefilltoborder ( resource image, int x, int y, int border, int color )
imagefilltoborder() 从 x,y(图像左上角为 0, 0)点开始用 color 颜色执行区域填充,直到碰到颜色为 border 的边界为止。【注:边界内的所有颜色都会被填充。如果指定的边界色和该点颜色相同,则没有填充。如果图像中没有该边界色,则整幅图像都会被填充。】
------------------------------------------------
bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)为中心画一个椭圆。w 和 h 分别指定了椭圆的宽和高。椭圆用 color 颜色填充。如果成功则返回 TRUE,失败则返回 FALSE。
=================================================
输出图像数据:imagepng($image[,$filename])
======================================================
例一:输出蓝色背景和交叉白线的图形
$width=35;
$height=35;
//创建对象
$image=imagecreate($width,$height);
//提取颜色
$color_white=imagecolorallocate($image,255,255,255);//白色
$color_blue=imagecolorallocate($image,0,0,108);//蓝色
imagefill($image,0,0,$color_blue);
//作图
//线宽
imagesetthickness($image,3);
imageline($image,0,0,$width,$height ,$color_white);
imageline($image,$width,0,0,$height ,$color_white);
//发送对象至头
header('content-type:image/png');
imagepng($image);
/*
//发送对象至文件
$filename="ex1.png";
imagepng($image,$filename);
*/
//销毁对象
imagedestroy($image);
?>
输出图像:
在线演示:http://www.phzzy.org/temp/5do8/ex1.php
例二: 阴阳图
$width=400;
$height=400;
$image=imagecreatetruecolor($width,$height);
//提取颜色
$color_black=imagecolorallocate($image,0,2,0);//
$color_white=imagecolorallocate($image,255,255,255);//白色
$color_blue=imagecolorallocate($image,0,0,108);//蓝色
$color_red=imagecolorallocate($image,151,0,4);//红色
$color_my=imagecolorallocate($image,192,192,255);//背景
$color_temp=imagecolorallocate($image,199,199,199);//背景
//作图
imagefill($image,0,0,$color_white);
//第一个是大圆
imagefilledarc ($image,$width/2,$height/2,$height,$height,0,360,$color_blue,IMG_ARC_PIE);
//两个小圆
imagefilledellipse ($image,$width/2,$height/4 ,$height/2,$height/2,$color_red);
imagefilledellipse ($image,$width/2,$height/4 * 3,$height/2,$height/2,$color_blue);
/*imagefilledellipse -- 画一椭圆并填充*/
imagefilledarc ($image,$width/2,$height/2,$height,$height,-90,90,$color_red,IMG_ARC_PIE);
imagefilledellipse ($image,$width/2,$height/4 * 3,$height/2,$height/2,$color_blue);
//发送对象至头
header('content-type:image/png');
imagepng($image);
/*
//发送对象至文件
$filename="ex1.png";
imagepng($image,$filename);
*/
//销毁对象
imagedestroy($image);
?>
演示:
http://www.phzzy.org/temp/5do8/ex2.php
例三:3D图像--cool
$width=400;
$height=400;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
imagefill($image,0,0,$white);
// make the 3D effect
for ($i = $height /2 +20; $i > $height /2; $i--) {
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, $width/2, $height /2, $width/2, $height /2, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $height /2, $width/2, $height /2, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $height /2, $width/2, $height /2, 75, 360 , $red, IMG_ARC_PIE);
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
/*
//发送对象至文件
$filename="ex1.png";
imagepng($image,$filename);
*/
?>
输出:
演示:http://www.phzzy.org/temp/5do8/ex3.php
例四:简单的验证码
PHP创建验证码非常容易,容易的要死,简单的思路是这样的:
随机种子生成,提取随机字符,相连打印到图形,输出.,为了防止色盲,可以随机提取颜色,也可以自定义颜色,下面看看:
session_start();
$width=65;
$height=20;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$image=imagecreate($width,$height);
$colorarrs=array(
imagecolorallocate($image,255,255,255),//white
imagecolorallocate($image,0 ,0 , 0)//black
);
unset($sessionval);
imagesetthickness($image,3);
//随机得到字符串个数
$strsize=rand(3,5);
imagefill($image,0,0,$colorarrs[0]);
//一个个的写字符串到图片
for($i=0;$i$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
$f
$y_i = $height/2 + $font_size /3 ;
imagechar($image,5, 1+ $i * $width /$strsize,5,$sourcestrings[$i_temp],$fontcolor);
}
//写入session,以后验证用
unset($_SESSION['cjjer']);
$_SESSION['cjjer'] = $sessionval;
//添加杂点
for($i=0;$i{ $i_x=rand(0,$width);
$i_y=rand(0,$height);
$pixelcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($image,$i_x,$i_y,$pixelcolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>
生成的样式演示:
在线演示:http://www.phzzy.org/temp/5do8/ex4_login.php
有个很明显的问题就是生成的图片不够艳丽,从而很多的用户看起来不清楚,这样吧,我们自己设定几个比较艳丽的颜色然后输出,扩展colorarrs数组:
$colorarrs=array(
imagecolorallocate($image,255,255,255),
imagecolorallocate($image,0,0,0),
imagecolorallocate($image,0,70,0),
imagecolorallocate($image,92,0,12),
imagecolorallocate($image,0,0,128),
imagecolorallocate($image,233,10,216)
);
然后把23行变为(17行):
$f
输出:
在线演示:http://www.phzzy.org/temp/5do8/ex5_login.php
例五:大点的比较cool的验证码
PS 的图像还是比较小的,有时候为了某些原因(个人站点为了玩cool,just我,商业站点玩风格,吸引用户,just google,后话),验证码不是局限于十几个px的限制,有时候完全可以整个2百多,没啥问题,这时候,一种方案是把前面生成的小图强制大点,可以不? 可以,但是,看起来不够光滑,这是事实,明显,宽带不再是最重要的问题,不说其他的,下面演示几个比较好看的生成方式:
session_start();
$width=600;
$height=100;
if($height $height=$width / 4;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
//创建对象
$image=imagecreate($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
//加载字体库
$f
putenv('"gdf
$f
$f / 2);
//得到字符串
unset($sessionval);
$strsize=rand(5,8);
for($i=0;$i$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
$x_i =$font_size + $i *$width / ($strsize+1);
$y_i = $height / 2;
$angle_i=rand(-120,120);
$f
imageTTFText($image,$font_size,$angle_i,$x_i,$y_i,$fontcolor_a,$fontname,$sourcestrings[$i_temp]);
}
unset($_SESSION['cjjer']);
$_SESSION['cjjer'] = $sessionval;
//杂点数目
for($i=0;$i{
$i_x=rand(0,$width);
$i_y=rand(0,$height);
imagesetpixel($image,$i_x,$i_y,imagecolorallocate($image,rand(0,255),rand(0,2550),rand(0,255)));
}
//发送对象
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>
在线测试: http://www.phzzy.org/temp/5do8/ex6_login.php
解释性说明:
首先是宽和高,高太小字都看不清楚.随机提取的字符还是那么几个:
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
创建对象,填充成白色:
$image=imagecreate($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
然后加载您要验证码的字体:
$f//返回当前根目录,字体文件复制到这里,字体文件是*.ttf文件
putenv('"gdf
$f
定义字符的高度,这里我设置成高度的一半:
$f / 2);
清除变量,随机设置要生成字符的个数:
unset($sessionval);
$strsize=rand(5,8);
循环,一个个的把字符打上去:
得到本次循环的字符串.,并加在变量后面一会儿写入session
$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
得到写入图像的字符串的位置(x和y坐标)
$x_i =$font_size + $i *$width / ($strsize+1);
$y_i = $height / 2;
设置倾斜度,是从正面看的,.
$angle_i=rand(-120,120);
随机生成颜色,
$f
写入到图像;
imageTTFText($image,$font_size,$angle_i,$x_i,$y_i,$fontcolor_a,$fontname,$sourcestrings[$i_temp]);
如果对此函数存在疑问,请查阅相关资料.非常容易.
写入到session,一边注册码使用:
unset($_SESSION['cjjer']);
$_SESSION['cjjer'] = $sessionval;
添加杂点:
//杂点数目
for($i=0;$i{
$i_x=rand(0,$width);
$i_y=rand(0,$height);
imagesetpixel($image,$i_x,$i_y,imagecolorallocate($image,rand(0,255),rand(0,2550),rand(0,255)));
}
输出到头:
header('content-type:image/png');//这行表明是png图像,可不要,默认可以输出的.但不是图像的头格式
imagepng($image);
imagedestroy($image);
你可以加载你自己的字体库,设置旋转角度$angle_i=rand(-120,120);设置字体高度$f / 2);字体颜色$fontcolor_a和随机数的个数:$strsize=rand(5,8);
例六:给图片打上水印,生成缩列图
传统的ASP页子打水印和生成缩列图都是比较繁琐的,一般使用到的是其他组件什么的,但是,PHP可以轻松的干这些事情,正如您预料,不到30行的程序搞定这一切,请看这个源程序:
$source="my.jpg";
$zoom=0.5;
$str='我是帅哥,你是MM么?';
$image=imagecreatefromjpeg($source);
$width=imagesx($image);
$height=imagesy($image);
$color_red=imagecolorallocate($image,111,0,0);//红色
$f "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
$f
$angle=25;
$fromx=$width/5;
$fromy=$height/2;
imagettftext($image,$fontsize,$angle,$fromx,$fromy,$color_red,$font,$str);
$width_temp=imagesx($image) * $zoom;
$height_temp=imagesy($image) * $zoom;
$img=imagecreatetruecolor($width_temp,$height_temp);
imagecopyresized ($img,$image,0,0,0,0,$width_temp, $height_temp,$width,$height);
imagedestroy($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img,$file_zoomname);
imagedestroy($img);
?>
原始图片:
生成的jpg图片:
原始图片70K, 这里说下,如果生成gif,文件18k多,而png要用去76k,so我们生成缩列图用jpeg格式.
代码分析:
这里我先设置了几个参数:
$source="my.jpg"; //源图片
$zoom=0.5; //缩放百分比
$str='我是帅哥,你是MM么?'; //水印的文字
装载源图(不打水印不装载):
$image=imagecreatefromjpeg($source);
获取长,宽的大小:
$width=imagesx($image);
$height=imagesy($image);
设置水印字体,因为我们用的是中文,必须导入中文字体库,否则写不上或乱码,然后必须转换字符串编码
$f "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
设置开始点,字体大小,视角:,写上字符串:
$f
$angle=25;
$fromx=$width/5;
$fromy=$height/2;
imagettftext($image,$fontsize,$angle,$fromx,$fromy,$color_red,$font,$str);
按照缩放的大小要求生成新大小的对象:
$width_temp=imagesx($image) * $zoom;
$height_temp=imagesy($image) * $zoom;
$img=imagecreatetruecolor($width_temp,$height_temp);
把源图copy到新图,gd库的imagecopyresized自动缩放大小的
imagecopyresized ($img,$image,0,0,0,0,$width_temp, $height_temp,$width,$height);
生成小图片,清除对象:
imagedestroy($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img,$file_zoomname);
imagedestroy($img);
生成缩洌图,水印大体核心技术就这么点.

以上就介绍了photoshop cs2 v9.0 绿色中文版 php 图像函数大举例(非原创),包括了photoshop cs2 v9.0 绿色中文版方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

您应该多久再生一次会话ID?您应该多久再生一次会话ID?Apr 23, 2025 am 12:03 AM

会话ID应在登录时、敏感操作前和每30分钟定期重新生成。1.登录时重新生成会话ID可防会话固定攻击。2.敏感操作前重新生成提高安全性。3.定期重新生成降低长期利用风险,但需权衡用户体验。

如何在PHP中设置会话cookie参数?如何在PHP中设置会话cookie参数?Apr 22, 2025 pm 05:33 PM

在PHP中设置会话cookie参数可以通过session_set_cookie_params()函数实现。1)使用该函数设置参数,如过期时间、路径、域名、安全标志等;2)调用session_start()使参数生效;3)根据需求动态调整参数,如用户登录状态;4)注意设置secure和httponly标志以提升安全性。

在PHP中使用会议的主要目的是什么?在PHP中使用会议的主要目的是什么?Apr 22, 2025 pm 05:25 PM

在PHP中使用会话的主要目的是维护用户在不同页面之间的状态。1)会话通过session_start()函数启动,创建唯一会话ID并存储在用户cookie中。2)会话数据保存在服务器上,允许在不同请求间传递数据,如登录状态和购物车内容。

您如何在子域中分享会议?您如何在子域中分享会议?Apr 22, 2025 pm 05:21 PM

如何在子域名间共享会话?通过设置通用域名的会话cookie实现。1.在服务器端设置会话cookie的域为.example.com。2.选择合适的会话存储方式,如内存、数据库或分布式缓存。3.通过cookie传递会话ID,服务器根据ID检索和更新会话数据。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版