Laravel 5intervention-image/intervention-cache:灵活的url/路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29098814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 intervention-image / intervention-cache: flexible url / routing
提问by Klaaz
Fiddling around with Intervention 2.0in Laravel 5. What I want is to manipulate images (size and cropping) and use Interventions Image Cachingto cache the images. What I basically try to achieve is the functionality of good old (and insecure) timthumb.php.
摆弄周围与干预2.0在Laravel 5.我想是处理图像(大小和裁剪),并使用干预图像缓存到缓存图像。我基本上试图实现的是旧的(和不安全的)timthumb.php 的功能。
I started using this as an example:
我开始以此为例:
// routes.php
Route::get('imager/{src?}', function ($src)
{
$cacheimage = Image::cache(function($image) use ($src) {
return $image->make("files/image/".$src)->resize(100,50);
}, 10, true);
return Response::make($cacheimage, 200, array('Content-Type' => 'image/jpeg'));
});
When I load an image like so:
当我像这样加载图像时:
// template
<img src="{{"imager/image.jpg"}}"/>
It works fine.
它工作正常。
But... In my situation images can be located in different (sub) directories, sometimes multiple levels deep. They are maintained in my CMS by their webmasters.
但是......在我的情况下,图像可以位于不同的(子)目录中,有时是多层次的。它们由他们的网站管理员在我的 CMS 中维护。
examples:
例子:
- files/images/image.jpg
- files/images/headers/image.jpg
- files/images/background/color/image.jpg
- img/common/logo.png
- 文件/图像/图像.jpg
- 文件/图像/标题/image.jpg
- 文件/图像/背景/颜色/image.jpg
- img/common/logo.png
These image url's are loaded from a mysql table record.
这些图像 url 是从 mysql 表记录加载的。
When such an images loads:
当这样的图像加载时:
// template
<img src="{{"imager/files/images/image.jpg"}}"/>
The route is not working anymore. After all, files, imagesand images.jpgare all url segments and the amount of them could differ.
该路线不再有效。毕竟files、images和images.jpg都是url段,数量可能会有所不同。
The image url (Bold) should be handled as one variable:
图像 url(粗体)应作为一个变量处理:
Route::get('imager/files/images/image.jpg', function ($src = false)
Route::get('imager/ files/images/image.jpg', function ($src = false)
Then I should be able to pass the sizing and cropping parameters off course. Because the img url length could vary I assume I could pass the parameters with a query like ?w=100&h=50&c=true or something?
然后我应该能够将尺寸和裁剪参数传递出去。因为 img url 长度可能会有所不同,所以我假设我可以通过诸如 ?w=100&h=50&c=true 之类的查询传递参数?
Update
更新
When I use a query parameter for the image url:
当我对图像 url 使用查询参数时:
Route::get('imager', function ()
{
$src = Input::get('src', 1);
$cacheimage = Image::cache(function($image) use ($src) {
return $image->make($src)->resize(100,100);
}, 1, false); // one minute cache expiry
return Response::make($cacheimage, 200, array('Content-Type' => 'image/jpeg'));
});
// template
<img src="{{"imager?src=files/images/image.jpg"}}"/>
This works.
这有效。
回答by Sh1d0w
It is simple. You just have to tell Laravel that your image parameter consists of letters, slash, dash, underscore and dot ('[A-Za-z0-9\/\.\-\_]+'
), because by default the framework matches everything but the slash /
.
很简单。你只需要告诉 Laravel 你的图像参数由字母、斜杠、破折号、下划线和点 ( '[A-Za-z0-9\/\.\-\_]+'
) 组成,因为默认情况下框架匹配除斜杠之外的所有内容/
。
Route::get('imager/{image?}', function($src) {
$cachedImage = Image::cache(function($image) use ($src) {
return $image->make($src)->resize(100,100);
}, 1, false);
return Response::make($cachedImage, 200, ['Content-Type' => 'image/jpeg']);
})->where('image', '[A-Za-z0-9\/\.\-\_]+');
You can find out more about parameter binding in the documentation.
您可以在文档中找到有关参数绑定的更多信息。
回答by Santi Barbat
Now you can now use the URL based image manipulation:
现在您可以使用基于 URL 的图像处理:
Within a Laravel application it is possible to use the URL to manipulate images dynamically. The manipulated version of the an image will be stored in the cache and will be loaded directly without resource-intensive GD operation.
An image has to be uploaded only once. All manipulations like resizing or cropping will be handled later, when the file is accessed via a HTTP request like this:
http://yourhost.com/{route-name}/{template-name}/{file-name}
在 Laravel 应用程序中,可以使用 URL 动态操作图像。图像的操纵版本将存储在缓存中,并且将直接加载而无需资源密集型 GD 操作。
一张图片只需上传一次。当通过像这样的 HTTP 请求访问文件时,将在稍后处理所有操作,如调整大小或裁剪:
http://yourhost.com/{route-name}/{template-name}/{file-name}