PHP Class - 图片水印

类文件

<?php

$image = new WaterMark();
// $path = $image->imgWater('./upload/bg1.jpg','./upload/w1.png',0,40);
// echo $path;
$image->textWater('./upload/bg1.jpg', 'ABCDEFG', 0, 23, [255,255,255]);

/**
 * @Purpose: 图像加图片水印
 * imgWater 图像水印
 * textWater 文字水印
 */
class WaterMark
{   
    // 保存图像的路径
    protected $path;
    // 是否随机名字
    protected $isRandName;
    // 保存图像的类型
    protected $type;

    function __construct($path = './image/', $isRandName = true, $type = 'png')
    {
        $this->path = $path;
        $this->isRandName = $isRandName;
        $this->type = $type;
    }

    // 生成图片水印 $image 原图片 $water水印图片 $position水印位置 $tmd透明度 $prefix前缀
    public function imgWater($image, $water, $position, $tmd = 100, $prefix = 'water_')
    {
        // 1 判断两图片是否存在
        if((!file_exists($image)) || (!file_exists($water))){
            die('图片资源不存在');
        }

        // 2 得到两图片高宽
        $imageInfo = self::getImageInfo($image);
        $waterInfo = self::getImageInfo($water);

        // 3 水印图片宽高是否大于原图
        if(!$this->checkImage($imageInfo, $waterInfo)){
            exit('水印图片太大');
        }

        // 4 打开图片
        $imageRes = self::openAnyImage($image);
        $waterRes = self::openAnyImage($water);

        // 5 计算水印坐标
        $pos = $this->getPosition($position, $imageInfo, $waterInfo);

        // 6 贴水印图片
        imagecopymerge($imageRes, $waterRes, $pos['x'], $pos['y'], 0, 0, $waterInfo['width'], $waterInfo['height'], $tmd);

        // 7 保存图片的文件名
        $newName = $this->createNewName($image, $prefix);

        // 8 保存图片的路径
        $newPath = rtrim($this->path, '/').'/'.$newName;

        // 9 保存图片
        $this->saveImage($imageRes, $newPath);

        // 10 销毁资源
        imagedestroy($imageRes);
        imagedestroy($waterRes);

        return $newPath;
    }

    // 生成文字水印 $image原图片 $text文字 $position水印位置 $fontsize文字尺寸 $prefix前缀
    public function textWater($image, $text, $position, $fontsize=13, $fontColor=[0,0,0], $prefix = 'water_')
    {
        $font = './upload/arial.ttf';//字体

        // 1. 判断两图片是否存在
        if((!file_exists($image))){
            die('图片资源不存在');
        }

        // 2. 得到图片信息
        $imageInfo = self::getImageInfo($image);

        // 3. 得到文字宽高
        $textInfo = imagettfbbox ( $fontsize, 0, $font, $text);

        $texth = abs($textInfo[1]-$textInfo[7]);
        $textw = abs($textInfo[2]-$textInfo[0]);
        $waterInfo['width'] = $textw;
        $waterInfo['height'] = $texth;

        // 4. 水印图片宽高是否大于原图
        if(!$this->checkImage($imageInfo, $waterInfo)){
            exit('水印文字太大');
        }

        // 5. 打开图片
        $imageRes = self::openAnyImage($image);
        
        // 6 字体颜色
        $black = imagecolorallocate($imageRes, $fontColor[0], $fontColor[1], $fontColor[2]);

        // 7. 计算水印位置
        $pos = $this->getPosition($position, $imageInfo, $waterInfo);

        // 8. 文字写入图像
        imagefttext($imageRes, $fontsize, 0, $pos['x'], $pos['y']+$texth, $black, $font, $text);

        // 9. 保存图片的文件名
        $newName = $this->createNewName($image, $prefix);

        // 10. 保存图片的路径
        $newPath = rtrim($this->path, '/').'/'.$newName;

        // 11. 保存图片
        $this->saveImage($imageRes, $newPath);

        // 12. 销毁图像
        imagedestroy($imageRes);
    }

    // 根据图片路径得到图片信息(宽, 高, mime)
    static function getImageInfo($imagePath)
    {
        $info = getimagesize($imagePath);
        $data['width'] = $info[0];
        $data['height'] = $info[1];
        $data['mime'] = $info['mime'];

        return $data;
    }

    // 检查水印图片大小是否大于原图
    protected function checkImage($imageInfo, $waterInfo)
    {
        if(($waterInfo['width']>$imageInfo['width']) || ($waterInfo['height']>$imageInfo['height'])){
            return false;
        }
        return true;
    }

    // 根据图片类型打开图片
    static function openAnyImage($imagePath)
    {
        // 图像的mime
        $mime = self::getImageInfo($imagePath)['mime'];
        switch ($mime) {
            case 'image/png':
                $image = imagecreatefrompng($imagePath);
                break;
            case 'image/gif':
                $image = imagecreatefromgif($imagePath);
                break;
            case 'image/jpeg':
                $image = imagecreatefromjpeg($imagePath);
                break;
            case 'image/wbmp':
                $image = imagecreatefromwbmp($imagePath);
                break;
        }

        return $image;
    }

    // 根据位置计算水印坐标
    protected function getPosition($position, $imageInfo, $waterInfo)
    {
        switch ($position) {
            case 1:
                $x = 0;
                $y = 0;
                break;
            case 2:
                $x = ($imageInfo['width'] - $waterInfo['width']) / 2;
                $y = 0;
                break;
            case 3:
                $x = $imageInfo['width'] - $waterInfo['width'];
                $y = 0;
                break;
            case 4:
                $x = 0;
                $y = ($imageInfo['height'] - $waterInfo['height']) / 2;
                break;
            case 5:
                $x = ($imageInfo['width'] - $waterInfo['width']) / 2;
                $y = ($imageInfo['height'] - $waterInfo['height']) / 2;
                break;
            case 6:
                $x = $imageInfo['width'] - $waterInfo['width'];
                $y = ($imageInfo['height'] - $waterInfo['height']) / 2;
                break;
            case 7:
                $x = 0;
                $y = $imageInfo['height'] - $waterInfo['height'];
                break;
            case 8:
                $x = ($imageInfo['width'] - $waterInfo['width']) / 2;
                $y = $imageInfo['height'] - $waterInfo['height'];
                break;
            case 9:
                $x = $imageInfo['width'] - $waterInfo['width'];
                $y = $imageInfo['height'] - $waterInfo['height'];
                break;
            case 0:
                $x = mt_rand(0, $imageInfo['width'] - $waterInfo['width']);
                $y = mt_rand(0, $imageInfo['height'] - $waterInfo['height']);
                break;
        }

        return ['x' => $x, 'y' => $y];
    }

    // 创建新名字
    protected function createNewName($imagePath, $prefix)
    {
        if($this->isRandName){
            $name = $prefix . uniqid().'.'.$this->type;
        }else{
            $name = $prefix . pathinfo($imagePath)['filename'].'.'.$this->type;
        }

        return $name;
    }

    // 保存图片
    protected function saveImage($imageRes, $newPath)
    {
        if(!$this->check()){
            die('目录不存在');
        }
        $func = 'image'.$this->type;
        $func($imageRes, $newPath);
    }

    // 检测创建文件夹, 检测文件是否可写
    protected function check()
    {
        // 文件夹不存在或不是目录,创建
        if(!file_exists($this->path) || !is_dir($this->path)){
            return mkdir($this->path, 0777, true);
        }

        // 判断文件是否可写
        if(!is_writeable($this->path)){
            return chmod($this->path, 0777);
        }

        return true;
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值