在使用 imagecache_create_path & 之前使用 Drupal imagecache 生成图像获取图像大小

发布于 2024-09-03 15:54:23 字数 607 浏览 2 评论 0原文

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

先感谢您

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2024-09-10 15:54:23

检查 imagecache.module 中的 imagecache_build_derivative() 函数及其用法。对于您的情况,它应该大致如下工作:(

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

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文