一、laravel图片上传方法
public function uploadPic($file) { $ext = $file->getClientOriginalExtension(); $ext_arr = array('gif', 'jpg', 'jpeg', 'bmp', 'png'); if (!in_array($ext, $ext_arr)) { return ['error_msg' => '图片格式错误']; } if ($file->isValid()) { $newName = date("YmdHis") . str_pad(mt_rand(0, 999999), 6, 0, STR_PAD_LEFT) . "." . $ext; $path_str = 'upload/img/' . date("Y") . "/" . date("md") . "/"; $url_path = public_path($path_str); if (!is_dir($url_path)) { mkdir($url_path, 0755, true); } $path = $file->move($url_path, $newName); if ($path) { $namePath = "/" . $path_str . $newName; return ['path' => $namePath]; } } return ['error_msg' => '上传失败']; }
二、图片处理类
<?php/** * Created by PhpStorm. * User: Administrator * Date: 2021/9/9 0009 * Time: 下午 5:03 */namespace App\Logic;use App\Http\Controllers\Controller;class ImgCompressLogic extends Controller{ //生成图片路径(保存路径、图片url) public function createImgPath($ext) { $newName = date("YmdHis") . str_pad(mt_rand(0, 999999), 6, 0, STR_PAD_LEFT) . "." . $ext; $path_str = 'upload/img/' . date("Y") . "/" . date("md") . "/"; $url_path = public_path($path_str); $newPath = $url_path . $newName; $namePath = "/" . $path_str . $newName; return [ 'savePath' => $newPath, 'namePath' => $namePath ]; } /** * 压缩图片 * @param $filename * @param $newx * @param $newy * @param $ext * @return string */ function resize_image($filename, $newx, $newy, $ext) { //根据后缀,由文件或 URL 创建一个新图象(内置函数) if ($ext == "jpg" || $ext == "jpeg") $im = imagecreatefromjpeg($filename); elseif ($ext == "png") $im = imagecreatefrompng($filename); elseif ($ext == "gif") $im = imagecreatefromgif($filename); //获取当前待修改图片像素(内置函数) $x = imagesx($im); $y = imagesy($im); //新建一个真彩色图像(内置函数) $im2 = imagecreatetruecolor($newx, $newy); //重采样拷贝部分图像并调整大小(内置函数) imagecopyresampled($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); //return $im2; $newPath = $this->createImgPath($ext); //生成图片相关路径 if ($ext == "jpg" || $ext == "jpeg") { imagejpeg($im2, $newPath); } elseif ($ext == "png") { imagepng($im2, $newPath); } elseif ($ext == "gif") { imagegif($im2, $newPath); } return $newPath['savePath']; } //计算图片宽高,并保存转换前的图片和转换后的图片 //本例已生成单边不大于2500像素的图片为例 public function calculateThePicture($imageFile) { list($width, $height) = getimagesize($imageFile); $ext = $imageFile->getClientOriginalExtension(); $ratio = $width / $height; if ($width <= 2500 && $height <= 2500) { return [ 'file' => $imageFile, 'ext' => $ext ]; } if ($width > $height) { if ($width > 2500) { $newWidth = 2500; $newHeight = round($newWidth / $ratio); } } if ($width == $height) { if ($width > 2500) { $newWidth = 2500; $newHeight = 2500; } } if ($width < $height) { if ($height > 2500) { $newHeight = 2500; $newWidth = round($newHeight * $ratio); } } if (isset($newWidth) && isset($newHeight)) { $uploadLogic = new UploadLogic(); $res = $uploadLogic->uploadPic($imageFile); if (!empty($res['error_msg'])) { return ['error_msg' => $res['error_msg']]; } $newImgPath = $this->resize_image(get_domain(1) . $res['path'], $newWidth, $newHeight, $ext); return [ 'file' => $newImgPath, 'ext' => $ext ]; } }}