Muut
May 6, 2017, 4:55pm
1
Hello,
Could someone help me on this little problem with image please ?
I get an image somewhere and i’d like to generate a cache version and put the cache url as src of img tag.
I use this code to generate this cached image :
use Gregwar\Image\Image;
Image::open($path_image)->cropResize(400, 400)->jpeg()
I got this path :
cache/images/c/e/0/d/2/ce0d2d3b97cfd70542dff5f03de15fb55ecb3d02.jpg
This is not routable as other images. (cached into images/…)
I tried all methods to force cache into regular cache directory (setActualCacheDir, setCacheDir). Without success.
Could someone give any tips please ?
Thanks in advance :-).
Thierry
Muut
May 6, 2017, 10:33pm
2
Hello,
After digging arround, i found a way to do it:
//on class initialization
$locator = Grav::instance()['locator'];
$this->cacheDir = $locator->findResource('cache://images', true) ?: $locator->findResource('cache://images', true, true);
private function get_cache_image($image){
$image = Image::open($image) // open the file
->cropResize(400, 400) // resize
->setCacheDir($this->cacheDir) // define cachedir
->setActualCacheDir($this->cacheDir) // cachedir
->cacheFile(); //save file in cache directory and return path
$remove = getcwd();
$cache_image = str_replace($remove,'',$image);
return $cache_image;
}
cacheFile() retrieve the full path(e.g /home…). A dirty way to clear this with str_replace.
It will be really usefull if someone get a better way to do.
Thanks for all.
Thierry