uibar / gravatar
Laravel 5 的 Gravatar
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- mockery/mockery: ~0.9.4
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-28 17:54:31 UTC
README
本包提供了一种快速简单的方法在 Laravel 项目中实现 Gravatar。祝您使用愉快!
##特性
- 生成带有自定义参数的 URL
- 生成带有自定义参数和 HTML 属性的完整 img 标签
- 支持方法链式调用
- 自动设置安全连接
##未来特性
计划未来的特性
- 可以使用您自己的自定义图片作为默认图片(如logo等)
- 默认配置文件
- Gravatar 个人资料处理
##安装
-
获取包
composer require uibar/gravatar -
将服务提供者和外观添加到 config/app.php 文件中
'providers' => [ 'Uibar\Gravatar\GravatarServiceProvider',
'aliases' => [ 'Gravatar' => 'Uibar\Gravatar\Facades\Gravatar',
##使用要获取 Gravatar URL,只需这样
$gravatar_url = Gravatar::make('user@domain.com');
当然,我们也可以生成完整的图片标签。为了方便,我们可以以多种方式完成。无论如何,都需要指定电子邮件。
// We can directly specify some HTML tags as the second parameter of the // make() method like this: {!! Gravatar::make('email@address.com', ['class' => 'img-circle']) !!} // We could specify the email using Method Chaining and then passing TRUE // to the make() method {!! Gravatar::email('user@domain.com')->make(TRUE) !!} // Or we can pass an array as the first parameter containing the email and // the image key as TRUE like this: {!! Gravatar::make(['email' => Auth::user()->email, 'image' => TRUE]) !!} // You could also use the image() method chaining if you don't want to // specify any parameters like so: {!! Gravatar::image()->make('email@domain.com') !!}
所有这些都将返回默认的 80x80 大小的 Gravatar。
##安全连接类会根据请求类型自动设置 Gravatar URL。因此,如果您在您的域上启用了 SSL,您无需担心使用什么连接类型。
但是,有一种方法可以强制使用 HTTPS 来检索 Gravatar。使用 forceSecure() 方法。
$secure_url = Gravatar::forceSecure()->make('user@domain.com'); // Or you could specify inside the make() parameter array like this: $secure_url = Gravatar::make(['email' => 'user@domain.com', 'forceSecure' => TRUE]);
##自定义要自定义 Gravatar 返回,可以参考以下元素
$email => Gravatar 的电子邮件
$size => 像素大小 [1 - 2048]
$defaults => 如果未找到头像时使用的默认图片集 [404 | mm | identicon | monsterid | wavatar]
$rating => 接受的图片评级 [g | pg | r | x]
$image => 如果您想要返回完整的图片标签而不是图片 URL,则为 TRUE 或 FALSE
$attributes => 如果您选择这种方式,则需要为图片标签添加的额外属性
$forceSecure => 强制连接为安全(TRUE | FALSE)
这些元素可以通过将它们作为数组键传递给 make() 方法的第一参数来更改,也可以通过方法链式调用或通过 get() 方法参数来更改。
以下是三个具有相同效果的定制示例,展示了定制方法
// The following will output a full image tag with custom size, rating and // attributes for the img tag: {!! Gravatar::make([ 'email' => 'user@domain.com', 'size' => 40, 'rating' => 'x', 'image' => TRUE, 'attributes' => ['class' => 'img-circle'] ]) !!} // The same effect could be accomplished by passing the attributes for the // img tag as second parameter of the make method and thus not specifying the // image key as TRUE anymore {!! Gravatar::make([ 'email' => 'user@domain.com', 'size' => 40, 'rating' => 'x' ], ['class' => 'img-circle']) !!} // Using the get() method {!! Gravatar::get('user@domain.com', 40, 'identicon', 'x', TRUE, ['class' => 'img-circle']); !!} // The order of the elements using this way DOSE MATTER. It will not work unless // you use it using this exact order: $url = Gravatar::get($email, $size, $default, $rating, $image, $attributes);
您可以根据自己的需求选择最适合您的方法。
##方法链式调用您可以使用方法链式调用来自定义您的 Gravatar。使用与上面相同的方法名称。让我们看看一些例子
// For custom URLs: $custom_url = Gravatar::email('custom@email.com')->size(40) ->defaults('identicon')->rating('x')->make(); // For custom img tag: {!! Gravatar::email('custom@email.com')->size(40)->rating('x') ->attributes(['class' => 'img-circle'])->make(TRUE) !!} // Or as learned above we could do: {!! Gravatar::size(40)->rating('x')->make('user@domain.com', ['class' => 'img-circle']) !!}
请注意,在方法链式调用中,我们使用 make() 作为语句的结尾,而不是 get()。
##许可证本包在 MIT 许可证下发布,因此任何人都可以免费使用、分发和升级。
##感谢!感谢这些出色的人和他们的包,我受到了启发,写出了自己的 Gravatar Helper 表示。
感谢!