Ajoute la gallerie

On utilise une libraire pour créer les miniatures
Fix #4
This commit is contained in:
Clement Desmidt 2020-03-10 13:09:05 +01:00
parent 99ec67affb
commit 7cec21bc21
10 changed files with 432 additions and 20 deletions

View File

@ -118,6 +118,11 @@ class HomeController extends Controller
} }
} }
/**
* @param TagDetectorService $tag_detector
* @return mixed
* @throws \Exception
*/
public function store(TagDetectorService $tag_detector) public function store(TagDetectorService $tag_detector)
{ {
$user_id = Auth::user()->getAuthIdentifier(); $user_id = Auth::user()->getAuthIdentifier();

View 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,
]);
}
}

View File

@ -42,20 +42,4 @@ class User extends Authenticatable
$arrayHash = str_split(strtolower(md5($this->id))); $arrayHash = str_split(strtolower(md5($this->id)));
return sprintf('%s/%s', $arrayHash[0], $arrayHash[1]); 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));
}
} }

View File

@ -10,8 +10,10 @@
"require": { "require": {
"php": "^7.2", "php": "^7.2",
"fideloper/proxy": "^4.0", "fideloper/proxy": "^4.0",
"intervention/image": "^2.5",
"laravel/framework": "^6.0", "laravel/framework": "^6.0",
"laravel/tinker": "^1.0" "laravel/tinker": "^1.0",
"lavary/laravel-menu": "^1.7"
}, },
"require-dev": { "require-dev": {
"facade/ignition": "^1.4", "facade/ignition": "^1.4",

285
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "3f43a645b4cb3d85ae04049046b0845a", "content-hash": "a8999b4d9f518e7a9608ff59c6ba40e9",
"packages": [ "packages": [
{ {
"name": "dnoegel/php-xdg-base-dir", "name": "dnoegel/php-xdg-base-dir",
@ -380,6 +380,147 @@
], ],
"time": "2019-09-03T16:45:42+00:00" "time": "2019-09-03T16:45:42+00:00"
}, },
{
"name": "guzzlehttp/psr7",
"version": "1.6.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "239400de7a173fe9901b9ac7c06497751f00727a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/239400de7a173fe9901b9ac7c06497751f00727a",
"reference": "239400de7a173fe9901b9ac7c06497751f00727a",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0",
"ralouphie/getallheaders": "^2.0.5 || ^3.0.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"ext-zlib": "*",
"phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8"
},
"suggest": {
"zendframework/zend-httphandlerrunner": "Emit PSR-7 responses"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.6-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"psr-7",
"request",
"response",
"stream",
"uri",
"url"
],
"time": "2019-07-01T23:21:34+00:00"
},
{
"name": "intervention/image",
"version": "2.5.1",
"source": {
"type": "git",
"url": "https://github.com/Intervention/image.git",
"reference": "abbf18d5ab8367f96b3205ca3c89fb2fa598c69e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/image/zipball/abbf18d5ab8367f96b3205ca3c89fb2fa598c69e",
"reference": "abbf18d5ab8367f96b3205ca3c89fb2fa598c69e",
"shasum": ""
},
"require": {
"ext-fileinfo": "*",
"guzzlehttp/psr7": "~1.1",
"php": ">=5.4.0"
},
"require-dev": {
"mockery/mockery": "~0.9.2",
"phpunit/phpunit": "^4.8 || ^5.7"
},
"suggest": {
"ext-gd": "to use GD library based image processing.",
"ext-imagick": "to use Imagick based image processing.",
"intervention/imagecache": "Caching extension for the Intervention Image library"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.4-dev"
},
"laravel": {
"providers": [
"Intervention\\Image\\ImageServiceProvider"
],
"aliases": {
"Image": "Intervention\\Image\\Facades\\Image"
}
}
},
"autoload": {
"psr-4": {
"Intervention\\Image\\": "src/Intervention/Image"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@olivervogel.com",
"homepage": "http://olivervogel.com/"
}
],
"description": "Image handling and manipulation library with support for Laravel integration",
"homepage": "http://image.intervention.io/",
"keywords": [
"gd",
"image",
"imagick",
"laravel",
"thumbnail",
"watermark"
],
"time": "2019-11-02T09:15:47+00:00"
},
{ {
"name": "jakub-onderka/php-console-color", "name": "jakub-onderka/php-console-color",
"version": "v0.2", "version": "v0.2",
@ -676,6 +817,58 @@
], ],
"time": "2019-08-07T15:10:45+00:00" "time": "2019-08-07T15:10:45+00:00"
}, },
{
"name": "lavary/laravel-menu",
"version": "v1.7.7",
"source": {
"type": "git",
"url": "https://github.com/lavary/laravel-menu.git",
"reference": "5592778b3193ae561614ecb107467469b694ab11"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lavary/laravel-menu/zipball/5592778b3193ae561614ecb107467469b694ab11",
"reference": "5592778b3193ae561614ecb107467469b694ab11",
"shasum": ""
},
"require": {
"illuminate/support": ">=5.0",
"illuminate/view": ">=5.0",
"php": ">=5.4.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Lavary\\Menu\\ServiceProvider"
],
"aliases": {
"Menu": "Lavary\\Menu\\Facade"
}
}
},
"autoload": {
"psr-0": {
"Lavary\\Menu\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Lavary",
"email": "mrl.8081@gmail.com"
}
],
"description": "A quick way to create menus in Laravel 5",
"homepage": "https://github.com/lavary/laravel-menu",
"keywords": [
"laravel"
],
"time": "2019-09-06T16:28:16+00:00"
},
{ {
"name": "league/flysystem", "name": "league/flysystem",
"version": "1.0.55", "version": "1.0.55",
@ -1164,6 +1357,56 @@
], ],
"time": "2017-02-14T16:28:37+00:00" "time": "2017-02-14T16:28:37+00:00"
}, },
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06T14:39:51+00:00"
},
{ {
"name": "psr/log", "name": "psr/log",
"version": "1.1.0", "version": "1.1.0",
@ -1333,6 +1576,46 @@
], ],
"time": "2018-10-13T15:16:03+00:00" "time": "2018-10-13T15:16:03+00:00"
}, },
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",
"source": {
"type": "git",
"url": "https://github.com/ralouphie/getallheaders.git",
"reference": "120b605dfeb996808c31b6477290a714d356e822"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822",
"reference": "120b605dfeb996808c31b6477290a714d356e822",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^5 || ^6.5"
},
"type": "library",
"autoload": {
"files": [
"src/getallheaders.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ralph Khattar",
"email": "ralph.khattar@gmail.com"
}
],
"description": "A polyfill for getallheaders.",
"time": "2019-03-08T08:55:37+00:00"
},
{ {
"name": "ramsey/uuid", "name": "ramsey/uuid",
"version": "3.8.0", "version": "3.8.0",

View File

@ -176,7 +176,7 @@ return [
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
\App\Providers\TagDetectorProvider::class, \App\Providers\TagDetectorProvider::class,
Intervention\Image\ImageServiceProvider::class,
], ],
/* /*
@ -209,6 +209,7 @@ return [
'File' => Illuminate\Support\Facades\File::class, 'File' => Illuminate\Support\Facades\File::class,
'Gate' => Illuminate\Support\Facades\Gate::class, 'Gate' => Illuminate\Support\Facades\Gate::class,
'Hash' => Illuminate\Support\Facades\Hash::class, 'Hash' => Illuminate\Support\Facades\Hash::class,
'Image' => Intervention\Image\Facades\Image::class,
'Lang' => Illuminate\Support\Facades\Lang::class, 'Lang' => Illuminate\Support\Facades\Lang::class,
'Log' => Illuminate\Support\Facades\Log::class, 'Log' => Illuminate\Support\Facades\Log::class,
'Mail' => Illuminate\Support\Facades\Mail::class, 'Mail' => Illuminate\Support\Facades\Mail::class,

20
config/image.php Normal file
View File

@ -0,0 +1,20 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports "GD Library" and "Imagick" to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP's "GD Library" implementation is used.
|
| Supported: "gd", "imagick"
|
*/
'driver' => 'gd'
];

