PHP图像处理

1、概述

php可以通过gd函数库创建新图片或处理已有的图片, 它可以支持常见的主流图片类型:gif,jpg,png, 此外还支持freetype字体.

实用场景:验证码、缩放、裁剪、水印

使用gd库需要先在php.ini中开启gd库扩展:extension=gd2

图片格式:
• jpeg是一种普及率最高的图片类型, 它使用的是有损压缩格式

• png是网络上常用的图片类型, 它使用是无损压缩格式

• gif是网站上常用的图片类型, 它可以支持动态图片, 它使用无损压缩格式

2、php创建图像的六个步骤

2.1、创建画布资源

新建一个基于调色板的图像:imagecreate();

新建一个真彩色图像:imagecreatetruecolor();

$img=imagecreatetruecolor(500,300);

2.2、准备颜色:imagecolorallocate()

$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);

2.3、填充画布:imagefill()
imagefill($img,0,0,$black);

2.4、在画布上画图像或文字

• imagefill();          //区域填充
• imagesetpixel(); //画一个像素
• imageline(); //画一条线
• imagerectangle(); //画一个矩形
• imagefilledrectangle(); //画一矩形并填充
• imagepolygon(); //画一个多边形
• imagefilledpolygon(); //画一个多边形并填充
• imageellipse(); //画一个椭圆
• imagefilledellipse(); //画一个椭圆并填充
• imagearc(); //画一个椭圆弧
• imagefilledarc(); //画一个椭圆弧并填充
• imagestring(); //水平地画一行字符串
• imagestringup(); //垂直地画一行字符串
• imagechar(); //水平地画一个字符
• imagecharup(); //垂直地画一个字符
• imagettftext(); //用truetype字符向图像画一个字符串

imagefilledellipse($img,250,150,200,200,$white);

imagefill($img,0,0,$blue);

imageline($img,0,0,300,200,$white);

imagestring($im,4,50,150,'hello world',$white);

2.5、输出最终图像或保存最终图像:imagegif();imagejpeg();imagepng();

header('content-type:image/jpeg');
//图片从浏览器上输出
imagejpeg($img);
//把图片保存到本地
imagejpeg($img,'jin.jpg');

2.6、释放画布资源,销毁一个图像:imagedestroy();

imagedestroy($img);

 

例1:

代码:

//创建画布
$im=imagecreatetruecolor(300,200);
//准备颜色
$white=imagecolorallocate($im,255,255,255);
$blue=imagecolorallocate($im,0,0,255);
//开始绘画
imagefill($im,0,0,$blue);
imageline($im,0,0,300,200,$white);
imagestring($im,4,50,150,'hello world',$white);
//输出图像
header('content-type:image/png');
imagepng($im);
//释放资源
imagedestroy($im);

结果:

例二:

代码:

<?php 
// 1.创建画布资源
$img=imagecreatetruecolor(1200,800);

// 2.准备颜色
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);

// 3.填充画布
imagefill($img,0,0,$white);

// 4.在画布上画图像或文字
imagesetpixel($img,10,10,$green);               //画点
for($i=0;$i<100;$i++){
    imagesetpixel($img,mt_rand(100,200),mt_rand(200,300),$red); //点干扰素
}
imageline($img,20,20,500,300,$black);           //画线
for($i=0;$i<100;$i++){
    imageline($img,mt_rand(600,1200),mt_rand(600,900),1200,800,$green);  //线干扰素1
    imageline($img,mt_rand(600,1200),mt_rand(300,600),mt_rand(600,1200),mt_rand(300,600),$red); //线干扰素2
}
imagefilledellipse($img,250,150,200,200,$black);//画圆
for($i=0;$i<50;$i++){
    imageellipse($img,mt_rand(00,700),mt_rand(500,800),mt_rand(0,300),mt_rand(0,200),$green);  //圆干扰素
}
imagearc($img,350,150,200,200,0,90,$black);     //画弧线
for($i=0;$i<50;$i++){
    imagearc($img,mt_rand(800,1000),mt_rand(0,300),mt_rand(0,300),mt_rand(0,300),0,90,$red); //弧线干扰素1
    imagearc($img,mt_rand(800,1000),mt_rand(0,300),mt_rand(0,300),mt_rand(0,300),mt_rand(0,360),mt_rand(0,360),$blue);//弧线干扰素2
}
imagefilledrectangle($img,10,200,100,40,$black);//画矩形
for($i=0;$i<50;$i++){
    imagerectangle($img,mt_rand(600,1100),mt_rand(200,500),mt_rand(600,1100),mt_rand(200,500),$green);//矩形干扰素
}
//画饼
imagefilledarc($img,250,450,250,250,0,90,$black,IMG_ARC_PIE);
imagefilledarc($img,250,450,250,250,90,230,$blue,IMG_ARC_PIE);
imagefilledarc($img,250,450,250,250,230,360,$green,IMG_ARC_PIE);
//画字
imagestring($img,5,270,470,'30%',$black);
imagestring($img,5,200,450,'70%',$green);
imagestring($img,5,270,420,'30%',$red);

