class ImageHelper
{
/**
* 图片加水印(适用于png/jpg/gif格式)
*
* @param $srcImg 原图片
* @param $waterImg 水印图片
* @param $savefile 保存路径
* @param $positon 水印位置:1:顶部居左, 2:顶部居右, 3:居中, 4:底部局左, 5:底部居右
* @param $alpha 透明度:0:完全透明, 100:完全不透明
*
* @return 成功 -- 加水印后的新图片地址
* 失败 -- -1:原文件不存在, -2:水印图片不存在, -3:原文件图像对象建立失败,-4:水印文件图像对象建立失败 -5:加水印后的新图片保存失败
*/
public static function waterMark($srcImg, $waterImg, $savefile, $positon, $alpha = 100)
{
//判断文件是否存在
$srcImgInfo = @getimagesize($srcImg);
if (!$srcImgInfo) {
return -1;
}
$waterImgInfo = @getimagesize($waterImg);
if (!$waterImgInfo) {
return -1;
}
//建立图像对象
$srcImgObj = imagecreatefromstring(file_get_contents($srcImg));
list($src_w, $src_h) = getimagesize($srcImg);
if (!$srcImgObj) {
return -3; //原文件图像对象建立失败
}
$waterImgObj = imagecreatefromstring(file_get_contents($waterImg));
if (!$waterImgObj) {
return -4; //原文件图像对象建立失败
}
//确定生成水印的位置
switch ($positon) {
//1顶部居左
case 1:
$x = $y = 0;
break;
//2顶部居右
case 2:
$x = $srcImgInfo[0] - $waterImgInfo[0];
$y = 0;
break;
//3居中
case 3:
$x = ($srcImgInfo[0] - $waterImgInfo[0]) / 2;
$y = ($srcImgInfo[1] - $waterImgInfo[1]) / 2;
break;
//4底部居左
case 4:
$x = 0;
$y = $srcImgInfo[1] - $waterImgInfo[1];
break;
//5底部居右
case 5:
$x = $srcImgInfo[0] - $waterImgInfo[0];
$y = $srcImgInfo[1] - $waterImgInfo[1];
break;
default:
$x = $y = 0;
}
//添加水印图片
imagecopymerge($srcImgObj, $waterImgObj, $x, $y, 0, 0, $src_w, $src_h, $alpha);
//输出图片
switch ($srcImgInfo[2]) {
case 1:
imagegif($srcImgObj, $savefile);
break;
case 2:
imagejpeg($srcImgObj, $savefile);
break;
case 3:
imagepng($srcImgObj, $savefile);
break;
default:
return -5; //保存失败
}
//销毁图像资源
imagedestroy($srcImgObj);
imagedestroy($waterImgObj);
return $savefile;
}
/**
* 生成圆形图片
* @param string $imgpath 要处理的图片路径
* @return 图片数据
*/
public static function changeCircularImg($imgpath)
{
$src_img = imagecreatefromjpeg($imgpath);
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
imagejpeg($img, $imgpath, 100);
}
/**
* 图片缩放
* @param $src
* @param $newwid
* @param $newhei
*/
public static function imgZip($src, $newwid, $newhei)
{
$imgInfo = getimagesize($src);
$imgType = image_type_to_extension($imgInfo[2], false);
$fun = "imagecreatefrom{$imgType}";
//声明图片 打开图片 在内存中
$image = $fun($src);
//方便配置长度宽度、高度,设置框为变量wid,高度为hei
$wid = $imgInfo[0];
$hei = $imgInfo[1];
//判断长度和宽度,以方便等比缩放,规格按照500, 320
if ($wid > $hei) {
$wid = $newwid;
$hei = $newwid / ($wid / $hei);
} else {
$wid = $newhei * ($wid / $hei);
$hei = $newhei;
}
//在内存中建立一张图片
$images2 = imagecreatetruecolor($newwid, $newhei); //建立一个500*320的图片
//将原图复制到新建图片中
//imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
imagecopyresampled($images2, $image, 0, 0, 0, 0, $wid, $hei, $imgInfo[0], $imgInfo[1]);
//销毁原始图片
imagedestroy($image);
//保存图片 到新文件
imagejpeg($images2, $src, 100); //10代码输出图片的质量 0-100 100质量最高
//销毁
imagedestroy($images2);
}
/**
* 图像裁剪
* @param $title string 原图路径
* @param $content string 需要裁剪的宽
* @param $encode string 需要裁剪的高
*/
public static function imageCropper($source_path, $target_width, $target_height)
{
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
// 源图过高
if ($source_ratio > $target_ratio) {
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
} // 源图过宽
elseif ($source_ratio < $target_ratio) {
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
} // 源图适中
else {
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
$source_image = imagecreatefromstring($source_path);
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 00, 0, 0, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
//保存图片到本地(两者选一)
//$randNumber = mt_rand(00000, 99999) . mt_rand(000, 999);
//$fileName = date('YmdHis') . ".jpg";
imagepng($target_image, $source_path);
imagedestroy($target_image);
}
/**
* 在二维码的中间区域镶嵌图片
* @param $QR 二维码数据流。比如file_get_contents(imageurl)返回的东东,或者微信给返回的东东
* @param $logo 中间显示图片的数据流。比如file_get_contents(imageurl)返回的东东
* @return 返回图片数据流
*/
public static function qrcodeWithLogo($QR,$logo)
{
$QR = imagecreatefromstring ($QR);
$logo = imagecreatefromstring ($logo);
$QR_width = imagesx ( $QR );//二维码图片宽度
$QR_height = imagesy ( $QR );//二维码图片高度
$logo_width = imagesx ( $logo );//logo图片宽度
$logo_height = imagesy ( $logo );//logo图片高度
$logo_qr_width = $QR_width / 4;//组合之后logo的宽度(占二维码的1/2.2)
$scale = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height / $scale;//组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
/**
* 重新组合图片并调整大小
* imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
*/
imagecopyresampled ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
// 获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng ($QR);
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}