montesjmm / resize-and-watermark
为 Laravel 优化的图片缩放和水印工具,轻松实现图片缩放和水印添加
0.3.1
2016-03-16 14:48 UTC
Requires
- php: >=5.4.0
- illuminate/support: ~5.2
- imagine/imagine: >=0.4.0
Requires (Dev)
- phpspec/phpspec: ~2.1
README
##图片缩放和水印(images)轻松自动化图片 上传、生成多个 尺寸 和(如需)水印。
##Laravel 5.2 安装
- 在 composer 需求中添加: "montesjmm/resize-and-watermark": "~0.3"
- 将服务提供者添加到你的 config/app.php: Montesjmm\ResizeAndWatermark\ResizeAndWatermarkServiceProvider::class,
- 在 config/app.php 别名中添加: 'ResizeAndWatermark' => Montesjmm\ResizeAndWatermark\ResizeAndWatermark::class,
- composer update
- php artisan vendor:publish
- php artisan migrate
- composer dump-autoload -o
- php artisan db:seed --class=RwPicturesSizesTableSeeder
##示例
####从图片 URL 下载并生成尺寸
$resizer = new ResizeAndWatermark; $picture = $resizer->store('http://example.com/image.jpg'); echo $picture->html('small'); // <img src="http://mysite.com/uploads/2015/03/20150327-picture_small.jpg"> echo $picture->html('big'); // <img src="http://laravel5.app/uploads/2015/03/20150327-picture_big.jpg">
####从表单上传的图片生成尺寸
$resizer = new ResizeAndWatermark; $file = Input::file()['file']; $picture = $resizer->store($file); echo $picture->html('small'); // <img src="http://mysite.com/uploads/2015/03/20150327-picture_small.jpg">
示例 App
####app/Http/routes.php
Route::get('/', 'WelcomeController@index'); Route::post('/', 'WelcomeController@index');
####app/Http/controllers/WelcomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Input; use Montesjmm\ResizeAndWatermark\ResizeAndWatermark; class WelcomeController extends Controller { public function index() { if (Request::isMethod('post')) { $resizer = new ResizeAndWatermark; $file = Input::file()['file']; $picture = $resizer->store($file); return '<img src="' . $picture->url('small') . '">'; } return view('welcome'); } }
####resources/views/welcome.blade.php
<html> <head> <title>Resize And Watermark Test</title> </head> <body> <form method="post" enctype="multipart/form-data"> <input type="hidden" name="_token" value="{{{ csrf_token() }}}"> <input type="file" name="file"> <input type="submit" value="send"> </form> </body> </html>
当你上传图片时,这是磁盘中的结果
private-uploads/2015/03/20150326-picture_orig.jpg public/uploads/2015/03/20150326-picture_bigger.jpg public/uploads/2015/03/20150326-picture_big.jpg public/uploads/2015/03/20150326-picture_medbig.jpg public/uploads/2015/03/20150326-picture_medium.jpg public/uploads/2015/03/20150326-picture_small.jpg public/uploads/2015/03/20150326-picture_thumb.jpg public/uploads/2015/03/20150326-picture_tiny.jpg
"laravel/private-uploads" 和 "laravel/public/uploads" 必须可写。
配置
在 config/resize-and-watermark.php 中,你可以设置水印文件的路径,如果你想使用水印。