thezeroday/laravel-unsplash 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thezeroday/laravel-unsplash

Composer 安装命令:

composer require thezeroday/laravel-unsplash

包简介

Provides a fluent Unsplash API for Laravel

README 文档

README

Provides a fluent api to use the Unsplash within Larvel applications. Use public actions or store images directly in your storage and persists all copyright informations automatically with the databse connector.

Latest Version on Packagist Build Status Quality Score Total Downloads

Install

$ composer require marksitko/laravel-unsplash

Laravel-Unsplash comes with package discovery and Laravel will register the service provider and facade automatically. Just in case you wanna add it manually, you should provide it in config/app.php

Service provider

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

Facade

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

Next you should publish the configuration.

$ php artisan vendor:publish --tag=config
Optional

If you wanna use Laravel-Unsplash with the database connector you have to publish the migration files.

$ php artisan vendor:publish --tag=migrations

It creates 2 migrations. One to store additional informations about stored image and one morph table to use it with the HasUnsplashables trait.

Configuration

You have to provide a unsplash api access key in your .env file. Read how to generate a Unsplash API key

UNSPLASH_ACCESS_KEY=YOUR_GENERATED_API_KEY_FROM_UNSPLASH

Optional configurations:

# default is false
UNSPLASH_STORE_IN_DATABASE=BOOLEAN

# default is local
UNSPLASH_STORAGE_DISK=YOUR_STORAGE_DISC

Basic Usage

Take a look at the full Unsplash API documentation https://unsplash.com/documentation

Random Photos

// 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

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

Users

$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

$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();

Collections

$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();

Topics

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

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

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

Stats

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

Usage with Database

If you wanna persist automaticly some informations (i.e. Copyrights) about you stored images you have to run the published migrations. In case you dont have ran the optional command, we start at the beginning:

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

When migration is successfull, you have to adjust the .env

UNSPLASH_STORE_IN_DATABASE=true

Now when you execute store() on the Unsplash client, the image is stored in your provided disc and informations like

  • the unsplash photo id
  • the stored image name
  • author name
  • author link

However, these informations are all required to use a unsplash photo on your website.

Example with Unsplash Client

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

You are now also able to use the build in UnsplashAsset Model Example with UnsplashAsset Model

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

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

You can also use the HasUnsplashables Trait on any model.

Example with HasUnsplashables Trait on the User Model

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

class User extends Authenticatable
{
    use HasUnsplashables;

    // ...
}

Now you are able to use it like:

// 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();

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

thezeroday/laravel-unsplash 适用场景与选型建议

thezeroday/laravel-unsplash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 195 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「marksitko」 「laravel-unsplash」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 thezeroday/laravel-unsplash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thezeroday/laravel-unsplash 我们能提供哪些服务?
定制开发 / 二次开发

基于 thezeroday/laravel-unsplash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 195
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-18