2010-06-03 30 views
3

我使用imagecache_create_path()和getimagesize()來獲取imagecache生成的圖像及其尺寸的路徑。但是,如果這是我們第一次訪問該圖像不存在的頁面,並且imagecache_create_path也不會生成該圖像。在使用imagecache_create_path&getimagesize之前使用Drupal imagecache生成圖像

下面的代碼:

// 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中生成圖像(使用預設),而無需在瀏覽器中顯示它嗎?

預先感謝您

回答

8

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

(注:未測試的代碼,拼寫錯誤和其他錯誤/疏漏提防)

+0

的偉大工程。謝謝:) – ozke 2010-06-03 12:48:48

+0

在Drupal 6.25上,如果你在頁面加載時'打印'imagecache'',或者在加載的響應內容中已經有imagecache'',則圖像將由系統。 – atwixtor 2012-05-04 18:37:52