View File

@ -0,0 +1,33 @@
@extends('layouts.connected')
@section('content')
<div class="container">
<div class="page-header">
<h1 class="page-title">
{{ __('My gallery') }}
</h1>
</div>
<div class="row flex-fill">
@foreach($posts as $post)
<div class="col-md-6 col-xl-4">
<div class="card">
<div class="card-status bg-blue"></div>
<div class="card-header">
<h3 class="card-title">{{ ucfirst(\Carbon\Carbon::instance($post->date_post)->diffForHumans()) }}</h3>
<div class="card-options">
<a href="#" class="card-options-collapse" data-toggle="card-collapse"><i class="fe fe-chevron-up"></i></a>
</div>
</div>
<div class="card-body">
<p>
<a href="{{ route('display_image', ['post_id' => $post->id, 'options' => 'o:full', 'image_name' => $post->image]) }}">
<img src="{{ route('display_image', ['post_id' => $post->id, 'options' => 'w:300', 'image_name' => $post->image]) }}"/>
</a>
</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
@endsection

View File

@ -133,7 +133,7 @@
</div> </div>
</div> </div>
<div class="card-body @if($today->format('Y-m-d') === \Carbon\Carbon::instance($post->date_post)->format('Y-m-d')) already @endif "> <div class="card-body @if($today->format('Y-m-d') === \Carbon\Carbon::instance($post->date_post)->format('Y-m-d')) already @endif ">
@if($post->image != '')<p><img src="{{ Auth::user()->getImageData($post) }}"/></p>@endif @if($post->image != '')<p><img src="{{ route('display_image', ['post_id' => $post->id, 'options' => 'w:300', 'image_name' => $post->image]) }}"/></p>@endif
{!! $tag_detector->linkTagFrom($post->content) !!} {!! $tag_detector->linkTagFrom($post->content) !!}
</div> </div>
<div class="card-footer"> <div class="card-footer">

View File

@ -19,3 +19,5 @@ Route::post('store', 'HomeController@store')->name('store');
Route::post('delete', 'HomeController@delete')->name('delete'); Route::post('delete', 'HomeController@delete')->name('delete');
Route::post('edit', 'HomeController@edit')->name('edit'); Route::post('edit', 'HomeController@edit')->name('edit');
Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats
Route::get('/display/{post_id}/{options}/{image_name}', 'ImageController@display')->name('display_image');
Route::get('/gallery', 'ImageController@gallery')->name('gallery');