images/src/Http/Controllers/ImagesController.php

202 lines
5.3 KiB
PHP
Raw Normal View History

2017-10-03 17:05:46 +02:00
<?php
namespace Meoran\Images\Http\Controllers;
2020-11-27 23:35:34 +01:00
use App\Http\Controllers\Controller;
2017-10-03 17:05:46 +02:00
use Closure;
2020-11-27 23:35:34 +01:00
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
2017-10-03 17:11:49 +02:00
use Illuminate\Http\Request;
2020-11-27 23:35:34 +01:00
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
2017-10-03 17:05:46 +02:00
use Meoran\Images\Model\Image;
2017-10-10 01:07:03 +02:00
use Meoran\Images\Templates\Custom;
2017-10-03 17:05:46 +02:00
2020-11-27 23:35:34 +01:00
/**
* Class ImagesController
* @package Meoran\Images\Http\Controllers
*/
class ImagesController extends Controller
2017-10-03 17:05:46 +02:00
{
2020-11-27 23:35:34 +01:00
/**
* @param $filename
* @return Application|ResponseFactory|Response
*/
2017-10-10 01:07:03 +02:00
public function get($filename)
2017-10-03 17:05:46 +02:00
{
2017-10-10 01:07:03 +02:00
$template = app('request')->input('template');
2017-10-04 16:46:56 +02:00
if ($template === null) {
2018-09-04 13:29:02 +02:00
$template = 'large';
2017-10-04 16:46:56 +02:00
}
2017-10-03 17:05:46 +02:00
switch (strtolower($template)) {
case 'original':
return $this->getOriginal($filename);
case 'download':
return $this->getDownload($filename);
default:
return $this->getImage($template, $filename);
}
}
2020-11-27 23:35:34 +01:00
/**
* @param Request $request
* @return JsonResponse
* @throws ValidationException
*/
public function upload(Request $request): JsonResponse
2017-10-03 17:05:46 +02:00
{
2017-10-03 17:11:49 +02:00
$this->validate($request, [
2017-10-04 16:46:56 +02:00
'image' => 'required|file|image'
2017-10-03 17:11:49 +02:00
]);
2017-10-03 17:05:46 +02:00
2018-10-16 00:04:21 +02:00
$content = $request->file('image');
if (empty($content)) {
$content = $request->input('image');
}
$image = new Image(['content' => $content]);
2017-10-03 17:05:46 +02:00
$image->save();
return response()->json($image);
}
2018-09-04 12:27:33 +02:00
public function delete($id)
{
2017-10-21 09:03:04 +02:00
$image = Image::findOrFail($id);
$image->delete();
2018-09-04 12:27:33 +02:00
return response()->json("Delete " . $id . " succesfully");
2017-10-21 09:03:04 +02:00
}
2017-10-03 17:05:46 +02:00
private function getOriginal($filename)
{
$path = $this->getImagePathOrAbort($filename);
return $this->buildResponse(file_get_contents($path));
}
private function getDownload($filename)
{
$response = $this->getOriginal($filename);
return $response->header(
'Content-Disposition',
'attachment; filename=' . $filename
);
}
private function getImagePathOrAbort($filename)
{
$path = Image::getAbsolutePath($filename);
if (is_file($path)) {
return $path;
}
abort(404);
}
2020-11-27 23:35:34 +01:00
/**
* @param $template
* @param $filename
* @return Application|ResponseFactory|Response
* @throws BindingResolutionException
*/
2017-10-03 17:05:46 +02:00
public function getImage($template, $filename)
{
$template = $this->getTemplate($template);
$path = $this->getImagePathOrAbort($filename);
$lifetime = config('image.lifetime');
if (empty($lifetime)) {
2017-10-04 16:46:56 +02:00
$content = $this->applyTemplate($template, $path)->encode();
2017-10-03 17:05:46 +02:00
} else {
$content = app('image')->cache(function ($image) use ($template, $path) {
2017-10-04 16:46:56 +02:00
$this->applyTemplate($template, $path, $image);
2017-10-03 17:05:46 +02:00
return $image;
}, $lifetime);
}
return $this->buildResponse($content);
}
2020-11-27 23:35:34 +01:00
/**
* @param $template
* @param $path
* @param null $image
* @return mixed
* @throws BindingResolutionException
*/
2017-10-03 17:05:46 +02:00
private function applyTemplate($template, $path, $image = null)
{
if (empty($image)) {
$image = app('image');
}
if ($template instanceof Closure) {
// build from closure callback template
return $template($image->make($path));
2020-11-27 23:35:34 +01:00
}
// build from filter template
$res = $image->make($path)->filter($template);
if ($res === null) {
abort(404);
2017-10-03 17:05:46 +02:00
}
}
/**
* Returns corresponding template object from given template name
*
* @param string $template
* @return mixed
*/
2017-10-10 01:07:03 +02:00
private function getTemplate($templateName)
2017-10-03 17:05:46 +02:00
{
2017-10-10 01:07:03 +02:00
$template = config("image.templates.{$templateName}");
2017-10-03 17:05:46 +02:00
switch (true) {
// closure template found
case is_callable($template):
return $template;
// filter template found
case class_exists($template):
return new $template;
default:
2017-10-10 01:07:03 +02:00
return $this->getCustomTemplate($templateName);
2017-10-03 17:05:46 +02:00
}
}
2020-11-27 23:35:34 +01:00
/**
* @param $content
* @return Application|ResponseFactory|Response
*/
2017-10-03 17:05:46 +02:00
private function buildResponse($content)
{
// define mime type
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
2018-09-04 12:27:33 +02:00
$timeInSecondsToExpire = config('image.lifetime') * 60;
2017-10-03 17:05:46 +02:00
// return http response
return response($content, 200, array(
'Content-Type' => $mime,
2018-09-04 12:27:33 +02:00
'Cache-Control' => 'max-age=' . $timeInSecondsToExpire . ', public',
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', time() + $timeInSecondsToExpire),
2017-10-03 17:05:46 +02:00
'Etag' => md5($content)
));
}
2017-10-10 01:07:03 +02:00
2020-11-27 23:35:34 +01:00
/**
* @param $templateName
* @return Custom
*/
private function getCustomTemplate($templateName): Custom
2017-10-10 01:07:03 +02:00
{
$custom = new Custom();
$custom->actions = [$templateName];
return $custom;
}
2017-10-03 17:05:46 +02:00
}