【发布时间】:2018-07-28 20:28:20
【问题描述】:
我有一个网站,上面有图片上传/显示功能。所有图像都保存到特定路径的文件系统中。
我在项目中使用 Yii2 框架。图像没有直接的方法,所有图像都由特定的 URL 请求。 ImageController 处理 URL 并决定图像大小。 ImageModel 完成了这项工作。用户获取图片内容。
这里是代码sn-p:
$file = ... // full path to image
...
$ext = pathinfo($file)['extension'];
if (file_exists($file)) {
// return original
return Imagine::getImagine()
->open($file)
->show($ext, []);
}
preg_match("/(.*)_(\d+)x(\d+)\.{$ext}/", $file, $matches);
if (is_array($matches) && count($matches)) {
if (!file_exists("{$matches[1]}.{$ext}")) {
throw new NotFoundHttpException("Image doen't exist!");
}
$options = array(
'resolution-units' => ImageInterface::RESOLUTION_PIXELSPERINCH,
'resolution-x' => $matches[2],
'resolution-y' => $matches[3],
'jpeg_quality' => 100,
);
return Imagine::resize("{$matches[1]}.{$ext}", $matches[2], $matches[3])
->show($ext, $options);
} else {
throw new NotFoundHttpException('Wrong URL params!');
}
我们不在本主题中讨论数据缓存。
所以,我想知道这种方法的效率。是否可以通过 PHP 返回所有图像,即使它们根本没有更改?会不会增加服务器负载?
或者,也许,我应该将图像保存到另一个公共目录并将浏览器重定向到它?在一个页面上进行如此多的重定向需要多长时间(可能有很多图像)。搜索引擎优化呢?
我需要一个建议。解决此类任务的最佳做法是什么?
【问题讨论】: