Laravel 图像缓存比源文件慢
Laravel Image Cache slower than source
我正在使用 Intervention/imagecache 来缓存我的图像。
但是缓存图像加载速度比源图像文件慢。
几乎额外的 60-70 毫秒时间延迟(在 chrome inspect element 网络中测试)
这是我在 Route.php
加载图像的代码
Route::get('images/cars/{src}', function ($src){
$cacheimage = Image::cache(function($image) use($src){
return $image->make("images/products/".$src);
},1440);
return Response::make($cacheimage,200, array('Content-Type'=>'image/jpg'));
});
在blade
<img src="{{ URL::asset('/images/cars/theimage.jpg' }}" alt="">
有什么想法或更好的方法来存储图像缓存吗?
我从未使用过laravel,但这是一个普遍问题。
如果让网络服务器处理图像到客户端的传送,php 解释器将不会启动。
如果您通过 PHP 传递内容(我假设,因为您写了一些关于缓存图像的内容),您需要 php 解释器。然后您需要执行脚本及其所有逻辑,脚本语言总是比本地语言慢。
你最好的选择是,将图像保存在文件系统上,并 link 到它,而不是 PHP 脚本。
这意味着例如:
在您的应用程序中的某处,您有一个点,即创建原始图像的位置。现在想想,你需要它的什么版本。随心所欲地调整大小、裁剪、编辑它。将您需要的每个版本保存在您的文件系统中。所以你有 image.jpg
而不是 image-200x200-cropped-with-branding.jpg
。在这一点上,性能应该没有那么重要(图像将被查看数千次,但只创建一次)。
你想要
<img src="/path/to/image-200x200-cropped-with-branding.jpg">;
而不是
<img src="/image.php?param1=1¶m2=2">;
只是一些额外的想法,基于 Christian Gollhardt 的回答。
他说得很对,这是一个普遍的问题。但我不喜欢他创建创建(或上传)原始图像所需的所有版本的方法。因为有一个大问题,如果 - 在未来的某个时候 - 您决定您的缩略图应该是 250x250 而不是 200x200(或任何其他尺寸)怎么办?所以基本上我想要的是 ImageCache 包提供的灵活性,而不会降低性能。
我还没有实际实现这个,但我的方法是使用某种 - 介于两者之间的 - 辅助函数将所有图像包含在视图中。本质上,辅助函数将模拟图像缓存的功能,但不是在实际图像请求上处理所有逻辑,而是在页面请求期间处理。所以当用户浏览器请求实际图像时,每个图像版本都已经在服务器上创建并且 link 将指向文件系统上的实际图像。一些伪代码更好地解释了它...
例如在 show_profile.blade 视图中
<h1>{{ $profile->name }}</h1>
<img src="{{ image_helper($profile->image->filename, 'small') }}">
helpers.php
function image_helper($filename, $version) {
if (!file_exists($version . '_' . $filename)) {
// some other helper function ...
create_image_version($filename, $version);
}
return "my/images/" . $version . '_' . $filename;
}
function create_image_version($filename, $version) {
// if you want to go that route, you would need some kind of mapping
// that maps the $version (string) to a codeblock that actually knows
// what to do if that version is requested.
// E.g. if the version 'small' is requested,
// create an image with a dimension of 100x100
}
我正在使用 Intervention/imagecache 来缓存我的图像。 但是缓存图像加载速度比源图像文件慢。 几乎额外的 60-70 毫秒时间延迟(在 chrome inspect element 网络中测试)
这是我在 Route.php
加载图像的代码 Route::get('images/cars/{src}', function ($src){
$cacheimage = Image::cache(function($image) use($src){
return $image->make("images/products/".$src);
},1440);
return Response::make($cacheimage,200, array('Content-Type'=>'image/jpg'));
});
在blade
<img src="{{ URL::asset('/images/cars/theimage.jpg' }}" alt="">
有什么想法或更好的方法来存储图像缓存吗?
我从未使用过laravel,但这是一个普遍问题。
如果让网络服务器处理图像到客户端的传送,php 解释器将不会启动。
如果您通过 PHP 传递内容(我假设,因为您写了一些关于缓存图像的内容),您需要 php 解释器。然后您需要执行脚本及其所有逻辑,脚本语言总是比本地语言慢。
你最好的选择是,将图像保存在文件系统上,并 link 到它,而不是 PHP 脚本。
这意味着例如:
在您的应用程序中的某处,您有一个点,即创建原始图像的位置。现在想想,你需要它的什么版本。随心所欲地调整大小、裁剪、编辑它。将您需要的每个版本保存在您的文件系统中。所以你有 image.jpg
而不是 image-200x200-cropped-with-branding.jpg
。在这一点上,性能应该没有那么重要(图像将被查看数千次,但只创建一次)。
你想要
<img src="/path/to/image-200x200-cropped-with-branding.jpg">;
而不是
<img src="/image.php?param1=1¶m2=2">;
只是一些额外的想法,基于 Christian Gollhardt 的回答。
他说得很对,这是一个普遍的问题。但我不喜欢他创建创建(或上传)原始图像所需的所有版本的方法。因为有一个大问题,如果 - 在未来的某个时候 - 您决定您的缩略图应该是 250x250 而不是 200x200(或任何其他尺寸)怎么办?所以基本上我想要的是 ImageCache 包提供的灵活性,而不会降低性能。
我还没有实际实现这个,但我的方法是使用某种 - 介于两者之间的 - 辅助函数将所有图像包含在视图中。本质上,辅助函数将模拟图像缓存的功能,但不是在实际图像请求上处理所有逻辑,而是在页面请求期间处理。所以当用户浏览器请求实际图像时,每个图像版本都已经在服务器上创建并且 link 将指向文件系统上的实际图像。一些伪代码更好地解释了它...
例如在 show_profile.blade 视图中
<h1>{{ $profile->name }}</h1>
<img src="{{ image_helper($profile->image->filename, 'small') }}">
helpers.php
function image_helper($filename, $version) {
if (!file_exists($version . '_' . $filename)) {
// some other helper function ...
create_image_version($filename, $version);
}
return "my/images/" . $version . '_' . $filename;
}
function create_image_version($filename, $version) {
// if you want to go that route, you would need some kind of mapping
// that maps the $version (string) to a codeblock that actually knows
// what to do if that version is requested.
// E.g. if the version 'small' is requested,
// create an image with a dimension of 100x100
}