php示例功能-图像类


图像的概念
在PHP中如何使用创建的图像,并简单的生成一个图像
一.插入图片
1.先插入一张图片的png图片,来了解它的Content-Type(内容类型)。

<?php
?>
<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>图像类</title>
</head>
<body>
<!--插入一张png头像-->
<img src="img.png" alt="嘚瑟猴">
</body>
</html>

如果是php或html文件,那么Content-Type为:GET text/html HTTP/1.1
如果是png图像,那么Content-Type为:GET image/png HTTP/1.1

二.图像概念
1.一张图片是由各种颜色的像素组成的矩形;
2.颜色通过调色板定义,由三种值:红绿蓝。每个值从0(无色)-255(全色)定义;
3.文件格式一般为PNG、JPEG 和 GIF 等,推荐用 NG;
4.不同的格式处理透明度,PNG和GIF支持,JPEG不支持;
5.GIF只支持256中颜色,而真彩色可以支持24位的16777216种颜色。

三.创建图像共分为五步
1.使用imagecreatetruecolor函数创建一个真彩色图像。
格式:resource imagecreatetruecolor (int $width,int $height)
2.设置一个颜色
格式:int imagecolorallocate (resource $image,int $red,int $green,int $blue)
3.填充矩形
格式:bool imagefilledrectangle(resource $image,int $x1,int $y1,int $x2,int $y2, int $color)
4.以PNG格式将图像输出到浏览器或文件
格式:bool imagepng(resource $image [,string $filename] )
5.设置输出类型
创建图像image.php

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个红色
$red = imagecolorallocate($image, 255, 0, 0);

//绘制一个矩形并将颜色填充到图像上,第一个参数是图像资源,
//第二第三是是左上角的x 和y 轴,第三第四是右下角的x 和y 轴
//第四个是分配的颜色
imagefilledrectangle($image, 0, 0, 200, 200, $red);

//设置php文件为png 类型,image.php
header('Content-Type: image/png');

//生成png 图像
imagepng($image);

插入一个设置为image/png内容类型的php文件1.php。

<?php
?>
<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>图像类</title>
</head>
<body>
<!--插入一张php绘制的图像,在 1.php里-->
<img src="image.php" alt="生成的图像">
</body>
</html>

执行1.php返回创建的图片

图像类-画图函数
一.画图函数
1.基于已有的图像载入进行修改

<?php
//imagecreatefromgif、imagecreatefromjpeg
//加载一张图片,基于这种图片进行处理
$image = imagecreatefrompng('img.png');

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');
//生成png 图像
imagepng($image);

2.给一张img.png图片左上角填充白色

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//imagecreatefromgif、imagecreatefromjpeg
//加载一张图片,基于这种图片进行处理
$image = imagecreatefrompng('img.png');

