87 lines
2.7 KiB
PHP
87 lines
2.7 KiB
PHP
<?php
|
|
/*
|
|
* @Author: 田老师 4021673@qq.com
|
|
* @Date: 2022-11-04 04:29:16
|
|
* @LastEditTime: 2022-11-05 15:34:04
|
|
* @Description: Do not edit
|
|
*/
|
|
namespace Org\ImageController;
|
|
class ImageController
|
|
{
|
|
private $width;
|
|
private $height;
|
|
private $fileName;
|
|
private $path;
|
|
/**
|
|
* @param {*} $width 缩放之后的宽度
|
|
* @param {*} $height 缩放之后的高度
|
|
* @param {*} $fileName 文件路径名字
|
|
* @param {*} $path 生成图片保存的路径
|
|
*/
|
|
public function __construct($width,$height,$fileName,$path)
|
|
{
|
|
date_default_timezone_set('Asia/Shanghai');
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
|
$this->width=$width;
|
|
$this->height=$height;
|
|
$this->fileName=$fileName;
|
|
$this->path=$path;
|
|
}
|
|
|
|
/**
|
|
* @description: 先缩放 再 圆角处理图片
|
|
* @return {*} 生成图片的 路径及名字
|
|
*/
|
|
public function make_img()
|
|
{
|
|
//第一步 压缩图片
|
|
$imggzip = $this->resize_img();
|
|
//第二步 裁减成圆角图片
|
|
return $this->round_img($imggzip);
|
|
}
|
|
|
|
public function resize_img()
|
|
{
|
|
$imgname = $this->path . uniqid() . '.png';
|
|
|
|
list($width, $height) = getimagesize($this->fileName); //获取原图尺寸
|
|
//缩放尺寸
|
|
$src_im = imagecreatefromjpeg($this->fileName);
|
|
$dst_im = imagecreatetruecolor($this->width, $this->height);
|
|
imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $this->width, $this->height, $width, $height);
|
|
imagejpeg($dst_im, $imgname); //输出压缩后的图片
|
|
imagedestroy($dst_im);
|
|
imagedestroy($src_im);
|
|
return $imgname;
|
|
}
|
|
public function round_img($url)
|
|
{
|
|
$w = $this->width;
|
|
$h = $this->height; // original size
|
|
$original_path = $url;
|
|
$dest_path = $this->path . uniqid() . '.png';
|
|
$src = imagecreatefromstring(file_get_contents($original_path));
|
|
$newpic = imagecreatetruecolor($w, $h);
|
|
imagealphablending($newpic, false);
|
|
$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);
|
|
$r = $w / 2;
|
|
for ($x = 0; $x < $w; $x++)
|
|
for ($y = 0; $y < $h; $y++) {
|
|
$c = imagecolorat($src, $x, $y);
|
|
$_x = $x - $w / 2;
|
|
$_y = $y - $h / 2;
|
|
if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {
|
|
imagesetpixel($newpic, $x, $y, $c);
|
|
} else {
|
|
imagesetpixel($newpic, $x, $y, $transparent);
|
|
}
|
|
}
|
|
imagesavealpha($newpic, true);
|
|
imagepng($newpic, $dest_path);
|
|
imagedestroy($newpic);
|
|
imagedestroy($src);
|
|
unlink($url);
|
|
return $dest_path;
|
|
}
|
|
}
|