2017-10-03 17:05:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Meoran\Images\Http\Controllers;
|
|
|
|
|
|
|
|
use Closure;
|
2017-10-03 17:11:49 +02:00
|
|
|
use Illuminate\Http\Request;
|
2017-10-03 17:05:46 +02:00
|
|
|
use Laravel\Lumen\Routing\Controller as BaseController;
|
|
|
|
use Meoran\Images\Model\Image;
|
|
|
|
|
|
|
|
class ImagesController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
public function get($template, $filename)
|
|
|
|
{
|
|
|
|
switch (strtolower($template)) {
|
|
|
|
case 'original':
|
|
|
|
return $this->getOriginal($filename);
|
|
|
|
case 'download':
|
|
|
|
return $this->getDownload($filename);
|
|
|
|
default:
|
|
|
|
return $this->getImage($template, $filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 17:11:49 +02:00
|
|
|
public function upload(Request $request)
|
2017-10-03 17:05:46 +02:00
|
|
|
{
|
2017-10-03 17:11:49 +02:00
|
|
|
$this->validate($request, [
|
|
|
|
'image' => 'required|file|image'
|
|
|
|
]);
|
2017-10-03 17:05:46 +02:00
|
|
|
|
2017-10-03 17:11:49 +02:00
|
|
|
$image = new Image(['content' => $request->input('image')]);
|
2017-10-03 17:05:46 +02:00
|
|
|
$image->save();
|
|
|
|
|
|
|
|
return response()->json($image);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImage($template, $filename)
|
|
|
|
{
|
|
|
|
$template = $this->getTemplate($template);
|
|
|
|
$path = $this->getImagePathOrAbort($filename);
|
|
|
|
|
|
|
|
$lifetime = config('image.lifetime');
|
|
|
|
if (empty($lifetime)) {
|
|
|
|
$content = $this->applyTemplate($template,$path)->encode();
|
|
|
|
} else {
|
|
|
|
$content = app('image')->cache(function ($image) use ($template, $path) {
|
|
|
|
$this->applyTemplate($template,$path,$image);
|
|
|
|
return $image;
|
|
|
|
}, $lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->buildResponse($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
} else {
|
|
|
|
// build from filter template
|
|
|
|
return $image->make($path)->filter($template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns corresponding template object from given template name
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function getTemplate($template)
|
|
|
|
{
|
|
|
|
$template = config("image.templates.{$template}");
|
|
|
|
switch (true) {
|
|
|
|
// closure template found
|
|
|
|
case is_callable($template):
|
|
|
|
return $template;
|
|
|
|
|
|
|
|
// filter template found
|
|
|
|
case class_exists($template):
|
|
|
|
return new $template;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// template not found
|
|
|
|
abort(404);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildResponse($content)
|
|
|
|
{
|
|
|
|
// define mime type
|
|
|
|
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
|
|
|
|
|
|
|
|
// return http response
|
|
|
|
return response($content, 200, array(
|
|
|
|
'Content-Type' => $mime,
|
|
|
|
'Cache-Control' => 'max-age=' . (config('image.lifetime') * 60) . ', public',
|
|
|
|
'Etag' => md5($content)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|