//填充颜色
imagefill($image, 0, 0, $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');

//生成png 图像
imagepng($image);

3.生成一张图片并给图片里画上一条白色直线。
bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color )

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//画一条白色直线
imageline($image, 50, 50, 150, 150, $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');
//生成png 图像 imagepng($image);

4.画一条矩形(空心矩形)
bool imagerectangle(resource $image,int $x1,int $y1,int $x2,int $y2,int $col)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//画一条矩形
imagerectangle($image, 50, 50, 150, 150, $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');

//生成png 图像
imagepng($image);

  

5.给生成的矩形块中间填充白色矩形块(实心矩形)
bool imagefilledrectangle(resource $image,int $x1,int $y1,int $x2,int $y2,int $color)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//填充矩形
imagefilledrectangle($image, 50, 50, 150, 150, $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');
//生成png 图像
imagepng($image);

6.画一个多边形(三角形)
bool imagepolygon (resource $image,array $points,int $num_points,int $color)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//画一个多边形
imagepolygon($image, array(50, 50, 20, 120, 150, 180), 3, $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');
//生成png 图像
imagepng($image);

7.填充多边形块
bool imagefilledpolygon (resource $image,array $points,int $num_points,int $color)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//画一个多边形
imagefilledpolygon($image, array(50,50,20,120,150,180),3,$white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');
//生成png 图像 imagepng($image);

8.画一条弧线
bool imagearc(resource $image,int $cx,int $cy,int $w,int $h,int $s,int $e,int $color)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//画一个弧形
imagearc($image, 50, 50, 80, 80, 0, 360, $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');

//生成png 图像
imagepng($image);

9.创建一个背景为红色的图片,给这张图片上添加一张im.png图片并根据角度旋转图像
resource imagerotate(resource $image,float $angle,int $bgd_color)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个红色
$red = imagecolorallocate($image, 255, 0, 0);

//imagecreatefromgif、imagecreatefromjpeg
//加载一张图片,基于这种图片进行处理
$image = imagecreatefrompng('img.png');

//旋转图像,第二个参数角度,第三个参数未填充的颜色
$image = imagerotate($image, 45, $red);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');

//生成png 图像
imagepng($image);

图像类-文本和缩放
一.文本函数(水印的方法)
1.带文本的图像(给创建的图像里添加一个文字Abc)
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//第二个参数表示font 内置字体1,2,3,4,5 可选
//第三和第四参数表示文字的x,y 轴位置
//第五个参数表示文字内容
imagestring($image, 5, 10, 10, 'Abc', $white);

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');
//生成png 图像 imagepng($image);

2.基于字体的图像(给创建的图像里添加一个字体为ariblk.ttf的Abc和字体为simhei.ttf的中文黑体)
array imagettftext(resource $image,float $size,float $angle,int $x,int $y,int $color,string $fontfile,string $text)

<?php
//创建一个真彩色,长度高度均为200 像素,默认黑色图像,返回资源类型
$image = imagecreatetruecolor(200, 200);

//创建一个白色
$white = imagecolorallocate($image, 255, 255, 255);

//第二个参数表示字体大小
//第三个参数表示旋转的角度
//第三和第四个是位置x,y轴
//第五个是字体,如果要支持中文,请选择中文字体
//第六个是文本
imagettftext($image,40,0,50,50,$white,'ariblk.ttf','Abc');

//支持中文,直接用中文字体就行了
imagettftext($image,20,45,50,150,$white,'simhei.ttf','中文黑体');

//设置php 文件为png 类型,image.php
header('Content-Type: image/png');

//生成png 图像
imagepng($image);

二.缩放函数(缩略和裁剪)
1.缩放图片函数(把原来图片缩放成100*100的图像并生成新的图像)
bool imagecopyresampled(resource $dst_image,resource $src_image,int $dst_x,int $dst_y,int $src_x,int $src_y,int $dst_w,int $dst_h,int $src_w,int $src_h)

<?php
//200x200的画布
$image = imagecreatefrompng('img.png');

//求出长度
$width = imagesx($image);
//求出高度
$height = imagesy($image);

//新图长度
$x = $width / 2;
//新图高度
$y = $height / 2;

//建立一个新画布, 100x100的画布
$newImage = imagecreatetruecolor($x,$y);
//第一个参数是缩略图的句柄
//第二个参数是原图的句柄
//第三四五六参数是坐标和位移
//第六七参数表示新图的大小
//第八就参数表示原图的大小
//缩略和裁剪都是这个函数(主要通过后面这些参数)
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $x, $y, $width, $height);

//设置php 文件为png类型,image.php
header('Content-Type: image/png');
//生成png图像 imagepng($newImage);

图像类-创建类之等比例缩放
创建一个图形处理类,核心功能为等比例缩放。
一.成员字段
1.设置图像处理类等比例的必要字段。

<?php
//图像处理类
class Image {
    //原图片的地址
    private $origFile;

    //原图片的长度
    private $origWidth;

    //原图片的宽度
    private $origHeight;

    //原图片的类型
    private $origType;

    //原图片的对象
    private $origImage;

    //新图片的对象
    private $newImage;

    //新图片的地址
    private $newPath;
}

二.构造和自我实例化
1.构造方法初始化数据。

//构造方法,初始化
    public function __construct($file)
    {
        //保存原图片地址
        $this->origFile = $file;

        //保存原图片的长度、高度和类型
        list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile);

        //创建原图的对象
        $this->origImage = $this->getFromImage($this->origFile, $this->origType);

    }

三.创建图形对象
1.根据gif、jpeg和png三种不同的图形生成对象。

//创建图像对象
    private function getFromImage($file, $type)
    {
        //通过类型gif、jpg、png三种类型,创建对象
        switch ($type) {
            case 1 :
                $image = imagecreatefromgif($file);
                break;
            case 2 :
                $image = imagecreatefromjpeg($file);
                break;
            case 3 :
                $image = imagecreatefrompng($file);
                break;
            default :
                exit('警告:此图片类型,系统不支持!');
        }

        //返回图片对象
        return $image;
    }

四.按比例缩略
1.通过百分比来等比例缩放图片。

//按等比例缩放图片
    public function thumbScale($pct = 1)
    {
        //比例值必须是数字,不能为空,不可大于1,否则原图比例
        if (!is_numeric($pct) || empty($pct) || $pct > 1) {
            $pct = 1;
        }

        //新的长度和高度
        $newWidth  = $this->origWidth * $pct;
        $newHeight = $this->origHeight * $pct;

        //创建一个新画布
        $this->newImage = imagecreatetruecolor($newWidth, $newHeight);

        //缩略后的新图
        imagecopyresampled($this->newImage, $this->origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->origWidth, $this->origHeight);

        //生成图片并清理
        $this->output();
    }

五.生成并清理
1.生成一个新的图像,并清理掉对象。

//生成图片并清理对象
    private function output()
    {
        //获取新图片的地址
        $this->newPath = $this->setPath();

        //生成PNG图片
        imagepng($this->newImage, $this->newPath);

        //清理原图和新图对象
        imagedestroy($this->origImage);
        imagedestroy($this->newImage);
    }

    //对外获取图片地址
    public function getPath()
    {
        return $this->newPath;
    }

    //设置一个以年月日时分秒创建的名称
    private function setPath()
    {
        return date('YmdHis').'.png';
    }
}

最终image.php代码:

<?php
//图像处理类
class Image {
    //原图片的地址
    private $origFile;

    //原图片的长度
    private $origWidth;

    //原图片的宽度
    private $origHeight;

    //原图片的类型
    private $origType;

    //原图片的对象
    private $origImage;

    //新图片的对象
    private $newImage;

    //新图片的地址
    private $newPath;

    //构造方法,初始化
    public function __construct($file)
    {
        //保存原图片地址
        $this->origFile = $file;

        //保存原图片的长度、高度和类型
        list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile);

        //创建原图的对象
        $this->origImage = $this->getFromImage($this->origFile, $this->origType);

    }

    //创建图像对象
    private function getFromImage($file, $type)
    {
        //通过类型gif、jpg、png 三种类型,创建对象
        switch ($type) {
            case 1 :
                $image = imagecreatefromgif($file);
                break;
            case 2 :
                $image = imagecreatefromjpeg($file);
                break;
            case 3 :
                $image = imagecreatefrompng($file);
                break;
            default :
                exit('警告:此图片类型,系统不支持!');
        }

        //返回图片对象
        return $image;
    }

    //按等比例缩放图片
    public function thumbScale($pct = 1)
    {
        //比例值必须是数字,不能为空,不可大于1,否则原图比例
        if (!is_numeric($pct) || empty($pct) || $pct > 1) {
            $pct = 1;
        }

        //新的长度和高度
        $newWidth  = $this->origWidth * $pct;
        $newHeight = $this->origHeight * $pct;

        //创建一个新画布
        $this->newImage = imagecreatetruecolor($newWidth, $newHeight);

        //缩略后的新图
        imagecopyresampled($this->newImage, $this->origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->origWidth, $this->origHeight);

        //生成图片并清理
        $this->output();
    }

    //生成图片并清理对象
    private function output()
    {
        //获取新图片的地址
        $this->newPath = $this->setPath();

        //生成PNG图片
        imagepng($this->newImage, $this->newPath);

        //清理原图和新图对象
        imagedestroy($this->origImage);
        imagedestroy($this->newImage);
    }

    //对外获取图片地址
    public function getPath()
    {
        return $this->newPath;
    }

    //设置一个以年月日时分秒创建的名称
    private function setPath()
    {
        return date('YmdHis').'.png';
    }
}

2.php创建新图片位置和新图片缩放的数值引入图像

<?php
//引入图像类
require 'image.php';
//生成的新图片的位置
$image = new Image('img.png');
//生成的新图片等比例缩放数值
$image->thumbScale(0.3);
?>

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>图像处理类</title>
</head>
<body>
<!--获取原图片地址-->
<img src="<?=$image->getPath();?>" alt="">

</body>
</html>

图像类-创建类之固值缩放
一.思路概述
1.很多时候,我们需要上传的图片转换成固定的长宽;
2.固定后,需要通过一定比例的缩放和裁剪才能保证不失真,不变形;
3.可以计算固定长宽的等比例因子,然后进行等比例缩放;
4.然后,通过计算溢出的部分除以2,来偏移到指定的坐标裁剪;
5.一般来说这种做法现在是通过前端技术来裁切的,后端并不需要这么费力。
二.imge.php代码实现

<?php
//图像处理类
class Image {
    //原图片的地址
    private $origFile;

    //原图片的长度
    private $origWidth;

    //原图片的宽度
    private $origHeight;

    //原图片的类型
    private $origType;

    //原图片的对象
    private $origImage;

    //新图片的对象
    private $newImage;

    //新图片的地址
    private $newPath;


    //构造方法,初始化
    public function __construct($file)
    {

        //保存原图片地址
        $this->origFile = $file;

        //保存原图片的长度、高度和类型
        list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile);

        //创建原图的对象
        $this->origImage = $this->getFromImage($this->origFile, $this->origType);

    }

    //创建图像对象
    private function getFromImage($file, $type)
    {
        //通过类型gif、jpg、png 三种类型,创建对象
        switch ($type) {
            case 1 :
                $image = imagecreatefromgif($file);
                break;
            case 2 :
                $image = imagecreatefromjpeg($file);
                break;
            case 3 :
                $image = imagecreatefrompng($file);
                break;
            default :
                exit('警告:此图片类型,系统不支持!');
        }

        //返回图片对象
        return $image;
    }

    //固定长高等比例缩放
    //思路流程:
    //固定长高还必须等比例,保证不变形、不失真(但新图大小超过原图会模糊)
    public function thumbWH($newWidth = 0, $newHeight = 0)
    {
        //判断新长度是否合法
        if (!is_numeric($newWidth) || empty($newWidth) || !is_numeric($newHeight) || empty($newHeight)) {
            exit('警告:固定长高必须传入有效的值!');
        }

        //创建一个新图的容器
        $newX = $newWidth;
        $newY = $newHeight;

        //创建一个新图
        $this->newImage = imagecreatetruecolor($newX, $newY);

        //求出长度的等比例因子(现在长度除以原始长度)
        $scaleX = $newWidth / $this->origWidth;

        //求出高度的等比例因子(现在高度除以原始高度)
        $scaleY = $newHeight / $this->origHeight;

        //裁剪点
        $cutX = $cutY = 0;

        //判断等比例因子谁大就按照它来缩放
        if ($scaleX < $scaleY) {
            //如果长度因子小于高度因子时
            //长度就按照高度的等比因子缩放
            $newWidth = $this->origWidth * $scaleY;
            //得到现图溢出的部分的一半,作为X 轴的裁切点
            $cutX = ($newWidth - $newX) / 2;
        } else {
            $newHeight = $this->origHeight * $scaleX;
            $cutY = ($newHeight - $newY) / 2;
        }

        //缩略后的新图
        imagecopyresampled($this->newImage, $this->origImage, 0, 0, $cutX, $cutY, $newWidth, $newHeight, $this->origWidth, $this->origHeight);

        //生成图片并清理
        $this->output();
    }

    //生成图片并清理对象
    private function output()
    {
        //获取新图片的地址
        $this->newPath = $this->setPath();

        //生成PNG图片
        imagepng($this->newImage, $this->newPath);

        //清理原图和新图对象
        imagedestroy($this->origImage);
        imagedestroy($this->newImage);
    }

    //对外获取图片地址
    public function getPath()
    {
        return $this->newPath;
    }

    //设置一个以年月日时分秒创建的名称
    private function setPath()
    {
        return date('YmdHis').'.png';
    }
}

2.php引入固定值缩放大小为300,150图像:

<?php
//引入图像类
require 'image.php';

$image = new Image('img.png');

//固定值缩放
$image->thumbWH(300, 150);
?>

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>图像处理类</title>
</head>
<body>
<!--获取图片地址-->
<img src="<?=$image->getPath();?>" alt="">

</body>
</html>

图像类-创建类之添加水印
一.思路概述
1.水印是一张小图片,我们可以将它转换成字符串读取,图片也是一串信息;
2.将水印信息添加到生成的图片上,即可。
二.代码实现
1.等比例缩放image.php代码

<?php
//图像处理类
class Image {
    //原图片的地址
    private $origFile;

    //原图片的长度
    private $origWidth;

    //原图片的宽度
    private $origHeight;

    //原图片的类型
    private $origType;

    //原图片的对象
    private $origImage;

    //新图片的对象
    private $newImage;

    //新图片的地址
    private $newPath;

    //水印图片的地址
    private $waterMark;

    //构造方法,初始化
    public function __construct($file)
    {
        //保存原图片地址
        $this->origFile = $file;

        //保存水印图片地址
        $this->waterMark = 'logo.png';

        //保存原图片的长度、高度和类型
        list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile);

        //创建原图的对象
        $this->origImage = $this->getFromImage($this->origFile, $this->origType);

    }

    //创建图像对象
    private function getFromImage($file, $type)
    {
        //通过类型gif、jpg、png 三种类型,创建对象
        switch ($type) {
            case 1 :
                $image = imagecreatefromgif($file);
                break;
            case 2 :
                $image = imagecreatefromjpeg($file);
                break;
            case 3 :
                $image = imagecreatefrompng($file);
                break;
            default :
                exit('警告:此图片类型,系统不支持!');
        }

        //返回图片对象
        return $image;
    }

    //按等比例缩放图片
    public function thumbScale($pct = 1, $waterMark = false)
    {
        //比例值必须是数字,不能为空,不可大于1,否则原图比例
        if (!is_numeric($pct) || empty($pct) || $pct > 1) {
            $pct = 1;
        }

        //新的长度和高度
        $newWidth  = $this->origWidth * $pct;
        $newHeight = $this->origHeight * $pct;

        //创建一个新画布
        $this->newImage = imagecreatetruecolor($newWidth, $newHeight);

        //缩略后的新图
        imagecopyresampled($this->newImage, $this->origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->origWidth, $this->origHeight);

        //判断是否添加水印
        if ($waterMark) {
            //添加水印
            $this->addWaterMark($newWidth, $newHeight);
        }

        //生成图片并清理
        $this->output();
    }

    //添加水印效果
    private function addWaterMark($newX, $newY)
    {
        //file_get_contents 从文件中读取数据,也可以读取图像流信息
        //imagecreatefromstring 从图像流信息创建一个图像
        $waterMarkObject = imagecreatefromstring(file_get_contents($this->waterMark));

        //得到水印的大小
        $waterMarkWidth  = imagesx($waterMarkObject);
        $waterMarkHeight = imagesy($waterMarkObject);

        //设置右下角的位置
        $WaterMarkX = $newX - $waterMarkWidth;
        $WaterMarkY = $newY - $waterMarkHeight;

        //拷贝图像的一部分
        //参数1,2 是图像和水印对象
        //参数3,4 是水印坐标
        //参数5,6 是拷贝图像的坐标
        //参数7,8 是水印的大小
        imagecopy($this->newImage, $waterMarkObject, $WaterMarkX, $WaterMarkY, 0, 0, $waterMarkWidth, $waterMarkHeight);

        //清理
        imagedestroy($waterMarkObject);
    }

    //生成图片并清理对象
    private function output()
    {
        //获取新图片的地址
        $this->newPath = $this->setPath();

        //生成PNG图片
        imagepng($this->newImage, $this->newPath);

        //清理原图和新图对象
        imagedestroy($this->origImage);
        imagedestroy($this->newImage);
    }

    //对外获取图片地址
    public function getPath()
    {
        return $this->newPath;
    }

    //设置一个以年月日时分秒创建的名称
    private function setPath()
    {
        return date('YmdHis').'.png';
    }
}

