Files
getgreen-backend/app/Api/ImageResize.php

61 lines
1.5 KiB
PHP
Executable File

<?php
namespace App\Api;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image as Imagee;
class ImageResize
{
/**
* @param string $type
* @return string
*/
private function mkdir(string $type)
{
$folder = Carbon::now()->format('Y/m/d');
$path = "uploads/{$type}/thumbs/{$folder}";
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
return $path;
}
/**
* @param $path
* @param $size
* @param $type
* @return string
*/
public function resize($path, $size, $type)
{
$name = basename($path);
$folder = $this->mkdir($type);
$path_thumb = "{$folder}/{$name}";
if (env('FILESYSTEM_DISK') == 's3') {
try{
$file = Storage::disk("public")->get($path);
}catch(Exception $e){
$file = file_get_contents($path);
}
$img = Imagee::make($file);
$img->resize($size, null, function ($constraint) {
$constraint->aspectRatio();
});
// delete temp file
$img->save("{$path_thumb}", 100);
Storage::disk('s3')->put($path_thumb, file_get_contents($path_thumb));
} else {
$img = Imagee::make(public_path($path));
$img->resize($size, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save(public_path() . "/{$path_thumb}", 100);
}
return $path_thumb;
}
}