Create an image using Drupal imagecache before using imagecache_create_path and getimagesize

I use imagecache_create_path () and getimagesize () to get the path to the imagecache image and its size. However, if this is the first time we access the page, the image does not exist yet, and imagecache_create_path does not generate it either.

Here is the code:

// we get the image path from a preset (always return the path even if the file doesn't exist)
$small_image_path = imagecache_create_path('gallery_image_small', $image["filepath"]);
// I get the image dimensions (only if the file exists already)
$data_small = list($width, $height, $type, $image_attributes) = @getimagesize($small_image_path);

Is there any API method to get the path AND generate the file? In other words, can I generate an image (using a preset) from PHP without showing it in the browser?

Thank you in advance

+3
source share
1 answer

imagecache_build_derivative() imagecache.module. :

$presetname = 'gallery_image_small';
$preset = imagecache_preset_by_name($presetname);
$src = $image["filepath"];
$dst = imagecache_create_path($presetname, $src);
// Ensure existing derivative or try to create it on the fly
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
  // Do what you need to do with the image
}

(: , /)

+8

Source: https://habr.com/ru/post/1748334/


All Articles