等比例缩放0.7后添加水印1.php代码

<?php
//引入图像类
require 'image.php';

$image = new Image('img.png');

//等比例缩放
$image->thumbScale(0.7, true);

?>

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>图像处理类</title>
</head>
<body>

<img src="<?=$image->getPath();?>" alt="">

</body>
</html>

2.固定长高等比例缩放添加水印image.php代码

<?php
//图像处理类
class Image {
    //原图片的地址
    private $origFile;

    //原图片的长度
    private $origWidth;

    //原图片的宽度
    private $origHeight;

    //原图片的类型
    private $origType;

    //原图片的对象
    private $origImage;

    //新图片的对象
    private $newImage;

    //新图片的地址
    private $newPath;

    //水印图片的地址
    private $waterMark;

    //构造方法,初始化
    public function __construct($file)
    {

        //保存原图片地址
        $this->origFile = $file;

        //保存水印图片地址
        $this->waterMark = 'logo.png';

        //保存原图片的长度、高度和类型
        list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile);

        //创建原图的对象
        $this->origImage = $this->getFromImage($this->origFile, $this->origType);

    }

    //创建图像对象
    private function getFromImage($file, $type)
    {
        //通过类型gif、jpg、png 三种类型,创建对象
        switch ($type) {
            case 1 :
                $image = imagecreatefromgif($file);
                break;
            case 2 :
                $image = imagecreatefromjpeg($file);
                break;
            case 3 :
                $image = imagecreatefrompng($file);
                break;
            default :
                exit('警告:此图片类型,系统不支持!');
        }

        //返回图片对象
        return $image;
    }

