test
This commit is contained in:
@@ -10,79 +10,71 @@ class ImageResize
|
||||
{
|
||||
/**
|
||||
* @param string $type
|
||||
* @return string
|
||||
* @return string relative path used as S3 key, e.g. uploads/posters/thumbs/2024/01/01
|
||||
*/
|
||||
private function mkdir(string $type)
|
||||
private function thumbFolder(string $type): string
|
||||
{
|
||||
$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;
|
||||
return "uploads/{$type}/thumbs/{$folder}";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $size
|
||||
* @param $type
|
||||
* @param bool $deleteOriginal
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
* Resize an image and upload to S3/MinIO (or save locally).
|
||||
*
|
||||
* @param string $path Relative path of the source image (from 'public' disk = storage/app/public/)
|
||||
* @param int $size Target width in pixels
|
||||
* @param string $type Subfolder name (posters, screens, brands …)
|
||||
* @param bool $deleteOriginal Delete the source file after resizing
|
||||
* @return string Relative path (S3 key) of the generated thumb
|
||||
*/
|
||||
public function resize($path, $size, $type, $deleteOriginal = false)
|
||||
public function resize(string $path, int $size, string $type, bool $deleteOriginal = false): string
|
||||
{
|
||||
// 1. Resolve source path
|
||||
$srcPath = file_exists(public_path($path))
|
||||
? public_path($path)
|
||||
: storage_path('app/public/' . $path);
|
||||
// 1. Locate the source file (stored via ->store('temp', 'public'))
|
||||
$srcPath = storage_path('app/public/' . $path);
|
||||
|
||||
if (!file_exists($srcPath)) {
|
||||
if (file_exists($path)) {
|
||||
$srcPath = $path;
|
||||
} else {
|
||||
// Last ditch effort: try to get from S3 if it's not local
|
||||
if (env('FILESYSTEM_DISK') == 's3' && Storage::disk('s3')->exists($path)) {
|
||||
$fileContent = Storage::disk('s3')->get($path);
|
||||
$tmpLocal = storage_path('app/public/temp_' . basename($path));
|
||||
file_put_contents($tmpLocal, $fileContent);
|
||||
$srcPath = $tmpLocal;
|
||||
$deleteOriginal = true; // Clean up the temp download
|
||||
} else {
|
||||
throw new \Exception("Source image not found: " . $path);
|
||||
}
|
||||
}
|
||||
throw new \Exception("Source image not found: {$srcPath}");
|
||||
}
|
||||
|
||||
// 2. Prepare thumb destination
|
||||
$name = basename($path);
|
||||
$folder = $this->mkdir($type);
|
||||
$path_thumb = "{$folder}/{$name}";
|
||||
$absPathThumb = public_path($path_thumb);
|
||||
// 2. Resize into a temp file in storage/app/public/tmp/
|
||||
$tmpDir = storage_path('app/public/tmp');
|
||||
if (!is_dir($tmpDir)) {
|
||||
mkdir($tmpDir, 0775, true);
|
||||
}
|
||||
|
||||
$thumbFilename = basename($path);
|
||||
$tmpThumb = "{$tmpDir}/{$thumbFilename}";
|
||||
|
||||
// 3. Resize and Save Thumb
|
||||
$img = Imagee::make($srcPath);
|
||||
$img->resize($size, null, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
$img->save($absPathThumb, 100);
|
||||
$img->save($tmpThumb, 90);
|
||||
|
||||
// 4. Handle S3 Upload
|
||||
if (env('FILESYSTEM_DISK') == 's3') {
|
||||
Storage::disk('s3')->put($path_thumb, file_get_contents($absPathThumb));
|
||||
// Remove local thumb after S3 upload
|
||||
if (file_exists($absPathThumb)) {
|
||||
unlink($absPathThumb);
|
||||
// 3. Upload thumb to S3/MinIO
|
||||
$thumbKey = $this->thumbFolder($type) . '/' . $thumbFilename;
|
||||
|
||||
if (env('FILESYSTEM_DISK') === 's3') {
|
||||
Storage::disk('s3')->put($thumbKey, file_get_contents($tmpThumb));
|
||||
} else {
|
||||
// Local: move to public/uploads/…/thumbs/…
|
||||
$localDir = public_path(dirname($thumbKey));
|
||||
if (!is_dir($localDir)) {
|
||||
mkdir($localDir, 0775, true);
|
||||
}
|
||||
rename($tmpThumb, public_path($thumbKey));
|
||||
}
|
||||
|
||||
// 5. Cleanup original if requested
|
||||
// 4. Clean up tmp thumb
|
||||
if (file_exists($tmpThumb)) {
|
||||
unlink($tmpThumb);
|
||||
}
|
||||
|
||||
// 5. Clean up original temp file if requested
|
||||
if ($deleteOriginal && file_exists($srcPath)) {
|
||||
unlink($srcPath);
|
||||
}
|
||||
|
||||
return $path_thumb;
|
||||
return $thumbKey;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user