161 lines
4.1 KiB
PHP
161 lines
4.1 KiB
PHP
<?php
|
|
/*
|
|
* ImageCacheManager.php
|
|
* Author: Riccardo Di Dato
|
|
* Creation Date: 24/giu/2016
|
|
*/
|
|
|
|
class ImageCacheManager {
|
|
|
|
public static function cacheImage(MediaModel $media, $size){
|
|
self::checkValidModel($media);
|
|
$cachedPath = self::getImageCachePath($media->id, $size);
|
|
$mimePath = self::getImageMimePath($media->id, $size);
|
|
$originalFilePath = self::getOriginalPath($media);
|
|
|
|
if ( SFSManager::fileExists($cachedPath) ){
|
|
SFSManager::deleteFile($cachedPath);
|
|
SFSManager::deleteFile($mimePath);
|
|
}
|
|
if (!is_dir(pathinfo($cachedPath,PATHINFO_DIRNAME))){
|
|
SFSManager::createDirectory(pathinfo($cachedPath,PATHINFO_DIRNAME));
|
|
}
|
|
SFSManager::writeFile(mime_content_type($originalFilePath), $mimePath);
|
|
SFSManager::writeFile(self::getImageContent($media, $size), $cachedPath);
|
|
}
|
|
|
|
/**
|
|
* Restituisce true se il media inserito è cacheato con la size selezionata
|
|
* @param string $id
|
|
* @param int $size
|
|
*/
|
|
public static function isCached($id, $size){
|
|
$cachedPath = self::getImageCachePath($id, $size);
|
|
return SFSManager::fileExists($cachedPath);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Restituisce il mimetype di un file cacheato
|
|
* @param string $id
|
|
* @param int $size
|
|
* @return string
|
|
*/
|
|
public static function getMimeType($id, $size){
|
|
$mimePath = self::getImageMimePath($id, $size);
|
|
return file_get_contents($mimePath);
|
|
}
|
|
|
|
/**
|
|
* Restituisce il mimetype di un file cacheato
|
|
* @param string $id
|
|
* @param int $size
|
|
* @return string
|
|
*/
|
|
public static function getContent($id, $size){
|
|
$cachedPath = self::getImageCachePath($id, $size);
|
|
return file_get_contents($cachedPath);
|
|
}
|
|
|
|
public static function printCachedMedia($id, $size){
|
|
$cachedPath = self::getImageCachePath($id, $size);
|
|
header('Content-type: '.self::getMimeType($id, $size));
|
|
return readfile($cachedPath);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
* PRIVATE STUFF
|
|
*/
|
|
/**
|
|
* Controlla che il modello sia un media di tipo immagine, altrimenti lancia eccezione
|
|
* @param MediaModel $media
|
|
* @throws CoreException
|
|
*/
|
|
private static function checkValidModel(MediaModel $media){
|
|
if (! $media->getRenderer() instanceof MediaImage){
|
|
throw new CoreException("ImageCacheManager can cache only MediaModel of type MediaImage (got '".get_class($media->getRenderer())."')");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Restituisce il path base dove si trova la cache delle immagini
|
|
* @return string
|
|
*/
|
|
private static function getOriginalPath(MediaModel $media){
|
|
$basepath = GlobalVariables::get("config")->paths->media;
|
|
return $basepath."/".$media->id.".".$media->extension;
|
|
}
|
|
|
|
/**
|
|
* Restituisce il path base dove si trova la cache delle immagini
|
|
* @return string
|
|
*/
|
|
private static function getCachePath(){
|
|
return GlobalVariables::get("config")->paths->imageCache;
|
|
}
|
|
|
|
/**
|
|
* Restituisce il path dell'immagine cacheata
|
|
* @param string $id
|
|
* @param int $size
|
|
* @return string
|
|
*/
|
|
private static function getImageCachePath($id, $size){
|
|
return self::getCachePath()."/".$size."/".$id;
|
|
}
|
|
|
|
/**
|
|
* Restituisce il path del file contenente il mimetype dell'immagine cacheata
|
|
* @param string $id
|
|
* @param int $size
|
|
* @return string
|
|
*/
|
|
private static function getImageMimePath($id, $size){
|
|
return self::getCachePath()."/".$size."/".$id.".mime";
|
|
}
|
|
|
|
|
|
private static function getImageContent(MediaModel $media, $size){
|
|
$originalFilePath = self::getOriginalPath($media);
|
|
|
|
$rval = new Imagick($originalFilePath);
|
|
|
|
$geom = $rval->getimagegeometry();
|
|
|
|
if($media->extension != "gif"){
|
|
if ($size!=MediaImage::SIZE_ORIGINAL){
|
|
if ($geom["width"]>=$geom["height"]){
|
|
$rval->scaleimage(MediaImage::$SIZES[$size]["width"], 0,false);
|
|
}
|
|
else {
|
|
$rval->scaleimage(0, MediaImage::$SIZES[$size]["height"],false);
|
|
}
|
|
}
|
|
|
|
return strval($rval);
|
|
}
|
|
else{
|
|
$rval = $rval->coalesceimages();
|
|
if ($size!=MediaImage::SIZE_ORIGINAL){
|
|
foreach ($rval as $frame){
|
|
if ($geom["width"]>=$geom["height"]){
|
|
$rval->scaleimage(MediaImage::$SIZES[$size]["width"], 0,false);
|
|
}
|
|
else {
|
|
$rval->scaleimage(0, MediaImage::$SIZES[$size]["height"],false);
|
|
}
|
|
}
|
|
}
|
|
$rval = $rval->deconstructimages();
|
|
|
|
return strval($rval->getimagesblob());
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|