【发布时间】:2020-04-04 01:09:38
【问题描述】:
我已经设法让标准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 脚本。
我已尝试设置documentation 中定义的第二个和第三个参数(正如Daniel Protopopov 所指出的那样)。无论我将第三个参数设置为TRUE 还是FALSE,如果我回显$img,它仍然只返回一个类似����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90 的字符串
如果我只使用核心干预图像包运行以下代码,它的输出非常好,但我似乎无法让缓存选项正常工作,并且 GitHub 问题跟踪器中的所有示例似乎都适用于 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;
我怎样才能让它工作?
更新
原来我将TRUE 参数放在$image->resize() 函数中,而不是放在$this->manager->cache() 方法的末尾。谢谢Daniel Protopopov!
【问题讨论】:
-
在最后调用 response() 之前,您需要检查 $img 变量中的内容。乍一看,你的代码很好,但最好调试一下看看发生了什么。
-
@DanielProtopopov 谢谢。如果我只是回显
$img变量,它会输出类似����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90的代码。我不确定如何调试这个? -
@DanielProtopopov 谢谢,是的,我已经尝试过了,但无论我将其设置为
TRUE还是FALSE,它都会返回完全相同的值
标签: php intervention image-caching