$file = request()->file('image'); //获取图片
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/'); //将获取到的图片存入根/public/uploads/
// 成功上传后 获取上传信息
if($info){
// 输出 jpg
$imageType = $info->getExtension(); //输出的为图片后缀名(.jpg/.png)
// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
$image = $info->getSaveName(); //输出的为uploads后面的路径,可以打印查看
if($imageType == 'jpg'){
// 获取完整路径
$image = ROOT_PATH . "/public/uploads/head/" . $image;
// 加载图片资源
$src = @imagecreatefromjpeg($image);
list($width,$height) = getimagesize($image); //获取图片的高度
$newwidth = $width; //宽高可以设置, 楼主是想让它的宽高不变才没赋值
$newheight = $height;
$tmp = imagecreatetruecolor($newwidth,$newheight); //生成新的宽高
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //缩放图像
$output = imagejpeg($tmp, $image, 50); //第三个参数(0~100);越大越清晰,图片大小也高; png格式的为(1~9)
// imagedestroy($tmp); // 销毁原来图片,一般不使用
// 请参考jpg中的详解
}elseif($imageType == 'png'){
$image = ROOT_PATH . "/public/uploads/head/" . $image;
$src = @imagecreatefrompng($image);
list($width,$height) = getimagesize($image);
$newwidth = $width;
$newheight = $height;
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$output = imagepng($tmp, $image, 5); //这个图片的第三参数(1~9)
// imagedestroy($tmp);
}