dedmytro/laravel-cloudflare-images
Composer 安装命令:
composer require dedmytro/laravel-cloudflare-images
包简介
Cloudflare API client and filesystem for Laravel
README 文档
README
Provides access to Cloudflare Images API for Laravel projects
Table of contents
Installation
To get the latest version of Laravel CloudflareImages, simply require the project using Composer:
$ composer require dedmytro/laravel-cloudflare-images
Or manually update require block of composer.json and run composer update.
{
"require": {
"dedmytro/laravel-cloudflare-images": "^0.2"
}
}
Configuration
Add environment variables to your .env file:
CLOUDFLARE_IMAGES_ACCOUNT='your-account-id' CLOUDFLARE_IMAGES_KEY='your-api-key' CLOUDFLARE_IMAGES_DELIVERY_URL='https://imagedelivery.net/ZWd9g1K8vvvVv_Yyyy_XXX' CLOUDFLARE_IMAGES_DEFAULT_VARIATION='your-default-variation' CLOUDFLARE_IMAGES_SIGNATURE_TOKEN='your-signature-token'
or publish config and set up vars there
return [ 'account'=> env('CLOUDFLARE_IMAGES_ACCOUNT'), 'key'=> env('CLOUDFLARE_IMAGES_KEY'), 'delivery_url' => env('CLOUDFLARE_IMAGES_DELIVERY_URL'), 'default_variation' => env('CLOUDFLARE_IMAGES_DEFAULT_VARIATION'), 'signature_token' => env('CLOUDFLARE_IMAGES_SIGNATURE_TOKEN') ];
CLOUDFLARE_IMAGES_KEY - is an API Token. To create a new one go to User Api Tokens on Cloudflare dashboard
CLOUDFLARE_IMAGES_ACCOUNT - is an Account ID on the Overview page
CLOUDFLARE_IMAGES_DELIVERY_URL - is an Image Delivery URL on the Overview page
CLOUDFLARE_IMAGES_DEFAULT_VARIATION - is a variation on the Variants page
CLOUDFLARE_IMAGES_SIGNATURE_TOKEN - is a token from the Images -> Keys page
Using
Direct upload
The Direct upload is feature of Cloudflare Images to upload image directly from frontend but without sharing your api key. Once you get this url you can use inside your html
<form method="post" enctype="multipart/form-data" action="{{ $uploadUrl }}">
IMPORTANT: You can use this url only once!
use DeDmytro\CloudflareImages\Facades\CloudflareApi; $response = CloudflareApi::images()->directUploadUrl() $response->result->id; // Your uploaded image ID $response->result->uploadURL; // One-time uploadUrl
Upload
Call upload() method and pass file as local file path or UploadedFile instance. As a result of upload you'll get DetailsResponse instance with uploaded
image details, so you can save it locally.
use DeDmytro\CloudflareImages\Facades\CloudflareApi; use DeDmytro\CloudflareImages\Http\Responses\DetailsResponse; use DeDmytro\CloudflareImages\Http\Entities\Image; /* @var $file \Illuminate\Http\UploadedFile|string */ /* @var $response DetailsResponse*/ $response = CloudflareApi::images()->upload($file) /* @var $image Image*/ $image = $response->result $image->id; $image->filename; $image->variants->thumbnail; //Depends on your Cloudflare Images Variants setting $image->variants->original; //Depends on your Cloudflare Images Variants setting
List
To list existing images you should use list() method which also has pagination and accept $page and $perPage arguments.
use DeDmytro\CloudflareImages\Facades\CloudflareApi; /* @var $response ListResponse*/ $response = CloudflareApi::images()->list() //OR $response = CloudflareApi::images()->list($page, $perPage) foreach($response->result as $image){ $image->id; $image->filename; $image->variants->thumbnail; //Depends on your Cloudflare Images Variants setting $image->variants->original; //Depends on your Cloudflare Images Variants setting }
Details
To get existing image details you should use get($id) method where $id is image identifier you received when you list or upload the image.
use DeDmytro\CloudflareImages\Facades\CloudflareApi; $response = CloudflareApi::images()->get($id) $image = $response->result; $image->id; $image->filename; $image->variants->thumbnail; //Depends on your Cloudflare Images Variants setting $image->variants->original; //Depends on your Cloudflare Images Variants setting
Delete
To delete existing image you should use delete($id) method where $id is image identifier you received when you list or upload the image.
use DeDmytro\CloudflareImages\Facades\CloudflareApi; $response = CloudflareApi::images()->delete($id) $response->success
Public url
To generate image url locally call method url($id) and pass image ID. Don't forget to set up
CLOUDFLARE_IMAGES_DELIVERY_URL= CLOUDFLARE_IMAGES_DEFAULT_VARIATION=
use DeDmytro\CloudflareImages\Facades\CloudflareApi; $url = CloudflareApi::images()->url($id)
<img src="{{ $url }}"/>
Signed url
To generate signed image url locally call method signedUrl($id, $expires = 3600) and pass image ID and expiration time in seconds. Don't forget to set up
CLOUDFLARE_IMAGES_DELIVERY_URL= CLOUDFLARE_IMAGES_DEFAULT_VARIATION= CLOUDFLARE_IMAGES_SIGNATURE_TOKEN=
use DeDmytro\CloudflareImages\Facades\CloudflareApi; $url = CloudflareApi::images()->signedUrl($id, $expires)
<img src="{{ $url }}"/>
dedmytro/laravel-cloudflare-images 适用场景与选型建议
dedmytro/laravel-cloudflare-images 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.38k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2021 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「cloudflare」 「CloudFlare API」 「cloudflare images」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dedmytro/laravel-cloudflare-images 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dedmytro/laravel-cloudflare-images 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dedmytro/laravel-cloudflare-images 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle for Cloudflare integration: trusted proxies, real client IP detection, forwarded headers.
A PSR-7 compatible library for making CRUD API endpoints
Cloudflare Middleware For Guzzle
Library for helping bypass the Cloudflare IUAM page
A ZF2 plugin to fix global variables when using CloudFlare
Boost the performance of your site with Cloudflare or Varnish.
统计信息
- 总下载量: 79.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-30