Skip to content

Commit 0518ab4

Browse files
author
Đặng Thế Anh
committed
feat(imgproxy): implement image proxy service with new route and controller handler for dynamic image manipulation.
1 parent 376d8f9 commit 0518ab4

3 files changed

Lines changed: 409 additions & 0 deletions

File tree

src/Http/Controllers/Frontend/AddonController.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* JUZAWEB CMS - Laravel CMS for Your Project
45
*
@@ -20,6 +21,7 @@
2021
use Juzaweb\Modules\Admin\Networks\Facades\Network;
2122
use Juzaweb\Modules\Core\Contracts\Viewable;
2223
use Juzaweb\Modules\Core\Http\Controllers\Controller;
24+
use Juzaweb\Modules\Core\Services\ImgProxyService;
2325

2426
class AddonController extends Controller
2527
{
@@ -201,4 +203,46 @@ private function getMimeType(string $extension): string
201203
default => 'application/octet-stream',
202204
};
203205
}
206+
207+
/**
208+
* Handle image proxy request
209+
*/
210+
public function proxy(string $methodParam, string $hash, Request $request, ImgProxyService $imgProxyService)
211+
{
212+
try {
213+
// Parse method and dimensions from methodParam (e.g., "resize:800x600")
214+
$method = $methodParam;
215+
$width = null;
216+
$height = null;
217+
218+
if (str_contains($methodParam, ':')) {
219+
[$method, $size] = explode(':', $methodParam, 2);
220+
[$width, $height] = array_pad(explode('x', $size), 2, null);
221+
$width = $width === 'auto' ? null : (int)$width;
222+
$height = $height === 'auto' ? null : (int)$height;
223+
}
224+
225+
// Handle image via service
226+
$result = $imgProxyService->handle($method, $hash, $width, $height);
227+
228+
// Clear any output buffers
229+
while (ob_get_level() > 0) {
230+
ob_end_clean();
231+
}
232+
233+
// Return response with proper headers
234+
return response($result['data'])
235+
->header('Content-Type', $result['contentType'])
236+
->header('Content-Length', $result['contentLength'])
237+
->header('X-Content-Type-Options', 'nosniff')
238+
->header('X-Accel-Buffering', 'no')
239+
->header('Cache-Control', 'public, max-age=31536000');
240+
} catch (\InvalidArgumentException $e) {
241+
return response('Error: ' . $e->getMessage(), 400)
242+
->header('Content-Type', 'text/plain');
243+
} catch (\Throwable $e) {
244+
return response('Error: ' . $e->getMessage(), 500)
245+
->header('Content-Type', 'text/plain');
246+
}
247+
}
204248
}

0 commit comments

Comments
 (0)