//画线型多边形
$arr=array(
    550,10,
    420,350,
    690,450,
);
imagepolygon($img,$arr,3,$black);

//画填充型多边形
$arr=array(
    650,600,
    600,650,
    490,550,
);
imagefilledpolygon($img,$arr,3,$white);

header('content-type:image/jpeg');
// 图片从浏览器上输出
imagejpeg($img);
// 把图片保存到本地
// imagejpeg($img,'jin.jpg');

imagedestroy($img);

结果:

3、html代码如何使用gd图片

3.1、<img src="index.php">
index.php必须能够完整输出图片
3.2、<img src="a.png">
使用index.php加工保存的图片

4、低配版验证码

$img=imagecreatetruecolor(150,50);

$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,180,180,180);

imagefill($img,0,0,$white);
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$str=join(' ',array_slice($arr,0,4));
magestring($img, 5, 30, 10, $str, $black);
//干扰素
for($i=0;$i<15;$i++){
    imagearc($img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),mt_rand(0,360),mt_rand(0,360),$blue);
}
header('content-type:image/png');
imagepng($img);
imagedestroy($img);

5、图片处理

获取图片资源

• imagecreatefrompng();

• imagecreatefromjpeg();

• imagecreatefromgif();

图片宽高获取

• imagesx($img);

• imagesy($img);

• getimagesize($file);不需要有图片资源即可获取到图片大小

图片缩放

imagecopyresampled()

$src_image=imagecreatefromjpeg('imgs/cs.jpg'); //获取已有图片的资源
$dst_image=imagecreatetruecolor(200,200);  //创建画布
$dst_x=0;
$dst_y=0;
$src_x=0;
$src_y=0;
$dst_w=300;
$dst_h=200;
$src_w=1920;
$src_h=1200;
//缩放
imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
//存储图片
imagejpeg($dst_image,'t_cs.jpg');

图片等比例缩放

function thumb($src_file,$dst_w,$dst_h){
    $srcarr=getimagesize($src_file);
    //变量函数
    switch($srcarr[2]){
        case 1:
            $imagecreatefrom="imagecreatefromgif";
            $imageout="imagegif";
            break;

        case 2:
            $imagecreatefrom="imagecreatefromjpeg";
            $imageout="imagejpeg";
            break;

        case 3:
            $imagecreatefrom="imagecreatefrompng";
            $imageout="imagepng";
            break;
    }

    $src_image=$imagecreatefrom($src_file);

    //等比例计算真实目标资源的宽和高
    $src_w=imagesx($src_image);
    $src_h=imagesy($src_image);

    $scale=($src_w/$dst_w)>($src_h/$dst_h)?($src_w/$dst_w):($src_h/$dst_h);

    $dst_w=floor($src_w/$scale);
    $dst_h=floor($src_h/$scale);

    $dst_image=imagecreatetruecolor($dst_w,$dst_h);

    $dst_x=0;
    $dst_y=0;
    $src_x=0;
    $src_y=0;

    imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);

    $t_name='t_'.basename($src_file);
    $t_dir=dirname($src_file);
    $s_file=$t_dir.'/'.$t_name;

    $imageout($dst_image,$s_file);
}
$src_file='imgs/cs.jpg';
thumb($src_file,200,200);

图片裁剪

$src_image=imagecreatefromjpeg('cs.jpg');
$dst_image=imagecreatetruecolor(200,200);
$dst_x=0;
$dst_y=0;
$src_x=0;
$src_y=0;
$dst_w=200;
$dst_h=200;
$src_w=1500;
$src_h=1200;
imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_image,'t_cs.jpg');

图片水印

$src_im=imagecreatefromjpeg('logo.jpg');
$dst_im=imagecreatefromjpeg('cs.jpg');
$dst_x=0;
$dst_y=0;
$src_x=0;
$src_y=0;
$src_w=200;
$src_h=50;
imagecopy($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h);
imagejpeg($dst_im,'w_cs.jpg');
原文地址:https://www.cnblogs.com/chuanzi/p/10391848.html