开发者

Generate image with Drupal imagecache before using imagecache_create_path & getimagesize

I'm using imagecache_create_path() and getimagesize() to get the path of a imagecache-generated image and its dimensions. However, if it's the first time we access the page that image doesn't exist yet and imagecache_create_path doesn't generate it either.

Here's 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 w开发者_如何学运维ords, can I generate the image (using a preset) from PHP without showing it in the browser?

Thank you in advance


Check the imagecache_build_derivative() function and its usage in imagecache.module. For your case, it should work roughly like so:

$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
}

(NOTE: untested code, beware of typos and other errors/oversights)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