//固定长高等比例缩放
    //思路流程:
    //固定长高还必须等比例,保证不变形、不失真(但新图大小超过原图会模糊)
    public function thumbWH($newWidth = 0, $newHeight = 0, $waterMark = false)
    {
        //判断新长度是否合法
        if (!is_numeric($newWidth) || empty($newWidth) || !is_numeric($newHeight) || empty($newHeight)) {
            exit('警告:固定长高必须传入有效的值!');
        }


        //创建一个新图的容器
        $newX = $newWidth;
        $newY = $newHeight;

        //创建一个新图
        $this->newImage = imagecreatetruecolor($newX, $newY);

        //求出长度的等比例因子
        $scaleX = $newWidth / $this->origWidth;

        //求出高度的等比例因子
        $scaleY = $newHeight / $this->origHeight;


        //裁剪点
        $cutX = $cutY = 0;

        //判断等比例因子谁大就按照它来缩放
        //比如$scaleX 0.4,$scaleY 0.83
        if ($scaleX < $scaleY) {
            //如果长度因子小于高度因子时
            //长度就按照高度的等比因子缩放
            $newWidth = $this->origWidth * $scaleY;
            //得到现图溢出的部分的一半,作为X 轴的裁切点
            $cutX = ($newWidth - $newX) / 2;
        } else {
            $newHeight = $this->origHeight * $scaleX;
            $cutY = ($newHeight - $newY) / 2;
        }

        //缩略后的新图
        imagecopyresampled($this->newImage, $this->origImage, 0, 0, $cutX, $cutY, $newWidth, $newHeight, $this->origWidth, $this->origHeight);

        //判断是否添加水印
        if ($waterMark) {
            //添加水印
            $this->addWaterMark($newX, $newY);
        }

        //生成图片并清理
        $this->output();

    }

    //添加水印效果
    private function addWaterMark($newX, $newY)
    {
        //file_get_contents 从文件中读取数据,也可以读取图像流信息
        //imagecreatefromstring 从图像流信息创建一个图像
        $waterMarkObject = imagecreatefromstring(file_get_contents($this->waterMark));

        //得到水印的大小
        $waterMarkWidth  = imagesx($waterMarkObject);
        $waterMarkHeight = imagesy($waterMarkObject);

        //设置右下角的位置
        $WaterMarkX = $newX - $waterMarkWidth;
        $WaterMarkY = $newY - $waterMarkHeight;

        //拷贝图像的一部分
        //参数1,2 是图像和水印对象
        //参数3,4 是水印坐标
        //参数5,6 是拷贝图像的坐标
        //参数7,8 是水印的大小
        imagecopy($this->newImage, $waterMarkObject, $WaterMarkX, $WaterMarkY, 0, 0, $waterMarkWidth, $waterMarkHeight);

        //清理
        imagedestroy($waterMarkObject);
    }

    //生成图片并清理对象
    private function output()
    {
        //获取新图片的地址
        $this->newPath = $this->setPath();

        //生成PNG图片
        imagepng($this->newImage, $this->newPath);

        //清理原图和新图对象
        imagedestroy($this->origImage);
        imagedestroy($this->newImage);
    }

    //对外获取图片地址
    public function getPath()
    {
        return $this->newPath;
    }

    //设置一个以年月日时分秒创建的名称
    private function setPath()
    {
        return date('YmdHis').'.png';
    }
}

