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; } }