Generate image with Drupal imagecache before using imagecache_create_path & getimagesize

让人想犯罪 __ 提交于 2019-12-23 18:59:09

问题


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 words, can I generate the image (using a preset) from PHP without showing it in the browser?

Thank you in advance


回答1:


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)



来源:https://stackoverflow.com/questions/2965490/generate-image-with-drupal-imagecache-before-using-imagecache-create-path-geti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!