代码之家  ›  专栏  ›  技术社区  ›  ozke

在使用imagecache\u create\u path和getimagesize之前,使用Drupal imagecache生成图像

  •  3
  • ozke  · 技术社区  · 16 年前

    我使用imagecache\u create\u path()和getimagesize()获取imagecache生成的图像的路径及其维度。但是,如果是第一次访问该页,那么图像还不存在,imagecache\u create\u 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生成图像(使用预设)而不在浏览器中显示它吗?

    先谢谢你

    1 回复  |  直到 16 年前
        1
  •  7
  •   Henrik Opel    16 年前

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

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

    推荐文章