phpinterventionimage-caching

How to output the cached image with Intervention Image Cache?


I've managed to get the standard Intervention Image to work, but I can't for the life of me get it to work with the cache system. I'm using this on a standard PHP setup with no framework.

This is my code

// 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;

But I'm getting the error Call to a member function response() on string.

Again, I'm not using Laravel or any other package, this is just a plain PHP script.

I have tried setting the second and third parameters as defined in the documentation (as pointed out by Daniel Protopopov). No matter if I set the third parameter to TRUE or FALSE it still just return a string like ����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90 if I echo out $img

If I run the following code using just the core Intervention Image package it outputs perfectly fine, but I can't seem to get the caching option to work and all the examples in the GitHub issue tracker seem to be for v1/pre 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;

How can I get this to work?

Update

Turns out I was putting the TRUE parameter in the $image->resize() function instead of on the end of the $this->manager->cache() method. Thanks Daniel Protopopov!


Solution

  • You need to add caching lifetime and true as third parameter so that it returns you an object, and not a string as per documentation