0

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!

halfer
  • 19,824
  • 17
  • 99
  • 186
dpDesignz
  • 1,909
  • 10
  • 34
  • 70
  • You need to check what is in $img variable before making an invocation of response() at the end. From first glance your code is fine, though it’s best to debug it to see what’s happening. – Daniel Protopopov Apr 04 '20 at 05:03
  • @DanielProtopopov thanks for that. If I just echo out the `$img` variable it outputs code like `����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90`. I'm not sure how to debug this? – dpDesignz Apr 04 '20 at 05:11
  • @DanielProtopopov Thanks, yes I've tried that but it returns the exact same value no matter if I set it to `TRUE` or `FALSE` – dpDesignz Apr 04 '20 at 05:17

1 Answers1

1

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

Daniel Protopopov
  • 6,778
  • 3
  • 23
  • 39
  • Thanks but I've already tried setting the second and third parameters with no luck. No matter what options I load in there they return the exact same value.' – dpDesignz Apr 04 '20 at 05:21
  • 1
    Thanks, I tried that but I get the error `Class 'Image' not found`. I just discovered that I was adding the parameter options in the wrong place! This now works. Thanks! :D – dpDesignz Apr 04 '20 at 05:38