<-- 在前端的代碼 -->
<form action="{:url('index/user/personal')}" method="post" enctype="multipart/form-data">
<input type="file" name="image" required="required"></div>
</form>
$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);
}
如有錯誤, 請告知~
樓主CSDN地址 https://blog.csdn.net/weixin_42358094