199 lines
5.5 KiB
PHP
199 lines
5.5 KiB
PHP
<?php
|
|
/*
|
|
* ImageCacheManager.php
|
|
* Author: Riccardo Di Dato
|
|
* Creation Date: 24/giu/2016
|
|
*/
|
|
|
|
class ImageCacheManager {
|
|
private static $memcached = null;
|
|
|
|
const EXPIRATION_SECONDS = 2592000; // 30 * 24 * 60 * 60 -> 30 gg
|
|
|
|
private static function getMemcached() {
|
|
if (is_null(self::$memcached)) {
|
|
self::$memcached = new Memcached();
|
|
self::$memcached->addServer('memcached', 11211);
|
|
}
|
|
return self::$memcached;
|
|
}
|
|
|
|
private static function getImageKey(MediaModel $media, $size) {
|
|
return self::getImageKeyById($media->id, $size);
|
|
}
|
|
private static function getImageKeyById(string $id, $size) {
|
|
return $id."-".$size."-image";
|
|
}
|
|
|
|
private static function getMimeKey(MediaModel $media, $size) {
|
|
return self::getMimeKeyById($media->id, $size);
|
|
}
|
|
private static function getMimeKeyById(string $id, $size) {
|
|
return $id."-".$size."-mime";
|
|
}
|
|
|
|
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);
|
|
self::getMemcached()->set(
|
|
self::getMimeKey($media, $size),
|
|
mime_content_type($originalFilePath),
|
|
self::EXPIRATION_SECONDS
|
|
);
|
|
self::getMemcached()->set(
|
|
self::getImageKey($media, $size),
|
|
self::getImageContent($media, $size),
|
|
self::EXPIRATION_SECONDS
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* Restituisce true se il media inserito è cacheato con la size selezionata
|
|
* @param string $id
|
|
* @param int $size
|
|
*/
|
|
public static function isCached($id, $size){
|
|
return self::getMemcached()->get(self::getImageKeyById($id, $size)) ? true : false;
|
|
// $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){
|
|
return self::getMemcached()->get(self::getMimeKeyById($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){
|
|
return self::getMemcached()->get(self::getImageKeyById($id, $size));
|
|
// $cachedPath = self::getImageCachePath($id, $size);
|
|
// return file_get_contents($cachedPath);
|
|
}
|
|
|
|
public static function printCachedMedia($id, $size) {
|
|
header('Content-type: '.self::getMimeType($id, $size));
|
|
echo self::getContent($id, $size);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
* 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());
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|