固定长高等比例缩放为300,150后添加水印1.php代码

<?php
//引入图像类
require 'image.php';

$image = new Image('img.png');

//固定值缩放
$image->thumbWH(300, 150, true);
?>

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>图像处理类</title>
</head>
<body>

<img src="<?=$image->getPath();?>" alt="">

</body>
</html>

图像类-创建验证码类
一.成员属性

class Vcode
{
    //验证码的随机因子
    private $charset = 'ABCDEFGHKMNPRSTUVWXYZ23456789';

    //验证码的字符
    private $code;

    //验证码的长度
    private $length;

    //验证码图形的长度
    private $width;

    //验证码图形的高度
    private $height;

    //验证码的字体
    private $font;

    //验证码的字体大小
    private $fontSize;

    //验证码的图形对象
    private $image;
}

二.构造方法

//构造方法,初始化
    public function __construct()
    {
        //默认验证码字符长度
        $this->length = 4;

        //默认验证码图形长度
        $this->width = 120;

        //默认验证码图形高度
        $this->height = 40;

        //默认验证码字体大小
        $this->fontSize = 20;

        //默认验证码字体
        $this->font = 'elephant.ttf';
    }

三.随机码

//生成一组随机码4个
    private function createCode()
    {
        //得到验证码字符集的个数
        $charsetLength = strlen($this->charset);

        //随机出指定长度的一组字符串
        for ($i = 0; $i < $this->length; $i ++) {
            $this->code .= $this->charset[mt_rand(0, $charsetLength - 1)];
        }
    }

