vendor\klio\klio-bundle\src\Files\Image.php line 8

  1. <?php
  2. namespace Klio\KlioBundle\Files;
  3. class Image
  4. {
  5.     public static function getImage(string $url)
  6.     {
  7.         $parts explode('/'$url);
  8.         $folder $parts[2];
  9.         $hash $parts[3];
  10.         $mimeType false;
  11.         $size  false;
  12.         // taille
  13.         if (@$parts[5]) {
  14.             $fileName $parts[5];
  15.             $size $parts[4];
  16.         }
  17.         //original
  18.         else {
  19.             $fileName $parts[4];
  20.         }
  21.         if (strtolower(substr($fileName, -4)) == '.jpg') {
  22.             $fileExt 'jpg';
  23.             $mimeType 'image/jpeg';
  24.         } else if (strtolower(substr($fileName, -5)) == '.jpeg') {
  25.             $fileExt 'jpeg';
  26.             $mimeType 'image/jpeg';
  27.         } else if (strtolower(substr($fileName, -4)) == '.png') {
  28.             $fileExt 'png';
  29.             $mimeType 'image/png';
  30.         } else if (strtolower(substr($fileName, -4)) == '.gif') {
  31.             $fileExt 'gif';
  32.             $mimeType 'image/gif';
  33.         } else if (strtolower(substr($fileName, -5)) == '.giff') {
  34.             $fileExt 'giff';
  35.             $mimeType 'image/gif';
  36.         } else if (strtolower(substr($fileName, -4)) == '.svg') {
  37.             $fileExt 'svg';
  38.             $mimeType 'image/svg+xml';
  39.         }
  40.         // SVG
  41.         if ($fileExt == 'svg') {
  42.             $filePath = @$originalPath;
  43.             $fileExist file_exists(@$originalPath);
  44.         }
  45.         // non SVG
  46.         else {
  47.             if (file_exists(__UPLOAD__ '/' $folder '/' substr($hash03) . '/' substr($hash361) . '/0.' $fileExt)) $originalPath __UPLOAD__ '/' $folder '/' substr($hash03) . '/' substr($hash361) . '/0.' $fileExt;
  48.             else $originalPath __UPLOAD__ '/' $folder '/' substr($hash03) . '/' substr($hash361) . '/' .  substr($hash64);
  49.             if (@$mimeType and @$size) {
  50.                 $filePath __UPLOAD__ '/' $folder '/' substr($hash03) . '/' substr($hash361) . '/' $size '.' $fileExt;
  51.                 $fileExist file_exists($filePath);
  52.                 // si pas la taille mais l'image source, on va créer le fichier à la bonne taille
  53.                 if (!$fileExist and file_exists(@$originalPath)) {
  54.                     $convert['src'] = $originalPath;
  55.                     $convert['dest'] = $filePath;
  56.                     $convert['size'] = $size;
  57.                     self::convert($convert);
  58.                     $fileExist file_exists($filePath);
  59.                 }
  60.             } else if ($mimeType) {
  61.                 $filePath = @$originalPath;
  62.                 $fileExist file_exists($filePath);
  63.                 // si le fichier n'existe pas, on regarde si on a un pdf pour conversion avec imagick
  64.                 if (!$fileExist and file_exists(__UPLOAD__ '/' $folder '/' substr($hash03) . '/' substr($hash361) . '/0.pdf')) {
  65.                     //$commande = __FW__.'/exe/imagemagick/convert.exe "'.__UPLOAD__.'/'.$folder.'/'.substr($hash,0,3).'/'.substr($hash,3,61).'/0.pdf[0]" "'.$filePath.'"';
  66.                     //$fileExist = file_exists($filePath);
  67.                     //exec($commande,$result);
  68.                 }
  69.             }
  70.         }
  71.         if ($fileExist) {
  72.             date_default_timezone_set('UTC');
  73.             $lastModified filemtime($filePath);
  74.             $eTag md5(@$hash . @$size);
  75.             if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= strtotime($lastModified)) {
  76.                 header('HTTP/1.1 304 Not Modified');
  77.                 exit();
  78.             }
  79.             if (isset($_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH'] == $eTag) {
  80.                 header('HTTP/1.1 304 Not Modified');
  81.                 exit();
  82.             }
  83.             $fsize filesize($filePath);
  84.             header_remove('Server');
  85.             header_remove('X-Powered-By');
  86.             header_remove('Expires');
  87.             header('Cache-Control: public');
  88.             header('Pragma: public');
  89.             header('Accept-Ranges: bytes');
  90.             header('Content-Length: ' filesize($filePath));
  91.             header('Content-Type: ' $mimeType);
  92.             header('ETag: "' $eTag '"');
  93.             header('Last-Modified: ' date("D, d M Y H:i:s \G\M\T"$lastModified));
  94.             readfile($filePath);
  95.             exit();
  96.         }
  97.     }
  98.     private static function convert($params)
  99.     {
  100.         error_reporting(0);
  101.         $source $params['src'];
  102.         $dest $params['dest'];
  103.         if ($params['size']) {
  104.             $size $params['size'];
  105.             if (substr($size01) == "c") {
  106.                 $params['crop'] = true;
  107.                 $size substr($size1);
  108.             }
  109.             $sizes explode("x"$size);
  110.             $params['width'] = $sizes[0];
  111.             $params['height'] = $sizes[1];
  112.         }
  113.         // size
  114.         $dest_width 9999;
  115.         $dest_height 9999;
  116.         if ($params['width'] > 0) {
  117.             $dest_width intval($params['width']);
  118.         }
  119.         if ($params['height'] > 0) {
  120.             $dest_height intval($params['height']);
  121.         }
  122.         // crop
  123.         $crop false;
  124.         if ($params['crop'] and $params['width'] and $params['height']) $crop true;
  125.         //pages (pour les pdf)
  126.         $page '';
  127.         // colospace
  128.         $colorspace 'sRGB';
  129.         // pdf
  130.         if (strtolower(substr($params['src'], -4)) == ".pdf") {
  131.             $page '[0]';
  132.             $colorspace 'CMYK';
  133.         }
  134.         /* === conversion de l'image === */
  135.         $imagick = new \Imagick($source);
  136.         // si on change la taille
  137.         if ($dest_width and $dest_height 0) {
  138.             if ($crop) {
  139.                 $imagick->cropThumbnailImage($dest_width$dest_heighttrue);
  140.                 $imagick->setImagePage(0000);
  141.             }
  142.             // wudth, heigth, filter, blur, bestfit
  143.             else $imagick->resizeImage($dest_width$dest_height\imagick::FILTER_LANCZOS1true);
  144.         }
  145.         $imagick->writeImages($destfalse);
  146.         /* ===  destination : screen === */
  147.         if ($params['dest'] == 'jpg') {
  148.             header('Content-Type: image/jpeg');
  149.             flush();
  150.             ob_end_clean();
  151.             readfile($img_dest);
  152.             unlink($img_dest);
  153.             exit();
  154.         } elseif ($params['dest'] == 'png') {
  155.             header('Content-Type: image/png');
  156.             flush();
  157.             ob_end_clean();
  158.             readfile($img_dest);
  159.             unlink($img_dest);
  160.             exit();
  161.         } elseif ($params['dest'] == 'gif') {
  162.             header('Content-Type: image/gif');
  163.             flush();
  164.             ob_end_clean();
  165.             readfile($img_dest);
  166.             unlink($img_dest);
  167.             exit();
  168.         }
  169.         return $result;
  170.     }
  171. }