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

dpDesignz

我已经设法让标准的干预图像工作,但我一生都无法让它与缓存系统一起工作。我在没有框架的标准 PHP 设置中使用它。

这是我的代码

// 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所指出的)。无论我是将第三个参数设置为TRUE还是FALSE它仍然只返回一个字符串,就像����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90我回显一样$img

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

// 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()方法的末尾感谢丹尼尔·普罗托波波夫

丹尼尔·普罗托波波夫

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Spring Cache中使用多个缓存实现

Laravel / Intervention Image Class-找不到类

使用Laravel Intervention库无法缓存动态图像

找不到Laravel 5类'Intervention \ Image \ ImageServiceProvider'

Laravel Intervention Image GD库扩展

调用未定义的方法Intervention \ Image \ Facades \ Image :: make()

使用Laravel中的Intervention Image调整最大宽度/高度,同时保持原始比例

Laravel + Image Intervention:强制下载未保存的文件

必须安装/启用PHP Fileinfo扩展名才能使用Intervention Image

Laravel-将干预图像模型(\ Intervention \ Image \ Image)转换为UploadedFile模型(\ Illuminate \ Http \ UploadedFile)

Laravel 5.4 Image Intervention在S3上以0字节上传图像

PHP Intervention-image,避免出现任何图像和崩溃崩溃异常

如何使用Laravel 5.5和Intervention Image从Amazon S3将图像文件上传到Stripe

Intervention \ Image \ Exception \ NotSupportedException不支持编码格式(tmp)

Laravel Image Intervention避免旋转

使用Laravel Intervention图像上传到S3返回布尔值而不是路径

如何存根节点缓存的cache.get()?

如何在flutter中将缓存的图像添加到应用栏(使用cached_image_network)

Intervention \ Image \ Exception \ NotWritableException无法将图像数据写入路径

ErrorException:imagecreatetruecolor():Intervention \ Image \ Image :: resize()时文件中的图像尺寸无效

如何使用Laravel中的Intervention图像处理库以自定义比例调整图像大小

如何使用flask-cache创建的缓存值

如何使用Laravel 5的Intervention.io库删除图像

如何通过Universal Image Loader使用已从缓存中下载的图像

gulp-cache:如何使用文件缓存进行LESS构建

无法在Laravel中使用Intervention上传大图像

“不能使用 Intervention\Image\Facades\Image 作为图像,因为名称已被使用”

我想使用 Intervention/image 调整照片大小

imagettfbbox():使用 Laravel Intervention/Image 进行文本布局时出现问题