【问题标题】:define a Route to show images via timthumb php script in laravel在 laravel 中定义一个通过 timthumb php 脚本显示图像的路由
【发布时间】:2015-10-28 06:37:17
【问题描述】:

我想要一个 Route 获取图像尺寸和来源,并通过 timthumb php 脚本返回裁剪和调整大小的图像。

我把 timthumb.php 文件放在 public 目录的一个文件夹中,我写了这条路线:

Route::get('/showImage/{w}/{h}/{src}', function ($w , $h , $src) {

    $img    =   'public/plugins/timthumb/timthumb.php?src='.$src.'&w='.$w.'&h='.$h;
    return Response::make($img, 200, array('Content-Type' => 'image/jpeg'));
})->where('src', '[A-Za-z0-9\/\.\-\_]+');

但是什么也没发生。

如何访问 timthumb.php 文件并发送所需参数并获取结果图像?

更新: 这是公共目录的结构以及 timthumb 和图像文件夹的位置:

根据,我试试:

$img    =   'public/plugins/timthumb/timthumb.php?src=../../'.$src.'&w='.$w.'&h='.$h;

还有:

$img    =   'public/plugins/timthumb/timthumb.php?src=public/'.$src.'&w='.$w.'&h='.$h;

但它们都不适用于以下网址:

http://localhost:8000/showImage/100/200/upload/slideshow/slide2.jpg 

【问题讨论】:

  • 什么也没发生究竟是什么意思?
  • 意味着当我尝试这个简单的图像时,只是一个以 chrome 显示的损坏的图像图标。我尝试 localhost:8000/showImage/100/200/upload/slideshow/slide2.jpg 而所有参数都给出并且 upload/slideshow/slide2.jpg 存在但它没有显示。
  • 我认为问题在于,您的路径与您正在使用的脚本相关。所以它在 public/plugins/timthumb/upload... 上寻找你的图片。
  • @Tim,我用新信息更新了我的问题

标签: php laravel


【解决方案1】:

我建议使用league/glide。观看Using Glide in Laravel 开始。

【讨论】:

  • 是否支持像timthumb这样的缓存?
  • 我不确定。 Timthumb 开发人员不为其提供支持 (binarymoon.co.uk/projects/timthumb/timthumb-technical-support)。我确信您可以使用 League/glide 完成您需要的所有图像处理。
  • @ahmad Glide 的作者在这里。是的,它确实支持缓存,这是 Glide 工作方式的核心。有关更多信息,请参阅文档here
  • @Jonathan,是的,我也在使用 Glide。它很棒。
【解决方案2】:

使用 intervention/image 在 Laravel 中使用 timthumb 图像的更好方法 安装这个包。

更新您的路线:

Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src) 
{

    $img_path = public_path().'/'.$src;
    $img = Image::make($img_path)->resize($w, $h);

    return $img->response('jpg');
})->where('src', '[A-Za-z0-9\/\.\-\_]+');

或图像缓存

安装intervention/imagecache

Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src) 
    {

        $img_path = public_path().'/'.$src;
        $img =  Image::cache(function($image)use($w,$h,$img_path) {
                return $image->make($img_path)->resize($w, $h);
            });
        return Response::make($img, 200, ['Content-Type' => 'image/jpeg']);
    })->where('src', '[A-Za-z0-9\/\.\-\_]+');

图片网址如下:

http://>/showImage/800/400/Desert.jpg

【讨论】:

  • 我已经尝试过干预,但是它有很多缓存问题并且不能很好地支持缓存,但这对我来说非常重要。
  • 使用intervention/imagecache包解决缓存问题。
猜你喜欢
  • 2011-10-21
  • 2011-08-28
  • 2017-01-14
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多