我已经设法让标准Intervention Image工作了,但我永远也不能让它与缓存系统一起工作。我在没有框架的标准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所指出的在documentation中定义的第二个和第三个参数进行设置。无论我将第三个参数设置为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()方法的末尾。谢谢你,Daniel Protopopov!
发布于 2020-04-04 13:17:08
您需要添加缓存生存期和true作为第三个参数,以便它返回一个对象,而不是按照documentation返回一个字符串
https://stackoverflow.com/questions/61022598
复制相似问题