85 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?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.');
 | 
						|
        }
 | 
						|
 | 
						|
        $original = sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), $image);
 | 
						|
        $thumb = sprintf('%s/%s/300-%s', storage_path('app'), Auth::user()->getFolder(), $image);
 | 
						|
        $img = Image::make($original);
 | 
						|
        if ('o:full' === $options) {
 | 
						|
            if (!is_readable($original)) {
 | 
						|
                throw new NotFoundHttpException();
 | 
						|
            }
 | 
						|
        } elseif (!is_readable($thumb)) {
 | 
						|
            if (!is_readable($original)) {
 | 
						|
                throw new NotFoundHttpException();
 | 
						|
            }
 | 
						|
            $options = explode('!', $options);
 | 
						|
            $width = null;
 | 
						|
            $height = null;
 | 
						|
            foreach ($options as $option) {
 | 
						|
                [$current_option_name, $current_option_value] = explode(':', $option);
 | 
						|
                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();
 | 
						|
            })->save(sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), sprintf('300-%s', $image)));
 | 
						|
        } else {
 | 
						|
            $img = Image::make($thumb);
 | 
						|
        }
 | 
						|
 | 
						|
        return $img->response(File::extension($original));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @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,
 | 
						|
        ]);
 | 
						|
    }
 | 
						|
}
 |