在使用 imagecache_create_path & 之前使用 Drupal imagecache 生成图像获取图像大小 [英] Generate image with Drupal imagecache before using imagecache_create_path & getimagesize

查看:29
本文介绍了在使用 imagecache_create_path & 之前使用 Drupal imagecache 生成图像获取图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 imagecache_create_path() 和 getimagesize() 来获取图像缓存生成的图像的路径及其尺寸.但是,如果这是我们第一次访问该图像尚不存在的页面并且 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?

提前致谢

推荐答案

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

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 & 之前使用 Drupal imagecache 生成图像获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