第一步,首先要开启gd2的扩展库,在phpinfo() 中,要看到
这个的存在 打开这个后,就可以去操做图片了。
那么操做图片,是个什么流程呢??图片也是文件,是否跟文件操做的流程同样呢。
答案是yes,php
打开图片。数组
对图片进行编辑,如拷贝,改变每个像素的模式,把其余图片叠加上来等等。session
能够选择保存图片。函数
貌似全部的文件操做都差很少同样,打开,编辑,保存.字体
这里要注意的是,打开图片的方式有多种,
好比打开一个已经存在的图片,会根据图片类型来,要使用不一样的函数打开
具体函数有这么多:spa
imagecreate -- 新建一个基于调色板的图像 imagecreatefromgd2 -- 从 GD2 文件或 URL 新建一图像 imagecreatefromgd2part -- 从给定的 GD2 文件或 URL 中的部分新建一图像 imagecreatefromgd -- 从 GD 文件或 URL 新建一图像 imagecreatefromgif -- 从 GIF 文件或 URL 新建一图像 imagecreatefromjpeg -- 从 JPEG 文件或 URL 新建一图像 imagecreatefrompng -- 从 PNG 文件或 URL 新建一图像 imagecreatefromstring -- 从字符串中的图像流新建一图像 imagecreatefromwbmp -- 从 WBMP 文件或 URL 新建一图像 imagecreatefromxbm -- 从 XBM 文件或 URL 新建一图像 imagecreatefromxpm -- 从 XPM 文件或 URL 新建一图像 imagecreatetruecolor -- 新建一个真彩色图像
看见这么多函数的时候,我当场就不乐意了,这么多函数,到底用哪个,。?code
难道有一个图片文件过来,我还要先判断它究竟是什么格式的图片,是jpg,png,或者说是gif??????图片
而后再用对应的函数打开这个图片?ip
若是用扩展名的方式去判断图片类型,万一某位同窗,把png的扩展名称改为jpg,那我这里不就出问题了?资源
难道只能使用文件头的方式去获取文件类型了?
带着这些问题,我再次花时间去看了一次php帮助手册,终于发现,救命的稻草出现了。
如下是帮助手册原文:
getimagesize
(PHP 3, PHP 4, PHP 5)
getimagesize -- 取得图像大小
说明
array getimagesize ( string filename [, array &imageinfo] )
getimagesize()函数将测定任何 GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM或 WBMP图像文件的大小并返回图像的尺寸以及文件类型和一个能够用于普通 HTML文件中 <IMG>标记中的 height/width 文本字符串。
此函数,功能就是取到文件宽,高
,以及文件类型
(png,jpg,gif),当场,我就测验了一下,若是用png图片,吧后缀名改为jpg,看看这个函数可否正常的获取文件类型,结果发现,是ok的,以下图:
数组中下标为0=宽度,1=高度,2=类型
, 类型怎么是数字呢???我要怎么对应呢?
带着疑问,再次阅读文档:发现以下
看见这里,脑壳中就已经开始构思,作一个配置数组,来自动获取打开文件的函数了,代码以下:
配置数组中 下标 为3的,也png,不知道为何,这函数会返回为3. 不过问题已经获得解决,已经不用再伤脑筋,到底用哪一个函数去打开图片了。
那么既然文件已经打开了,那么就该思考如何制做缩略图了。
缩略图的概念
:应该就是把大图缩小,变成一个指定尺寸的小图。这个时候,问题又来了,把一张图片,变成指定尺寸,提及来简单,万一要吧一个200 * 400的图片,变成一个400 * 200的,你变给我看看
。长宽比例彻底不对称,强行变,拉伸,都不知道会失真成什么样子,如何变嘛!!!!!!!!
仔细思考了下,解决方案有2种:
保持原图长宽比,以图片的长度,或者宽度中,最大的一个为准,尽量的显示整张图,剩下的空白,用其余纯色填充。
保持原图长宽比,以图片的长度,或者宽度中,最小的一个为准,尽量填充整个缩略图的画框,只显示原图中的某一部分。
整个缩略图的制做方式大概分以下几步:
新建一个图片,图片的宽高就是所须要的缩略图的宽高。
把原图中的某一部分copy到新建的图片里面来
把新建的图片显示出来,或者保存成文件。
涉及到的相关图片操做函数以下:
Imagecreatetruecolor 建立黑色图片 Getimagesize 获取图片相关信息 Imagecopyresampled copy一个图片中的一部分到另一个图片 Imagejpeg 输出图片
具体使用方法请查看相关手册。
分别按照2种方式制做写了以下代码:
而后准备了3张图片
通过考虑,最终选择以小为基准,也就是尽可能铺满整个缩略图.
以小为准
header('Content-type: image/jpeg'); function get_thumb($filename,$width,$height,$outPath='',$type='1') { $newimages = imagecreatetruecolor($width,$height);//资源型 $config = array( IMG_GIF => 'imagecreatefromgif', IMG_JPG => 'imagecreatefromjpeg', IMG_PNG => 'imagecreatefrompng', IMG_JPEG =>'imagecreatefromjpeg' ); $imgInfo = getimagesize($filename); $o_image = $config[$imgInfo[2]]($filename); $w = &$imgInfo[0]; $h = &$imgInfo[1]; if ($w > $h){ $nheight = min($height,$h); $nwidth = $w / $h * $nheight; $x = -($nwidth - $width)/2; $y = -($nheight - $height)/2; }elseif ($w <= $h){ $nwidth = min($w,$width); $nheight = $h / $w * $nwidth; $y = -($nheight - $height)/2; $x = -($nwidth - $width)/2; } imagecopyresampled($newimages,$o_image,$x,$y,0,0,$nwidth,$nheight,$imgInfo[0],$imgInfo[1]); imagejpeg($newimages,null,100); } get_thumb($_GET['img'] ? $_GET['img']:"1.png",290,230);
以大为准
header('Content-type: image/jpeg'); function get_thumb($filename,$width,$height,$outPath='',$type='1') { $newimages = imagecreatetruecolor($width,$height);//资源型 //$white = imagecolorallocate($newimages,255,255,255); //imagefill($newimages,0,0,$white); $config = array( IMG_GIF => 'imagecreatefromgif', IMG_JPG => 'imagecreatefromjpeg', IMG_PNG => 'imagecreatefrompng', IMG_JPEG =>'imagecreatefromjpeg' ); $imgInfo = getimagesize($filename); $o_image = $config[$imgInfo[2]]($filename); $w = &$imgInfo[0]; $h = &$imgInfo[1]; if ($w > $h){ $nwidth = min($width,$w); $nheight = $h / $w * $nwidth; $x = ($width - $nwidth) / 2; $y = ($height - $nheight) / 2; }elseif ($w < $h){ $nheight = min($height,$h); $nwidth = $w / $h * $nheight; $y = ($height - $nheight) / 2; $x = ($width - $nwidth) / 2; } imagecopyresampled($newimages,$o_image,$x,$y,0,0,$nwidth,$nheight,$imgInfo[0],$imgInfo[1]); imagejpeg($newimages,null,100); } get_thumb($_GET['img'] ? $_GET['img']:"1.png",400,400);
水印
function get_thumb($filename,$width,$height,$outPath='',$type='1') { $newimages = imagecreatetruecolor($width,$height);//资源型 //$white = imagecolorallocate($newimages,255,255,255); //imagefill($newimages,0,0,$white); $config = array( IMG_GIF => 'imagecreatefromgif', IMG_JPG => 'imagecreatefromjpeg', 3 => 'imagecreatefrompng', IMG_PNG => 'imagecreatefrompng', IMG_JPEG =>'imagecreatefromjpeg' ); $imgInfo = getimagesize($filename); // $o_image_type = image_type_to_extension($imgInfo['2']); $o_image = $config[$imgInfo[2]]($filename); $w = &$imgInfo[0]; $h = &$imgInfo[1]; if ($w > $h){ $nheight = min($height,$h); $nwidth = $w / $h * $nheight; $x = -($nwidth - $width)/2; $y = -($nheight - $height)/2; }elseif ($w <= $h){ $nwidth = min($w,$width); $nheight = $h / $w * $nwidth; $y = -($nheight - $height)/2; $x = -($nwidth - $width)/2; } imagecopyresampled($newimages,$o_image,$x,$y,0,0,$nwidth,$nheight,$imgInfo[0],$imgInfo[1]); /*水印*/ $filename = "sy.png"; $imgInfo = getimagesize($filename); $o_image = $config[$imgInfo[2]]($filename); // var_dump($imgInfo); // imagecopyresampled($newimages,$o_image,0,0,0,0,$imgInfo[0],$imgInfo[1],$imgInfo[0],$imgInfo[1]); if ($width >= $height){ $h = $height / 16; $w = $imgInfo[0] / $imgInfo[1] * $h; }else{ $w = $width / 9; $h = $imgInfo[1] / $imgInfo[0] * $w; } $h = min($h,$imgInfo[1]); $w = min($w,$imgInfo[0]); $sy_x = $width - $w - 40; $sy_y = $height - $h -30; imagecopyresampled($newimages,$o_image,$sy_x ,$sy_y,0,0,$w,$h,$imgInfo[0],$imgInfo[1]); header('Content-type: image/jpeg'); imagejpeg($newimages,null,100); } get_thumb($_GET['img'] ? $_GET['img']:"1.png",400,400);
验证码
error_reporting(7); session_start(); header("Content-Type:image/png"); vcode(); function vcode($width=100,$height=40) { $_SESSION['vcode'] = ""; //初始化session $im = imagecreatetruecolor($width,$height); //新建一个300 * 300 的黑色图像,资源型数据 imagefill ($im, 0, 0, imagecolorallocate($im,0xCC,0xCC,0xCC));//填充灰色,其实就是吧背景改为灰色 $colors = array( //imagecolorallocate($im,255,255,255), ); for($i=0;$i<10;++$i){//生成10个比较深的颜色 $colors[] = imagecolorallocate($im,mt_rand(0,128),mt_rand(0,128),mt_rand(0,128)); } $string = "噢静安寺奇偶挂科率快点快点快递发从摩擦摩擦在迪欧发顺丰是大发明家路上打劫风口浪尖独守空房激励大师开发就确定是房间里看电视费我我荣获惟一让退全额一千五要吗新民婆呕吐太无二套有"; $len = strlen($string)/3;//计算中文的长度 $font = "2.TTF";//设置字体 for($i=0;$i<4;++$i){//取4个字,而后画到图片里面,顺便也放进session $_tmp = mt_rand(0,$len-1); $_tmp = $_tmp * 3; //$text .= mb_substr($string,$_tmp,1,'utf8'); $text = $string[$_tmp].$string[$_tmp+1].$string[$_tmp+2]; $_SESSION['vcode'] .= $text; imagettftext($im,mt_rand(16,26),mt_rand(-45,45),11+21*$i,25,$colors[array_rand($colors)],$font,$text); } $colors = array(); for($i=0;$i<10;++$i){//再次随机10个浅色 $colors[] = imagecolorallocate($im,mt_rand(128,255),mt_rand(128,255),mt_rand(128,255)); } for($i=0;$i<20;++$i){//用浅色划线 imageline ( $im, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $colors[array_rand($colors)]); //画一根白线 } //bool imagesetpixel ( resource image, int x, int y, int color ) for($i=0;$i<200;++$i){//用浅色画点 imagesetpixel ($im, mt_rand(0,$width), mt_rand(0,$height), $colors[array_rand($colors)] ); } imagepng($im); }