2

我正在使用 imagecache_create_path() 和 getimagesize() 来获取 imagecache 生成的图像的路径及其尺寸。但是,如果这是我们第一次访问该图像尚不存在的页面,并且 imagecache_create_path 也不会生成它。

这是代码:

// 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 生成图像(使用预设)而不在浏览器中显示它吗?

先感谢您

4

1 回答 1

7

检查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
}

(注意:未经测试的代码,请注意拼写错误和其他错误/疏忽)

于 2010-06-03T12:13:54.203 回答