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 に答える