首页 >php教程 >php手册 >Laravel4创建一个占位图片服务例子

Laravel4创建一个占位图片服务例子

WBOY
WBOY原创
2016-05-25 16:45:291550浏览

下面我来给大家转一个关于Laravel4创建一个占位图片服务例子,使用Composer安装intervention/image库,代码如下:

composer require intervention/image:dev-master

编码,代码如下:

// vim app/routes.php 

<?php 
Route::pattern(&#39;width&#39;, &#39;\d+&#39;); 
Route::pattern(&#39;height&#39;, &#39;\d+&#39;); 
Route::get(&#39;{width}x{height}&#39;, &#39;ImageHolderController@getIndex&#39;); 
// vim app/controllers/ImageHolderController.php
?>
<?php 
 
class ImageHolderController extends BaseController { 
    public function getIndex($width, $height) 
    { 
        $width = intval($width); 
        $height = intval($height); 
        if ($width > 1900 || $height > 900) 
            App::abort(404); 
        $fontSize = min(max(intval($width / 5), 12), 38)(www.111cn.net); 
        $image = Image::canvas($width, $height, &#39;#CCCCCC&#39;) 
                ->line(&#39;#B5B5B5&#39;, 0, 0, $width, $height) 
                ->line(&#39;#B5B5B5&#39;, $width, 0, 0, $height) 
                ->text($width . &#39;x&#39; . $height, $width / 2, $height / 2, function ($font) use ($fontSize) { 
                    $font->file(public_path(&#39;font/Georgia.ttf&#39;)); 
                    $font->align(&#39;center&#39;); 
                    $font->valign(&#39;middle&#39;); 
                    $font->size($fontSize); 
                    $font->color(&#39;#666666&#39;); 
                }); 
        return Response::make($image, 200, array(&#39;Content-Type&#39; => &#39;image/png&#39;)); 
    } 
} 
?>


永久地址:

转载随意~请带上教程地址吧^^

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn