首页 php教程 php手册 php 图像函数大举例(非原创)

php 图像函数大举例(非原创)

Jun 13, 2016 pm 12:23 PM
exists function 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];
$fontcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
$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行):
$fontcolor=$colorarrs[rand(1,count($colorarrs)-1)];
输出:
在线演示: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);
//加载字体库
$fonts= dirname(__FILE__);
putenv('"gdfontpath=".$fonts=.""');
$fontname='arial';
$font_size=floor($height / 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);
$fontcolor_a=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
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);
然后加载您要验证码的字体:
$fonts= dirname(__FILE__);//返回当前根目录,字体文件复制到这里,字体文件是*.ttf文件
putenv('"gdfontpath=".$fonts=.""');
$fontname='arial';
定义字符的高度,这里我设置成高度的一半:
$font_size=floor($height / 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);
随机生成颜色,
$fontcolor_a=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
写入到图像;
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);设置字体高度$font_size=floor($height / 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);//红色
$font=dirname(__FILE__). "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
$fontsize=30;
$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);
设置水印字体,因为我们用的是中文,必须导入中文字体库,否则写不上或乱码,然后必须转换字符串编码
$font=dirname(__FILE__). "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
设置开始点,字体大小,视角:,写上字符串:
$fontsize=30;
$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);
生成缩洌图,水印大体核心技术就这么点.

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系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.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 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)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

我后悔之前不知道的 7 个 PHP 函数 我后悔之前不知道的 7 个 PHP 函数 Nov 13, 2024 am 09:42 AM

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

php程序在字符串中计数元音 php程序在字符串中计数元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? 什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

See all articles