php图片是啥,PHP图片操作

1, PHP 中 图片的处理 要 依靠于扩展库, 可以选择gd2,或者imagemagick

第一步,首先要开启gd2的扩展库,在phpinfo() 中,要看到

738fbf1f22011344252719fa514aa4fe.png

这个的存在

打开这个后,就能够去操作图片了。

那么操作图片,是个什么流程呢??图片也是文件,是否跟文件操作的流程一样呢。

答案是yes,

打开图片。

对图片进行编辑,如拷贝,改变每一个像素的模式,把其他图片叠加上来等等。

可以选择保存图片。

貌似所有的文件操作都差不多一样,打开,编辑,保存.

那么在PHP中怎么打开一个图片呢?

这里要注意的是,打开图片的方式有多种,

比如打开一个已经存在的图片,会根据图片类型来,要使用不同的函数打开

具体函数有这么多:

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 -- 新建一个真彩色图像

看见这么多函数的时候,我当场就不乐意了,这么多函数,到底用哪一个,。?

难道有一个图片文件过来,我还要先判断它到底是什么格式的图片,是jpg,png,或者说是gif??????

然后再用对应的函数打开这个图片?

如果用扩展名的方式去判断图片类型,万一某位同学,把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文件中 标记中的 height/width 文本字符串。

此函数,功能就是取到文件宽,高,以及文件类型(png,jpg,gif),当场,我就测验了一下,如果用png图片,吧后缀名改成jpg,看看这个函数能否正常的获取文件类型,结果发现,是ok的,如下图:

6b90280f6a505ef98fd4001252891ac9.png

数组中下标为0=宽度,1=高度,2=类型, 类型怎么是数字呢???我要怎么对应呢?

带着疑问,再次阅读文档:发现如下

b668601cd8f062923a304d0eb4c0c3c4.png

看见这里,脑袋中就已经开始构思,做一个配置数组,来自动获取打开文件的函数了,代码如下:

44e1e260da15b2739a1ba2ea134bd73a.png

配置数组中 下标 为3的,也png,不知道为什么,这函数会返回为3. 不过问题已经得到解决,已经不用再伤脑筋,到底用哪个函数去打开图片了。

那么既然文件已经打开了,那么就该思考如何制作缩略图了。

缩略图的概念:应该就是把大图缩小,变成一个指定尺寸的小图。

这个时候,问题又来了,把一张图片,变成指定尺寸,说起来简单,万一要吧一个200 * 400的图片,变成一个400 * 200的,你变给我看看。长宽比例完全不对称,强行变,拉伸,都不知道会失真成什么样子,如何变嘛!!!!!!!!

仔细思考了下,解决方案有2种:

保持原图长宽比,以图片的长度,或者宽度中,最大的一个为准,尽可能的显示整张图,剩下的空白,用其他纯色填充。

保持原图长宽比,以图片的长度,或者宽度中,最小的一个为准,尽可能填充整个缩略图的画框,只显示原图中的某一部分。

整个缩略图的制作方式大概分如下几步:

新建一个图片,图片的宽高就是所需要的缩略图的宽高。

把原图中的某一部分copy到新建的图片里面来

把新建的图片显示出来,或者保存成文件。

涉及到的相关图片操作函数如下:

Imagecreatetruecolor 创建黑色图片

Getimagesize 获取图片相关信息

Imagecopyresampled copy一个图片中的一部分到另外一个图片

Imagejpeg 输出图片

具体使用方法请查看相关手册。

分别按照2种方式制作写了如下代码:

1, 以小为准

3b4712c22a31a364e3c97f329a78a783.png

2,以大为准

fb538cde7440666a53eebfa849d3ef65.png

88d5c095dbf79ab89845028d69b8ed75.png

bc8e8237f0ea0df056f13ad3e13a8e19.png

然后准备了3张图片

0bc180819515aae4347852bea559ea7b.png

8e5ffbdbb55a18dc3233cb9b095964bd.png

9e4577d91e70cbf2f3e0c185112b97d5.png

最终得到如下结果:

6db271b4a6a14a5fa3adcdbb5235a32c.png

4d8b435194ccff9e2b54f64bb00c14e0.png

经过考虑,最终选择以小为基准,也就是尽量铺满整个缩略图.

扩展:水印如何制作,验证码如何制作?原理跟缩略图差不多。附上效果

6d00604f3e2f1ca03f1536c27226c80b.png

2bad659c3ab8db1a4cc2447ba7020fb8.png

废话少说,上源码

以小为准

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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值