ldc-dam/app/Http/Traits/MediaTools.php
2019-06-05 09:10:06 +02:00

222 lines
5.7 KiB
PHP

<?php
namespace App\Http\Traits;
use Illuminate\Support\Facades\Storage;
trait MediaTools {
public function get_file_size($file_path) {
return $this->fix_integer_overflow(filesize($file_path));
}
public function get_mediaFolder() {
$mediaPath = "medias/";
$directories = Storage::allDirectories($mediaPath );
$selectedFolder = "";
// on obtient le dernier dossier créé
foreach ($directories as $key => $foldername) {
if($selectedFolder < $foldername){
$selectedFolder = $foldername;
}
}
// si on n'a pas de dossier, on le créé
if($selectedFolder == ""){
$selectedFolder = $mediaPath.uniqid();
Storage::makeDirectory($selectedFolder);
}else{
// on compte le nombre de fichier
$files = Storage::allFiles($selectedFolder);
// si on en a plus de 1000, on créé un nouveau dossier
if(count($files)> 1000){
$selectedFolder = $mediaPath.uniqid();
Storage::makeDirectory($selectedFolder);
}
}
return $selectedFolder;
}
public function fix_integer_overflow($size) {
if ($size < 0) {
$size += 2.0 * (PHP_INT_MAX + 1);
}
return $size;
}
public function formatBytes($octet, $precision = 2) {
// Array contenant les differents unités
$unite = array('octet','Ko','Mo','Go');
if ($octet < 1000) // octet
{
return $octet.$unite[0];
}
else
{
if ($octet < 1000000) // ko
{
$ko = round($octet/1024,2);
return $ko.$unite[1];
}
else // Mo ou Go
{
if ($octet < 1000000000) // Mo
{
$mo = round($octet/(1024*1024),2);
return $mo.$unite[2];
}
else // Go
{
$go = round($octet/(1024*1024*1024),2);
return $go.$unite[3];
}
}
}
}
public function get_dpi($filename){
// open the file and read first 20 bytes.
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
// get the value of byte 14th up to 18th
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,4,4);
//return array(hexdec($x),hexdec($y));
return hexdec($x);
}
/**
* @param $src - a valid file location
* @param $dest - a valid file target
* @param $targetWidth - desired output width
* @param $targetHeight - desired output height or null
*/
function createThumbnail($src, $dest, $targetWidth, $targetHeight = null) {
// Link image type to correct image loader and saver
// - makes it easier to add additional types later on
// - makes the function easier to read
$IMAGETYPE_JPEG = 2;
$IMAGETYPE_PNG = 3;
$IMAGETYPE_GIF = 1;
$IMAGE_HANDLERS = [
2 => [
'load' => 'imagecreatefromjpeg',
'save' => 'imagejpeg',
'quality' => 100
],
3 => [
'load' => 'imagecreatefrompng',
'save' => 'imagepng',
'quality' => 0
],
1 => [
'load' => 'imagecreatefromgif',
'save' => 'imagegif',
'quality' => 0
]
];
// 1. Load the image from the given $src
// - see if the file actually exists
// - check if it's of a valid image type
// - load the image resource
// get the type of the image
// we need the type to determine the correct loader
$type = exif_imagetype($src);
// if no valid type or no handler found -> exit
if (!$type || !$IMAGE_HANDLERS[$type]) {
return null;
}
// load the image with the correct loader
$image = call_user_func($IMAGE_HANDLERS[$type]['load'], $src);
// no image found at supplied location -> exit
if (!$image) {
return null;
}
// 2. Create a thumbnail and resize the loaded $image
// - get the image dimensions
// - define the output size appropriately
// - create a thumbnail based on that size
// - set alpha transparency for GIFs and PNGs
// - draw the final thumbnail
// get original image width and height
$width = imagesx($image);
$height = imagesy($image);
// maintain aspect ratio when no height set
if ($targetHeight == null) {
// get width to height ratio
$ratio = $width / $height;
// if is portrait
// use ratio to scale height to fit in square
if ($width > $height) {
$targetHeight = floor($targetWidth / $ratio);
}
// if is landscape
// use ratio to scale width to fit in square
else {
$targetHeight = $targetWidth;
$targetWidth = floor($targetWidth * $ratio);
}
}
// create duplicate image based on calculated target size
$thumbnail = imagecreatetruecolor($targetWidth, $targetHeight);
// set transparency options for GIFs and PNGs
if ($type == $IMAGETYPE_GIF || $type == $IMAGETYPE_PNG) {
// make image transparent
imagecolortransparent(
$thumbnail,
imagecolorallocate($thumbnail, 0, 0, 0)
);
// additional settings for PNGs
if ($type == $IMAGETYPE_PNG) {
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);
}
}
// copy entire source image to duplicate image and resize
imagecopyresampled(
$thumbnail,
$image,
0, 0, 0, 0,
$targetWidth, $targetHeight,
$width, $height
);
// 3. Save the $thumbnail to disk
// - call the correct save method
// - set the correct quality level
// save the duplicate version of the image to disk
return call_user_func(
$IMAGE_HANDLERS[$type]['save'],
$thumbnail,
$dest,
$IMAGE_HANDLERS[$type]['quality']
);
}
}