|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2018. MckenzieArts |
| 4 | + * @author Mckenziearts <[email protected]> |
| 5 | + * @link https://www.camer-freelancer.com |
| 6 | + * @since 7.1 |
| 7 | + * @version 1.0 |
| 8 | + * @package CF/Helpers |
| 9 | + */ |
| 10 | + |
| 11 | +namespace App\Helpers; |
| 12 | + |
| 13 | +use Intervention\Image\Facades\Image; |
| 14 | + |
| 15 | +class MediaHelper |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @protected |
| 19 | + * |
| 20 | + * @var string $dir, the file uploaded path |
| 21 | + */ |
| 22 | + protected static $dir = 'app/public'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @return string |
| 26 | + */ |
| 27 | + public static function getUploadsFolder() |
| 28 | + { |
| 29 | + return self::$dir; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + public static function getStoragePath() |
| 36 | + { |
| 37 | + return storage_path(self::$dir); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Return the size of an image |
| 42 | + * |
| 43 | + * @param string $file |
| 44 | + * @param string $folder |
| 45 | + * @return array $width and $height of the file give in parameter |
| 46 | + */ |
| 47 | + public static function getFileSizes(string $file, string $folder = null) |
| 48 | + { |
| 49 | + if ($folder) { |
| 50 | + list($width, $height, $type, $attr) = getimagesize(storage_path(self::$dir.'/'. $folder .'/'. date('FY') .'/'.$file)); |
| 51 | + } |
| 52 | + list($width, $height, $type, $attr) = getimagesize(storage_path(self::$dir.'/'. date('FY') .'/'.$file)); |
| 53 | + |
| 54 | + return [ |
| 55 | + 'width' => $width, |
| 56 | + 'height' => $height |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * resize, To rezise and image |
| 62 | + * |
| 63 | + * @param string $file, l'image à redimmensionner |
| 64 | + * @param int $width, la largeur à laquelle on doit redimensionner l'image |
| 65 | + * @param int $height, la hateur à laquelle on doit redimensionner l'image |
| 66 | + * @param string $filepath, le chemin ou est garder le fichier redimensionner |
| 67 | + */ |
| 68 | + public static function resize($file, $width, $height, $filepath) |
| 69 | + { |
| 70 | + Image::make($file)->resize($width, $height)->save($filepath); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * getImageWeight, permet de retourner le poids d'une image |
| 75 | + * |
| 76 | + * @param $octets |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + public static function getImageWeight($octets) { |
| 80 | + $resultat = $octets; |
| 81 | + for ($i = 0; $i < 8 && $resultat >= 1024; $i++) { |
| 82 | + $resultat = $resultat / 1024; |
| 83 | + } |
| 84 | + if ($i > 0) { |
| 85 | + return preg_replace('/,00$/', '', number_format($resultat, 2, ',', '')) |
| 86 | + . ' ' . substr('KMGTPEZY', $i-1, 1) . 'o'; |
| 87 | + } else { |
| 88 | + return $resultat . ' o'; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments