php高级实战-图像处理类

<?php

$image = new Image();

$image->water('meinv.png', 'tubiao.png', 5);
$image->suofang('meinv.png', 300, 300);

class Image {
    protected $path;
    protected $isRandName;
    protected $type;

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


    }
    public function water ($image, $water, $position, $tmd = 100, $prefix = 'water_') {
        if ((!file_exists($image)) || (!file_exists($water))) {
            dir('图片资源不存在');
        }

        $imageInfo = self::getImageInfo($image);
        $waterInfo = self::getImageInfo($water);

        if (!$this->checkImage($imageInfo, $waterInfo)) {
            die('水印图片太大');
        }

        $imageResource = self::openAnyImage($image);
        $waterResource = self::openAnyImage($water);

        $pos = $this->getPosition($position, $imageInfo, $waterInfo);

        imagecopymerge($imageResource, $waterResource, $pos['x'], $pos['y'], 0, 0, $waterInfo['width'], $waterInfo['height'], $tmd);

        $newName = $this->createNewName($image, $prefix);
        $newPath = rtrim($this->path, '/') . '/' . $newName;
        $this->saveImage($imageResource, $newPath);

        imagedestroy($imageResource);
        imagedestroy($waterResource);

        return $newPath;
    }

    protected function saveImage($imageResource, $newPath) {
        $func = 'image' . $this->type;
        $func($imageResource, $newPath);
    }

    static function getImageInfo ($imagePath){
        $info = getimagesize($imagePath);
        $data['width'] = $info[0];
        $data['height'] = $info[1];
        $data['mime'] = $info['mime'];

        return $data;
    }

    public function suofang ($image, $width, $height, $prefix = 'sf_') {
        $info = self::getImageInfo($image);
        $size = $this->getNewSize($width, $height, $info);
        $imageResource = self::openAnyImage($image);
        $newRsc = $this->kidOfImage($imageResource, $size, $info);
        $newName = $this->createNewName($image, $prefix);
        $newPath = rtrim($this->path, '/') . '/' . $newName;
        $this->saveImage($newRsc, $newPath);

        imagedestroy($imageResource);
        imagedestroy($newRsc);
    }

    protected function kidOfImage ($srcImg, $size, $imgInfo) {
        $newImg = imagecreatetruecolor($size['old_w'], $size['old_h']);
        $otsc = imagecolortransparent($srcImg);
        if ($otsc >= 0) {
            $transparentColor = imagecolorsforindex($srcImg, $otsc);
            $newtransparentcolor = imagecolorallocate(
                $newImg,
                $transparentColor['red'],
                $transparentColor['green'],
                $transparentColor['blue']
            );
        } else {
            $newtransparentcolor = imagecolorallocate($newImg, 0, 0, 0);
        }

        imagefill($newImg, 0, 0, $newtransparentcolor);

        imagecolortransparent($newImg, $newtransparentcolor);

        imagecopyresampled($newImg, $srcImg, $size['x'], $size['y'], 0, 0, $size['new_w'], $size['new_h'], $imgInfo['width'], $imgInfo['height']);

        return $newImg;
    }

    protected function getNewSize ($width, $height, $imgInfo) {
        $size['old_w'] = $width;
        $size['old_h'] = $height;

        $scaleWidth = $width / $imgInfo['width'];
        $scaleHeight = $height / $imgInfo['height'];
        $scaleFinale = min($scaleWidth, $scaleHeight);

        $size['new_w'] = round($imgInfo['width'] * $scaleFinale);
        $size['new_h'] = round($imgInfo['height'] * $scaleFinale);
        if ($scaleWidth < $scaleHeight) {
            $size['x'] = 0;
            $size['y'] = round(abs($size['new_h'] - $height) / 2);

        } else {
            $size['y'] = 0;
            $size['x'] = round(abs($size['new_w'] - $width) / 2);
        }

        return $size;
    }

    protected function checkImage ($imageInfo, $waterInfo) {
        if (($waterInfo['width'] > $imageInfo['width']) || ($waterInfo['height'] > $imageInfo['height'])) {
            return false;
        }

        return true;
    }

    static function openAnyImage ($imagePath) {
        $mime = self::getImageInfo($imagePath)['mime'];
        $image = null;
        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;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值