Commit afd45d39 authored by Sylvain's avatar Sylvain

Ajout helper Image

parent f816d73c
<?php
use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
if ( !function_exists('optimize_image')) {
/**
* Conversion et optimisation d'une image
* @param string $path_image chemin vers l'image
* @param integer $width Largeur de l'image
* @param integer $height Hauteur de l'image (null pour automatique)
* @param string $type Type mime de l'image
* @param integer $quality Qualité de l'image, niveau de compression
* @return void
*/
function optimize_image($path_image, $width = 1920, $height = null, $type = 'jpg', $quality = 80)
{
// On défini les format d'image accepté
$imageMimeTypes = ['image/jpeg', 'image/gif', 'image/png'];
if (!File::isFile($path_image) || !in_array(mime_content_type($path_image), $imageMimeTypes)) {
return false;
}
$img = Image::make($path_image);
$img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->encode($type, $quality);
$img->save();
}
}
/**
* Fonction pour générer des miniatures d'image
*/
if ( !function_exists('make_thumb_image')) {
/**
* Conversion en miniature d'une image
* @param string $path_image chemin vers l'image
* @param integer $width Largeur de l'image
* @param integer $height Hauteur de l'image (null pour automatique)
* @param string $type Type mime de l'image
* @param integer $quality Qualité de l'image, niveau de compression
* @return void
*/
function make_thumb_image($path_image, $width = 200, $height = null, $type = 'jpg', $quality = 80)
{
$imageMimeTypes = ['image/jpeg', 'image/gif', 'image/png'];
if (!File::isFile($path_image) || !in_array(mime_content_type($path_image), $imageMimeTypes)) {
return false;
}
$img = Image::make($path_image);
$filename = $img->filename . '_thumb.'. $format;
$pathname = $img->dirname . '/' . $filename;
$img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->encode($type, $quality);
$img->save($pathname);
return $pathname;
}
}
......@@ -96,7 +96,7 @@ if ( !function_exists('str_indent')) {
}
}
if (! function_exists('stringToArrayArgs')) {
if (! function_exists('str_args_to_array')) {
/**
* Converti une chaine d'arguments en tableau d'arguments en respectant le typage
*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment