✨ Ajoute la gallerie
On utilise une libraire pour créer les miniatures Fix #4
This commit is contained in:
@@ -118,6 +118,11 @@ class HomeController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TagDetectorService $tag_detector
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function store(TagDetectorService $tag_detector)
|
||||
{
|
||||
$user_id = Auth::user()->getAuthIdentifier();
|
||||
|
82
app/Http/Controllers/ImageController.php
Normal file
82
app/Http/Controllers/ImageController.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Post;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Intervention\Image\Constraint;
|
||||
use Intervention\Image\Facades\Image;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
class ImageController extends Controller
|
||||
{
|
||||
/**
|
||||
* @param int $post_id
|
||||
* @param string|array $options
|
||||
* @param string $image
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function display($post_id, $options = 'o:full', $image)
|
||||
{
|
||||
if (Auth::guest()) {
|
||||
throw new UnauthorizedHttpException('Vous devez être connecté pour voir cette image.');
|
||||
}
|
||||
|
||||
$post = Post::find($post_id);
|
||||
|
||||
if (Auth::user()->getAuthIdentifier() !== (int)$post->user_id) {
|
||||
throw new UnauthorizedHttpException('Cette image ne vous appartient pas.');
|
||||
}
|
||||
|
||||
$path = sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), $image);
|
||||
if (!is_readable($path)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
|
||||
$img = Image::make($path);
|
||||
|
||||
if ('o:full' !== $options) {
|
||||
$options = explode('!', $options);
|
||||
$width = null;
|
||||
$height = null;
|
||||
foreach ($options as $option) {
|
||||
$current_option = explode(':', $option);
|
||||
foreach ($current_option as $current_option_name => $current_option_value) {
|
||||
switch ($current_option_name) {
|
||||
case 'w':
|
||||
$width = $current_option_value;
|
||||
break;
|
||||
case 'h':
|
||||
$height = $current_option_value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//http://image.intervention.io/getting_started/
|
||||
$img->resize($width, $height, static function (Constraint $constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
}
|
||||
|
||||
return $img->response(File::extension($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function gallery()
|
||||
{
|
||||
$posts_with_image = Post::where('user_id', Auth::user()->getAuthIdentifier())
|
||||
->where('image', '!=', 'null')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(30);
|
||||
|
||||
return view('gallery.my', [
|
||||
'posts' => $posts_with_image,
|
||||
]);
|
||||
}
|
||||
}
|
16
app/User.php
16
app/User.php
@@ -42,20 +42,4 @@ class User extends Authenticatable
|
||||
$arrayHash = str_split(strtolower(md5($this->id)));
|
||||
return sprintf('%s/%s', $arrayHash[0], $arrayHash[1]);
|
||||
}
|
||||
|
||||
public function getImageData(Post $post)
|
||||
{
|
||||
if (empty($post->image)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$path = sprintf('%s/%s/%s', storage_path('app'), $this->getFolder(), $post->image);
|
||||
|
||||
if (!is_readable($path)) {
|
||||
return '';
|
||||
}
|
||||
$type = pathinfo($path, PATHINFO_EXTENSION);
|
||||
$data = file_get_contents($path);
|
||||
return sprintf('data:image/%s;base64,%s', $type, base64_encode($data));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user