57 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Services;
 | 
						|
 | 
						|
use Illuminate\Support\Facades\Auth;
 | 
						|
use Intervention\Image\Constraint;
 | 
						|
use Intervention\Image\Facades\Image;
 | 
						|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 | 
						|
 | 
						|
class ImageService
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @param string $image path to image
 | 
						|
     * @param array $options option to make thumbnail
 | 
						|
     *
 | 
						|
     * @return \Intervention\Image\Image
 | 
						|
     */
 | 
						|
    public function makeThumbnail($image, $options = [])
 | 
						|
    {
 | 
						|
        if (!is_readable($image)) {
 | 
						|
            throw new NotFoundHttpException();
 | 
						|
        }
 | 
						|
 | 
						|
        $img = Image::make($image);
 | 
						|
        if (empty($options)) {
 | 
						|
            return $img;
 | 
						|
        }
 | 
						|
 | 
						|
        $folder = dirname($img->basePath());
 | 
						|
        $file_name = basename($image);
 | 
						|
 | 
						|
        $width = null;
 | 
						|
        if (array_key_exists('width', $options)) {
 | 
						|
            $width = $options['width'];
 | 
						|
            $file_name = sprintf('w%u-%s', $width, $file_name);
 | 
						|
        }
 | 
						|
 | 
						|
        $height = null;
 | 
						|
        if (array_key_exists('$height', $options)) {
 | 
						|
            $height = $options['$height'];
 | 
						|
            $file_name = sprintf('h%u-%s', $height, $file_name);
 | 
						|
        }
 | 
						|
 | 
						|
        $full_path = sprintf('%s/%s', $folder, $file_name);
 | 
						|
        if (!array_key_exists('force', $options) && is_readable($full_path)) {
 | 
						|
            return Image::make($full_path);
 | 
						|
        }
 | 
						|
 | 
						|
        //http://image.intervention.io/getting_started/
 | 
						|
        $img->resize($width, $height, static function (Constraint $constraint) {
 | 
						|
            $constraint->aspectRatio();
 | 
						|
        })->save(sprintf('%s/%s', $folder, $file_name));
 | 
						|
 | 
						|
        return $img;
 | 
						|
    }
 | 
						|
}
 |