page contents

PHP图像处理

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

attachments-2020-05-LWbhwJ6D5eb8e0a696ad3.jpg

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

结果:

attachments-2020-05-Z86v3YQf5eb8e1dd0c08e.jpg


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



attachments-2020-05-wzTwOIEy5eb8e22b87059.jpg

  • 发表于 2020-05-11 13:22
  • 阅读 ( 471 )
  • 分类:PHP开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1300 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章