vendor\klio\klio-bundle\src\Files\Image.php line 19
<?php
namespace Klio\KlioBundle\Files;
class Image
{
public static function getImage(string $url)
{
$parts = explode('/', $url);
$folder = $parts[2];
$hash = $parts[3];
$mimeType = false;
$size = false;
// taille
if (@$parts[5]) {
$fileName = $parts[5];
$size = $parts[4];
}
//original
else {
$fileName = $parts[4];
}
if (strtolower(substr($fileName, -4)) == '.jpg') {
$fileExt = 'jpg';
$mimeType = 'image/jpeg';
} else if (strtolower(substr($fileName, -5)) == '.jpeg') {
$fileExt = 'jpeg';
$mimeType = 'image/jpeg';
} else if (strtolower(substr($fileName, -4)) == '.png') {
$fileExt = 'png';
$mimeType = 'image/png';
} else if (strtolower(substr($fileName, -4)) == '.gif') {
$fileExt = 'gif';
$mimeType = 'image/gif';
} else if (strtolower(substr($fileName, -5)) == '.giff') {
$fileExt = 'giff';
$mimeType = 'image/gif';
} else if (strtolower(substr($fileName, -4)) == '.svg') {
$fileExt = 'svg';
$mimeType = 'image/svg+xml';
}
// SVG
if ($fileExt == 'svg') {
$filePath = @$originalPath;
$fileExist = file_exists(@$originalPath);
}
// non SVG
else {
if (file_exists(__UPLOAD__ . '/' . $folder . '/' . substr($hash, 0, 3) . '/' . substr($hash, 3, 61) . '/0.' . $fileExt)) $originalPath = __UPLOAD__ . '/' . $folder . '/' . substr($hash, 0, 3) . '/' . substr($hash, 3, 61) . '/0.' . $fileExt;
else $originalPath = __UPLOAD__ . '/' . $folder . '/' . substr($hash, 0, 3) . '/' . substr($hash, 3, 61) . '/' . substr($hash, 64);
if (@$mimeType and @$size) {
$filePath = __UPLOAD__ . '/' . $folder . '/' . substr($hash, 0, 3) . '/' . substr($hash, 3, 61) . '/' . $size . '.' . $fileExt;
$fileExist = file_exists($filePath);
// si pas la taille mais l'image source, on va créer le fichier à la bonne taille
if (!$fileExist and file_exists(@$originalPath)) {
$convert['src'] = $originalPath;
$convert['dest'] = $filePath;
$convert['size'] = $size;
self::convert($convert);
$fileExist = file_exists($filePath);
}
} else if ($mimeType) {
$filePath = @$originalPath;
$fileExist = file_exists($filePath);
// si le fichier n'existe pas, on regarde si on a un pdf pour conversion avec imagick
if (!$fileExist and file_exists(__UPLOAD__ . '/' . $folder . '/' . substr($hash, 0, 3) . '/' . substr($hash, 3, 61) . '/0.pdf')) {
//$commande = __FW__.'/exe/imagemagick/convert.exe "'.__UPLOAD__.'/'.$folder.'/'.substr($hash,0,3).'/'.substr($hash,3,61).'/0.pdf[0]" "'.$filePath.'"';
//$fileExist = file_exists($filePath);
//exec($commande,$result);
}
}
}
if ($fileExist) {
date_default_timezone_set('UTC');
$lastModified = filemtime($filePath);
$eTag = md5(@$hash . @$size);
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= strtotime($lastModified)) {
header('HTTP/1.1 304 Not Modified');
exit();
}
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH'] == $eTag) {
header('HTTP/1.1 304 Not Modified');
exit();
}
$fsize = filesize($filePath);
header_remove('Server');
header_remove('X-Powered-By');
header_remove('Expires');
header('Cache-Control: public');
header('Pragma: public');
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($filePath));
header('Content-Type: ' . $mimeType);
header('ETag: "' . $eTag . '"');
header('Last-Modified: ' . date("D, d M Y H:i:s \G\M\T", $lastModified));
readfile($filePath);
exit();
}
}
private static function convert($params)
{
error_reporting(0);
$source = $params['src'];
$dest = $params['dest'];
if ($params['size']) {
$size = $params['size'];
if (substr($size, 0, 1) == "c") {
$params['crop'] = true;
$size = substr($size, 1);
}
$sizes = explode("x", $size);
$params['width'] = $sizes[0];
$params['height'] = $sizes[1];
}
// size
$dest_width = 9999;
$dest_height = 9999;
if ($params['width'] > 0) {
$dest_width = intval($params['width']);
}
if ($params['height'] > 0) {
$dest_height = intval($params['height']);
}
// crop
$crop = false;
if ($params['crop'] and $params['width'] and $params['height']) $crop = true;
//pages (pour les pdf)
$page = '';
// colospace
$colorspace = 'sRGB';
// pdf
if (strtolower(substr($params['src'], -4)) == ".pdf") {
$page = '[0]';
$colorspace = 'CMYK';
}
/* === conversion de l'image === */
$imagick = new \Imagick($source);
// si on change la taille
if ($dest_width > 0 and $dest_height > 0) {
if ($crop) {
$imagick->cropThumbnailImage($dest_width, $dest_height, true);
$imagick->setImagePage(0, 0, 0, 0);
}
// wudth, heigth, filter, blur, bestfit
else $imagick->resizeImage($dest_width, $dest_height, \imagick::FILTER_LANCZOS, 1, true);
}
$imagick->writeImages($dest, false);
/* === destination : screen === */
if ($params['dest'] == 'jpg') {
header('Content-Type: image/jpeg');
flush();
ob_end_clean();
readfile($img_dest);
unlink($img_dest);
exit();
} elseif ($params['dest'] == 'png') {
header('Content-Type: image/png');
flush();
ob_end_clean();
readfile($img_dest);
unlink($img_dest);
exit();
} elseif ($params['dest'] == 'gif') {
header('Content-Type: image/gif');
flush();
ob_end_clean();
readfile($img_dest);
unlink($img_dest);
exit();
}
return $result;
}
}