在使用imagecache_create_path和amp之前,使用Drupal imagecache生成图像和getimagesize [英] Generate image with Drupal imagecache before using imagecache_create_path & getimagesize
问题描述
我正在使用imagecache_create_path()和getimagesize()来获取imagecache生成的图像及其维度的路径。但是,如果这是我们第一次访问该图像不存在的页面,并且imagecache_create_path也不会生成它。
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.
以下是代码:
// 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);
有没有API方法来获取路径并生成文件?换句话说,我可以从PHP中生成图像(使用预设),而不在浏览器中显示?
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
推荐答案
查看imagecache.module中的 imagecache_build_derivative()
函数及其用法。对于你的情况,它应该大致如下:
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)
这篇关于在使用imagecache_create_path和amp之前,使用Drupal imagecache生成图像和getimagesize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!