四.生成背景

//生成背景
    private function createBG()
    {
        //创建一个图形对象
        $this->image = imagecreatetruecolor($this->width, $this->height);

        //创建一个颜色
        $color = imagecolorallocate($this->image, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));

        //将颜色填充到图像上去
        //2,3 参数为左上角的坐标覆盖区域
        //4,5 参数为右下角的坐标覆盖区域
        imagefilledrectangle($this->image, 0, $this->height, $this->width, 0, $color);

    }

五.输出验证码   

//生成验证码
    public function output()
    {
        //生成随机码
        $this->createCode();
        //生成背景
        $this->createBG();

        //输出PNG图像
        header('Content-type: image/png');
        //输出图像
        imagepng($this->image);
        //清理图像
        imagedestroy($this->image);
    }    

六.生成线条雪花

//生成雪花和线条
    private function createLine()
    {
        //生成六根线条
        for ($i = 0; $i < 6; $i++) {
            //创建一个颜色
            $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            //从x1,y1轴到x2,y2周的一条直线
            imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
        }

        //生成十个雪花
        for ($i = 0; $i < 10; $i++) {
            //创建一个颜色
            $color = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
            //雪花
            imagestring($this->image, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
        }
    }    

七.生成文字

//生成文字
    private function createFont()
    {
        //求出每一个字符所占的比例位置
        $x = $this->width / $this->length;
        //循环出每个字符,比如4 个
        for ($i = 0; $i < $this->length; $i++) {
            //创建一个颜色
            $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            //在画布上添加字符
            //第2 个参数是字体大小,比如20
            //第3 个参数是字体角度,可以是负值
            //第4 个参数是字符出现的x 周,倍数* 第几个字符+ 1-5 的随机值
            //第5 个参数是字符出现的y 周,高度/ 1.4,这里1.4 刚好得到偏离中间的位置
            //第6 个参数是颜色
            //第7 个参数是字体
            //第8 个参数是设置每个字符
            imagettftext($this->image, $this->fontSize, mt_rand(-30, 30), $x * $i + mt_rand(1,5), $this->height / 1.4, $color, $this->font, $this->code[$i]);
        }
    }

最终vcode.php代码:

<?php
//验证码类
class Vcode
{
    //验证码的随机因子
    private $charset = 'ABCDEFGHKMNPRSTUVWXYZ23456789';

    //验证码的字符
    private $code;

    //验证码的长度
    private $length;

    //验证码图形的长度
    private $width;

    //验证码图形的高度
    private $height;

    //验证码的字体
    private $font;

    //验证码的字体大小
    private $fontSize;

    //验证码的图形对象
    private $image;


    //构造方法,初始化
    public function __construct()
    {
        //默认验证码字符长度
        $this->length = 4;

        //默认验证码图形长度
        $this->width = 120;

        //默认验证码图形高度
        $this->height = 40;

        //默认验证码字体大小
        $this->fontSize = 20;

        //默认验证码字体
        $this->font = 'elephant.ttf';
    }


    //生成一组随机码
    private function createCode()
    {
        //得到验证码字符集的个数
        $charsetLength = strlen($this->charset);

        //随机出指定长度的一组字符串
        for ($i = 0; $i < $this->length; $i ++) {
            $this->code .= $this->charset[mt_rand(0, $charsetLength - 1)];
        }

        //写入session
        session_start();
        $_SESSION['code'] = $this->code;
    }


    //生成背景
    private function createBG()
    {
        //创建一个图形对象
        $this->image = imagecreatetruecolor($this->width, $this->height);

        //创建一个颜色
        $color = imagecolorallocate($this->image, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));

        //将颜色填充到图像上去
        //2,3 参数为左上角的坐标覆盖区域
        //4,5 参数为右下角的坐标覆盖区域
        imagefilledrectangle($this->image, 0, $this->height, $this->width, 0, $color);

    }

    //生成雪花和线条
    private function createLine()
    {
        //生成六根线条
        for ($i = 0; $i < 6; $i++) {
            //创建一个颜色
            $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            //从x1,y1轴到x2,y2周的一条直线
            imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
        }

        //生成十个雪花
        for ($i = 0; $i < 10; $i++) {
            //创建一个颜色
            $color = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
            //雪花
            imagestring($this->image, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
        }
    }

    //生成文字
    private function createFont()
    {
        //求出每一个字符所占的比例位置
        $x = $this->width / $this->length;
        //循环出每个字符,比如4 个
        for ($i = 0; $i < $this->length; $i++) {
            //创建一个颜色
            $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            //在画布上添加字符
            //第2 个参数是字体大小,比如20
            //第3 个参数是字体角度,可以是负值
            //第4 个参数是字符出现的x 周,倍数* 第几个字符+ 1-5 的随机值
            //第5 个参数是字符出现的y 周,高度/ 1.4,这里1.4 刚好得到偏离中间的位置
            //第6 个参数是颜色
            //第7 个参数是字体
            //第8 个参数是设置每个字符
            imagettftext($this->image, $this->fontSize, mt_rand(-30, 30), $x * $i + mt_rand(1,5), $this->height / 1.4, $color, $this->font, $this->code[$i]);
        }
    }

    //生成验证码
    public function output()
    {
        //生成随机码
        $this->createCode();
        //生成背景
        $this->createBG();
        //生成线条和雪花
        $this->createLine();
        //生成文字
        $this->createFont();

        //输出PNG图像
        header('Content-type: image/png');
        //输出图像
        imagepng($this->image);
        //清理图像
        imagedestroy($this->image);
    }

}

$vcode = new Vcode();
$vcode->output();

创建一个3.php载入vcode.php验证

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<img src="vcode.php" alt="">

</body>
</html>

创建一个4.php载入session

<?php
session_start();
echo $_SESSION['code'];

执行3.php返回随机四位验证码

执行4.php返回3.php输出的四位验证码验证是否同步:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM