<?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;
}
}
php高级实战-图像处理类
最新推荐文章于 2024-11-03 15:01:11 发布
文章描述了一个使用PHP编写的类,该类提供了对图片进行添加水印和缩放的功能,包括检查图片尺寸、合并水印、裁剪和重命名图片。
摘要由CSDN通过智能技术生成