操作水印函数
/**
* 设置水印 zst
* @param $imgSrc 待处理图片全路径
* @param $markImg 水印图片全路径
* @param $watermark_file 处理完毕水印图片路径
* @param $markType 图片添加水印的方式,img代表以图片方式,text代表以文字方式添加水印
* @param int $markPos 图片水印添加的位置默认为0,取值范围:0~9 0:随机位置,在1~8之间随机选取一个位置 1:顶部居左 2:顶部居中 3:顶部居右 4:左边居中 5:图片中心 6:右边居中 7:底部居左 8:底部居中 9:底部居右
* @param $markText 给图片添加的水印文字
* @param $TextColor 水印文字的字体颜色
* @param $fontType 具体的字体库,可带相对目录地址
*/
public static function setWater($imgSrc, $markImg, $watermark_file, $markType, $markPos, $markText, $TextColor, $fontType)
{
$srcInfo = @getimagesize($imgSrc);
$srcImg_w = $srcInfo[0];
$srcImg_h = $srcInfo[1];
switch ($srcInfo[2]) {
case 1:
$srcim = imagecreatefromgif($imgSrc);
break;
case 2:
$srcim = imagecreatefromjpeg($imgSrc);
break;
case 3:
$srcim = imagecreatefrompng($imgSrc);
break;
default:
throw new \Exception("不支持的图片文件类型");
exit;
}
/*合成水印路径为空就覆盖原图*/
if (empty($watermark_file)) {
$watermark_file = $imgSrc;
}
if (!strcmp($markType, "img")) {
if (!file_exists($markImg) || empty($markImg)) {
return;
}
$markImgInfo = @getimagesize($markImg);
$markImg_w = $markImgInfo[0];
$markImg_h = $markImgInfo[1];
if ($srcImg_w < $markImg_w || $srcImg_h < $markImg_h) {
return;
}
switch ($markImgInfo[2]) {
case 1:
$markim = imagecreatefromgif($markImg);
break;
case 2:
$markim = imagecreatefromjpeg($markImg);
break;
case 3:
$markim = imagecreatefrompng($markImg);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
$logow = $markImg_w;
$logoh = $markImg_h;
}
if (!strcmp($markType, "text")) {
$fontSize = 16;
if (!empty($markText)) {
if (!file_exists($fontType)) {
return;
}
} else {
return;
}
$box = @imagettfbbox($fontSize, 0, $fontType, $markText);
$logow = max($box[2], $box[4]) - min($box[0], $box[6]);
$logoh = max($box[1], $box[3]) - min($box[5], $box[7]);
}
if ($markPos == 0) {
$markPos = rand(1, 9);
}
switch ($markPos) {
case 1:
$x = +5;
$y = +5;
break;
case 2:
$x = ($srcImg_w - $logow) / 2;
$y = +5;
break;
case 3:
$x = $srcImg_w - $logow - 5;
$y = +15;
break;
case 4:
$x = +5;
$y = ($srcImg_h - $logoh) / 2;
break;
case 5:
$x = ($srcImg_w - $logow) / 2;
$y = ($srcImg_h - $logoh) / 2;
break;
case 6:
$x = $srcImg_w - $logow - 5;
$y = ($srcImg_h - $logoh) / 2;
break;
case 7:
$x = +5;
$y = $srcImg_h - $logoh - 5;
break;
case 8:
$x = ($srcImg_w - $logow) / 2;
$y = $srcImg_h - $logoh - 5;
break;
case 9:
$x = $srcImg_w - $logow - 5;
$y = $srcImg_h - $logoh - 5;
break;
default:
die("此位置不支持");
exit;
}
$dst_img = @imagecreatetruecolor($srcImg_w, $srcImg_h);
imagecopy($dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h);
if (!strcmp($markType, "img")) {
imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh);
imagedestroy($markim);
}
if (!strcmp($markType, "text")) {
$rgb = explode(',', $TextColor);
$color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]);
imagettftext($dst_img, $fontSize, 0, $x, $y, $color, $fontType, $markText);
}
switch ($srcInfo[2]) {
case 1:
imagegif($dst_img, $watermark_file);
break;
case 2:
imagejpeg($dst_img, $watermark_file);
break;
case 3:
imagepng($dst_img, $watermark_file);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
unlink($markImg);
imagedestroy($dst_img);
imagedestroy($srcim);
}
转换图片格式(gd库,imagegick扩展插件)
1,gd库
$webpIm = imagecreatefromwebp($imgSrc); //获取文件im码
imagejpeg($webpIm,$imgSrc,100); // (文件im编码,文件路径,转换质量)
2,imagegick插件
$img = new \Imagick($imgSrc); //倒入文件路径
$img->setImageFormat('jpeg'); // 转换文件格式
$img->writeImage($imgSrc); // 写出文件路径
图片操作
/**
* 内部:操作图片
* @param $imagePath 文件路径
* @param $imageinfo [ 'height' , 'width']
* @param float $percent 缩放比例
*/
private static function thumpImage($imagePath, $imageinfo, $percent = 0.5)
{
if ($percent > 1 && $percent < $imageinfo['height'] && $percent < $imageinfo['width']) {
if ($imageinfo['height'] < $imageinfo['width']) {
$percent = $percent / $imageinfo['height'];
} else {
$percent = $percent / $imageinfo['width'];
}
}
$ratio = $percent > 1 ? 1 : $percent;
$new_width = $imageinfo['width'] * $ratio;
$new_height = $imageinfo['height'] * $ratio;
// 根據文件路徑獲取 Imagick 對象
$image = new \Imagick($imagePath);
//縮放到 200x100
$image->scaleImage($new_width, $new_height);
$temp_path = \Yii::$app->getBasePath().'/../web/static/'.time().'.png';
$image->writeImage($temp_path);
return [$temp_path, $new_width, $new_height];
}
|