UploadController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Storage;
  6. use Intervention\Image\Facades\Image;
  7. use yasmuru\LaravelTinify\Facades\Tinify;
  8. class UploadController extends Controller
  9. {
  10. /**
  11. * @param Request $request
  12. * @return \Illuminate\Http\JsonResponse
  13. */
  14. public function image(Request $request)
  15. {
  16. if ($request->hasFile('file')
  17. && $request->file('file')->isValid()
  18. && in_array($request->file->extension(), ["png", "jpg", "jpeg", "gif"])
  19. ) {
  20. $path = $request->file->store(date('Ymd'), config('vienblog.disks.article_image'));
  21. $url = Storage::disk(config('vienblog.disks.article_image'))->url($path);
  22. if (env('TINIFY_APIKEY', '') && in_array($request->file->extension(), ["png", "jpg", "jpeg"])) {
  23. try {
  24. set_time_limit(300);
  25. $oldPath = public_path($url);
  26. $result = Tinify::fromFile($oldPath);
  27. $result->toFile($oldPath);
  28. } catch (\Exception $e){
  29. return response()->json(['filename' => $url]);
  30. }
  31. }
  32. // $img = Image::make($oldPath);
  33. //
  34. // $thumbnailPath = pathinfo($oldPath, PATHINFO_DIRNAME).'/thumbnail/';
  35. //
  36. // if(!file_exists($thumbnailPath)){
  37. // mkdir ($thumbnailPath);
  38. // }
  39. //
  40. // $newPath = $thumbnailPath.pathinfo($oldPath, PATHINFO_FILENAME).'.jpg';
  41. //
  42. // $img->text('The quick brown fox jumps over the lazy dog.', 120, 100);
  43. //
  44. // $img->save($newPath, 100);
  45. return response()->json(['filename' => $url]);
  46. }
  47. }
  48. }