marksitko / laravel-unsplash

为Laravel提供流畅的Unsplash API

2.2.1 2024-04-29 05:57 UTC

This package is auto-updated.

Last update: 2024-09-02 06:28:29 UTC


README

提供流畅的API以在Laravel应用程序中使用Unsplash。使用公共操作或直接在您的存储中存储图像,并使用数据库连接器自动持久化所有版权信息。

Latest Version on Packagist Build Status Quality Score Total Downloads

安装

$ composer require marksitko/laravel-unsplash

Laravel-Unsplash包含包发现,Laravel将自动注册服务提供者和外观。如果您想手动添加它,您应该在config/app.php中提供它

服务提供者

'providers' => [
    //...
    MarkSitko\LaravelUnsplash\UnsplashServiceProvider::class,
];

外观

'aliases' => [
    //...
    'Unsplash' => MarkSitko\LaravelUnsplash\Facades\Unsplash::class,
];

接下来,您应该发布配置。

$ php artisan vendor:publish --tag=config
可选

如果您想使用数据库连接器与Laravel-Unsplash一起使用,您必须发布迁移文件。

$ php artisan vendor:publish --tag=migrations

它创建了2个迁移。一个用于存储存储图像的附加信息,另一个用于与HasUnsplashables特质一起使用。

配置

您必须在.env文件中提供一个Unsplash API访问密钥。阅读如何生成Unsplash API密钥

UNSPLASH_ACCESS_KEY=YOUR_GENERATED_API_KEY_FROM_UNSPLASH

可选配置

# default is false
UNSPLASH_STORE_IN_DATABASE=BOOLEAN

# default is local
UNSPLASH_STORAGE_DISK=YOUR_STORAGE_DISC

基本用法

查看完整的Unsplash API文档https://unsplash.com/documentation

随机照片

// Returns the http response body.
$twoRandomPhotosOfSomePeoples = Unsplash::randomPhoto()
    ->orientation('portrait')
    ->term('people')
    ->count(2)
    ->toJson();

// Store the image in on your provided disc
$theNameFromTheStoredPhoto = Unsplash::randomPhoto()
    ->orientation('landscape')
    ->term('music')
    ->randomPhoto()
    ->store();
];

照片

$photos = Unsplash::photos()->toJson();
$photo = Unsplash::photo($id)->toJson();
$photosStatistics = Unsplash::photosStatistics($id)->toJson();
$trackPhotoDownload = Unsplash::trackPhotoDownload($id)->toJson();

用户

$user = Unsplash::user($username)->toJson();
$userPortfolio = Unsplash::userPortfolio($username)->toJson();
$userPhotos = Unsplash::userPhotos($username)->toJson();
$userLikes = Unsplash::userLikes($username)->toJson();
$userCollections = Unsplash::userCollections($username)->toJson();
$userStatistics = Unsplash::userStatistics($username)->toJson();

搜索

$search = Unsplash::search()
    ->term('buildings')
    ->color('black_and_white')
    ->orientation('squarish')
    ->toJson();

$searchCollections = Unsplash::searchCollections()
    ->query('events')
    ->page($pageNumber)
    ->toJson();

$searchUsers = Unsplash::searchUsers()
    ->query('search_term')
    ->toJson();

收藏夹

$collectionsList = Unsplash::collectionsList()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$featuredCollection = Unsplash::featuredCollection()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$showCollection = Unsplash::showCollection()
    ->id($collectionId)
    ->toJson();

$showCollectionPhotos = Unsplash::showCollectionPhotos()
    ->id($collectionId)
    ->toJson();

$showCollectionRelatedCollections = Unsplash::showCollectionRelatedCollections()
    ->id($collectionId)
    ->toJson();

主题

$topicsList = Unsplash::topicsList()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$showTopic = Unsplash::showTopic()
    ->id($topicIdOrSlug)
    ->toJson();

$showTopicPhotos = Unsplash::showTopicPhotos()
    ->id($topicIdOrSlug)
    ->toJson();

统计数据

$totalStats = Unsplash::totalStats()->toJson();
$monthlyStats = Unsplash::monthlyStats()->toJson();

与数据库的使用

如果您想自动持久化有关您存储的图像的一些信息(例如,版权),您必须运行已发布的迁移。如果没有运行可选命令,我们将从头开始

$ php artisan vendor:publish --tag=migrations
$ php artisan migrate

迁移成功后,您必须调整.env

UNSPLASH_STORE_IN_DATABASE=true

现在,当您在Unsplash客户端上执行store()时,图像将存储在您提供的磁盘上,并且信息如下:

  • unsplash照片ID
  • 存储的图像名称
  • 作者姓名
  • 作者链接

然而,所有这些信息都是使用Unsplash照片在您的网站上所必需的。

使用Unsplash客户端的示例

// Returns the created unsplash asset record
$databaseRecord = Unsplash::randomPhoto()->store();

您现在还可以使用内置的UnsplashAsset模型使用UnsplashAsset模型的示例

// Returns the created unsplash asset record
$databaseRecord = UnsplashAsset::api()->randomPhoto()->store();

// Get an stored unsplash asset
$unsplashAsset = UnsplashAsset::find($id);

您还可以在任何模型上使用HasUnsplashables特质。

在用户模型上使用HasUnsplashables特质的示例

use Illuminate\Foundation\Auth\User as Authenticatable;
use MarkSitko\LaravelUnsplash\Traits\HasUnsplashables;

class User extends Authenticatable
{
    use HasUnsplashables;

    // ...
}

现在您可以使用它

// store the unsplash asset in a morphToMany relation
$unsplashAsset = Unsplash::randomPhoto()->store();
User::unsplash()->save($unsplashAsset);

// retrive all related unsplash assets
User::find($userId)->unsplash();

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。

Laravel包模板

此包是使用Laravel包模板生成的。