一、laravel图片上传方法

  1. public function uploadPic($file)
  2. {
  3. $ext = $file->getClientOriginalExtension();
  4. $ext_arr = array('gif', 'jpg', 'jpeg', 'bmp', 'png');
  5. if (!in_array($ext, $ext_arr)) {
  6. return ['error_msg' => '图片格式错误'];
  7. }
  8. if ($file->isValid()) {
  9. $newName = date("YmdHis") . str_pad(mt_rand(0, 999999), 6, 0, STR_PAD_LEFT) . "." . $ext;
  10. $path_str = 'upload/img/' . date("Y") . "/" . date("md") . "/";
  11. $url_path = public_path($path_str);
  12. if (!is_dir($url_path)) {
  13. mkdir($url_path, 0755, true);
  14. }
  15. $path = $file->move($url_path, $newName);
  16. if ($path) {
  17. $namePath = "/" . $path_str . $newName;
  18. return ['path' => $namePath];
  19. }
  20. }
  21. return ['error_msg' => '上传失败'];
  22. }

二、图片处理类

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/9/9 0009
  6. * Time: 下午 5:03
  7. */
  8. namespace App\Logic;
  9. use App\Http\Controllers\Controller;
  10. class ImgCompressLogic extends Controller
  11. {
  12. //生成图片路径(保存路径、图片url)
  13. public function createImgPath($ext)
  14. {
  15. $newName = date("YmdHis") . str_pad(mt_rand(0, 999999), 6, 0, STR_PAD_LEFT) . "." . $ext;
  16. $path_str = 'upload/img/' . date("Y") . "/" . date("md") . "/";
  17. $url_path = public_path($path_str);
  18. $newPath = $url_path . $newName;
  19. $namePath = "/" . $path_str . $newName;
  20. return [
  21. 'savePath' => $newPath,
  22. 'namePath' => $namePath
  23. ];
  24. }
  25. /**
  26. * 压缩图片
  27. * @param $filename
  28. * @param $newx
  29. * @param $newy
  30. * @param $ext
  31. * @return string
  32. */
  33. function resize_image($filename, $newx, $newy, $ext)
  34. { //根据后缀,由文件或 URL 创建一个新图象(内置函数)
  35. if ($ext == "jpg" || $ext == "jpeg") $im = imagecreatefromjpeg($filename);
  36. elseif ($ext == "png") $im = imagecreatefrompng($filename);
  37. elseif ($ext == "gif") $im = imagecreatefromgif($filename);
  38. //获取当前待修改图片像素(内置函数)
  39. $x = imagesx($im);
  40. $y = imagesy($im);
  41. //新建一个真彩色图像(内置函数)
  42. $im2 = imagecreatetruecolor($newx, $newy);
  43. //重采样拷贝部分图像并调整大小(内置函数)
  44. imagecopyresampled($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
  45. //return $im2;
  46. $newPath = $this->createImgPath($ext); //生成图片相关路径
  47. if ($ext == "jpg" || $ext == "jpeg") {
  48. imagejpeg($im2, $newPath);
  49. } elseif ($ext == "png") {
  50. imagepng($im2, $newPath);
  51. } elseif ($ext == "gif") {
  52. imagegif($im2, $newPath);
  53. }
  54. return $newPath['savePath'];
  55. }
  56. //计算图片宽高,并保存转换前的图片和转换后的图片
  57. //本例已生成单边不大于2500像素的图片为例
  58. public function calculateThePicture($imageFile)
  59. {
  60. list($width, $height) = getimagesize($imageFile);
  61. $ext = $imageFile->getClientOriginalExtension();
  62. $ratio = $width / $height;
  63. if ($width <= 2500 && $height <= 2500) {
  64. return [
  65. 'file' => $imageFile,
  66. 'ext' => $ext
  67. ];
  68. }
  69. if ($width > $height) {
  70. if ($width > 2500) {
  71. $newWidth = 2500;
  72. $newHeight = round($newWidth / $ratio);
  73. }
  74. }
  75. if ($width == $height) {
  76. if ($width > 2500) {
  77. $newWidth = 2500;
  78. $newHeight = 2500;
  79. }
  80. }
  81. if ($width < $height) {
  82. if ($height > 2500) {
  83. $newHeight = 2500;
  84. $newWidth = round($newHeight * $ratio);
  85. }
  86. }
  87. if (isset($newWidth) && isset($newHeight)) {
  88. $uploadLogic = new UploadLogic();
  89. $res = $uploadLogic->uploadPic($imageFile);
  90. if (!empty($res['error_msg'])) {
  91. return ['error_msg' => $res['error_msg']];
  92. }
  93. $newImgPath = $this->resize_image(get_domain(1) . $res['path'], $newWidth, $newHeight, $ext);
  94. return [
  95. 'file' => $newImgPath,
  96. 'ext' => $ext
  97. ];
  98. }
  99. }
  100. }