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

65 lines
1.8 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(public_path($path))) {
mkdir(public_path($path), 0775, 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 {
// source can be in storage/app/public (from ->store('temp','public')) or public/
$srcPath = file_exists(public_path($path))
? public_path($path)
: storage_path('app/public/' . $path);
$img = Imagee::make($srcPath);
$img->resize($size, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save(public_path($path_thumb), 100);
}
return $path_thumb;
}
}