首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Intervention Cache输出缓存的图像?

如何使用Intervention Cache输出缓存的图像?
EN

Stack Overflow用户
提问于 2020-04-04 09:09:39
回答 1查看 184关注 0票数 0

我已经设法让标准Intervention Image工作了,但我永远也不能让它与缓存系统一起工作。我在没有框架的标准PHP设置上使用它。

这是我的代码

代码语言:javascript
复制
// import the Intervention Image Manager Class ~ http://image.intervention.io/
use Intervention\Image\ImageManager;

// create an image manager instance with favored driver
if (!extension_loaded('imagick')) {
  $this->manager = new ImageManager(array('driver' => 'GD'));
} else {
  $this->manager = new ImageManager(array('driver' => 'imagick'));
}

$img = $this->manager->cache(
  function ($image) use ($imagePath) {

    $image = $image->make($imagePath);

    // Check for dimensions
    if (
      (!empty($_GET['w']) && is_numeric($_GET['w'])) || (!empty($_GET['h']) && is_numeric($_GET['h']))
    ) {
      // Dimensions set

      // Set default options
      $width = (!empty($_GET['w'])) ? (int) trim($_GET['w']) : null;
      $height = (!empty($_GET['h'])) ? (int) trim($_GET['h']) : null;

      // Resize and return the image
      return $image->resize($width, $height, function ($constraint) {
        $constraint->aspectRatio();
        // prevent possible upsizing
        if (empty($_GET['e']) || trim($_GET['e']) !== 'y') {
          $constraint->upsize();
        }
      });
    } else {
      // Return the image
      return $image;
    }
  }
);

// Output the image
echo $img->response();
exit;

但是我得到了错误Call to a member function response() on string

再次声明,我没有使用Laravel或其他任何包,这只是一个普通的PHP脚本。

我已经尝试按照Daniel Protopopov所指出的在documentation中定义的第二个和第三个参数进行设置。无论我将第三个参数设置为TRUE还是FALSE,如果我回显����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90,它仍然只返回一个类似$img的字符串

如果我只使用核心干预图像包运行以下代码,它会输出得非常好,但我似乎无法让缓存选项工作,并且GitHub问题跟踪器中的所有示例似乎都适用于v1/ 2017之前的版本。

代码语言:javascript
复制
// import the Intervention Image Manager Class ~ http://image.intervention.io/
use Intervention\Image\ImageManager;

// create an image manager instance with favored driver
if (!extension_loaded('imagick')) {
  $this->manager = new ImageManager(array('driver' => 'GD'));
} else {
  $this->manager = new ImageManager(array('driver' => 'imagick'));
}

// Create the image
$img = $this->manager->make($imagePath);

// Check for dimensions
if (
  (!empty($_GET['w']) && is_numeric($_GET['w'])) || (!empty($_GET['h']) && is_numeric($_GET['h']))
) {
  // Dimensions set

  // Set default options
  $width = (!empty($_GET['w'])) ? (int) trim($_GET['w']) : null;
  $height = (!empty($_GET['h'])) ? (int) trim($_GET['h']) : null;

  // Resize and return the image
  $img = $img->resize($width, $height, function ($constraint) {
    $constraint->aspectRatio();
    // prevent possible upsizing
    if (empty($_GET['e']) || trim($_GET['e']) !== 'y') {
      $constraint->upsize();
    }
  });
}

// Output the image
echo $img->response();
exit;

我怎么才能让它工作呢?

更新

原来我是把TRUE参数放在$image->resize()函数中,而不是放在$this->manager->cache()方法的末尾。谢谢你,Daniel Protopopov

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-04 13:17:08

您需要添加缓存生存期和true作为第三个参数,以便它返回一个对象,而不是按照documentation返回一个字符串

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61022598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档