1. 程式人生 > >TP框架圖片壓縮/上傳

TP框架圖片壓縮/上傳

<-- 在前端的程式碼 -->
<
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