How to output the cached image?
dpDesignz opened this issue · comments
Ok 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.
If I run the following code it outputs perfectly fine, but I want to enable caching
// 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;
Is anyone able to point me in the right direction to enable this to work please?
Turns out I was putting the TRUE
parameter in the $image->resize()
function instead of on the end of the $this->manager->cache()
method. All sorted now.
My final code
// Create the image
$cached_image = $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();
}
}, 10, TRUE);
} else {
// Return the image
return $image;
}
},
10,
TRUE
);
// Output the image
echo $cached_image->response();
exit;