Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Help on image rendering with PHP

Started by Muut Archive 9 years ago · 1 replies · 539 views
9 years ago

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 :

PHP
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

9 years ago

Hello,

After digging arround, i found a way to do it:

PHP
//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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1322 9 years ago
Archive · by Muut Archive, 9 years ago
2 917 9 years ago
Archive · by Muut Archive, 9 years ago
2 4048 9 years ago
Archive · by Muut Archive, 9 years ago
1 2922 9 years ago
Archive · by Muut Archive, 9 years ago
3 1106 9 years ago