我已经设法让标准的干预图像工作,但我一生都无法让它与缓存系统一起工作。我在没有框架的标准 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] 删除。
我来说两句