Help on image rendering with PHP


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 :


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 :-).



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.