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

photoshop cs2 v9.0 绿色中文版 php 图像函数大举例(非原创)

Jul 29, 2016 am 08:40 AM

如下方式是一种方法:
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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

长URL(通常用关键字和跟踪参数都混乱)可以阻止访问者。 URL缩短脚本提供了解决方案,创建了简洁的链接,非常适合社交媒体和其他平台。 这些脚本对于单个网站很有价值

Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

在Facebook在2012年通过Facebook备受瞩目的收购之后,Instagram采用了两套API供第三方使用。这些是Instagram Graph API和Instagram Basic Display API。作为开发人员建立一个需要信息的应用程序

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

这是有关用Laravel后端构建React应用程序的系列的第二个也是最后一部分。在该系列的第一部分中,我们使用Laravel为基本的产品上市应用程序创建了一个RESTFUL API。在本教程中,我们将成为开发人员

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

2025年的PHP景观调查调查了当前的PHP发展趋势。 它探讨了框架用法,部署方法和挑战,旨在为开发人员和企业提供见解。 该调查预计现代PHP Versio的增长

See all articles