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

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

WBOY
WBOYOriginal
2016-05-25 16:45:291551browse

下面我来给大家转一个关于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;)); 
    } 
} 
?>


永久地址:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn