benbjurstrom/cloudflare-images-php 问题修复 & 功能扩展

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

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

benbjurstrom/cloudflare-images-php

Composer 安装命令:

composer require benbjurstrom/cloudflare-images-php

包简介

A PHP client for the Cloudflare Images API

README 文档

README

This is a framework-agnostic PHP client for Cloudflare Images built on the amazing Saloon v2 🤠 library.

Latest Version on Packagist GitHub Tests Action Status

Table of contents

🚀 Quick start

Install with composer.

composer require benbjurstrom/cloudflare-images-php

Create a new api instance.

use BenBjurstrom\CloudflareImages\CloudflareImages;
...

$api = new CloudflareImages(
    apiToken: $_ENV['CLOUDFLARE_IMAGES_API_TOKEN'],
    accountId: $_ENV['CLOUDFLARE_IMAGES_ACCOUNT_ID']
);

Then use it to get details about an existing image.

$id = '2cdc28f0-017a-49c4-9ed7-87056c83901'
$data = $api->images()->get($id);
$data->variants[0]; // https://imagedelivery.net/Vi7wi5KSItxGFsWRG2Us6Q/2cdc28f0-017a-49c4-9ed7-87056c83901/public

Or to upload a new image from an image string.

$fileName = 'example.jpg';
$file = file_get_contents($fileName);

$data = $api->images()->create($file, $fileName);
$data->id; // 2cdc28f0-017a-49c4-9ed7-87056c83901

Or generate a one time upload url that lets your users upload images directly to cloudflare without exposing your api key.

$data = $api->images()->getUploadUrl();
$data->uploadUrl; // https://upload.imagedelivery.net/Vi7wi5KSItxGFsWRG2Us6Q/d63a6953-12b9-4d89-b8d6-083c86289b93

You can find more information about direct creator uploads in the Cloudflare Docs.

Using with Laravel

Begin by adding your credentials to your services config file.

// config/services.php
'cloudflare' => [
    'api_token' => env('CLOUDFLARE_IMAGES_API_TOKEN'),
    'account_id' => env('CLOUDFLARE_IMAGES_ACCOUNT_ID'),
    'signing_key' => env('CLOUDFLARE_IMAGES_SIGNING_KEY'),
],

Bind the CloudflareImages class in a service provider.

// app/Providers/AppServiceProvider.php
public function register()
{
    $this->app->bind(CloudflareImages::class, function () {
        return new CloudflareImages(
            apiToken: config('services.cloudflare.api_token'),
            accountId: config('services.cloudflare.account_id'),
            signingKey: config('services.cloudflare.signing_key'),
        );
    });
}

And use anywhere in your application.

$data = app(CloudflareImages::class)->images()->get($id);

Test your integration using Saloon's amazing response recording.

use Saloon\Laravel\Saloon; // composer require sammyjo20/saloon-laravel "^2.0"
...
Saloon::fake([
    MockResponse::fixture('getImage'),
]);

$id = 'a74a4313-a51d-4837-b5c1-73e4c562ff00';

// The initial request will check if a fixture called "getImage" 
// exists. Because it doesn't exist yet, the real request will be
// sent and the response will be recorded to tests/Fixtures/Saloon/getImage.json.
$imgData = app(CloudflareImages::class)->images()->get($id);

// However, the next time the request is made, the fixture will 
// exist, and Saloon will not make the request again.
$imgData = app(CloudflareImages::class)->images()->get();

Response Data

All responses are returned as data objects. Detailed information can be found by inspecting the following class properties:

Image Metadata

Cloudflare allows you attach a modifiable key-value store to your images. To attach metadata to your image chain withMetadata($metadata) onto your api instance before calling the create, createFromUrl, update, or getUploadUrl methods. For example:

$url = 'https://en.wikipedia.org/wiki/File:Example.jpg'
$api->images()->withMetadata(['user_id' => '123'])->createFromUrl($url);

Private Images

Cloudflare allows you to configure an image to only be accessible with a signed URL token. To make an image private chain private(true) onto your api instance before calling the create, createFromUrl, update, or getUploadUrl methods. For example:

$api->images()->private(true)->getUploadUrl();

To generate signatures instantiate your api with the optional signing key parameter and then pass the url you want to sign to the signUrl method.

$api = new CloudflareImages(
    ...
    $signingKey: $_ENV['CLOUDFLARE_IMAGES_SIGNING_KEY']
);

$url = 'https://imagedelivery.net/2222222222222222222222/00000000-0000-0000-0000-000000000000/public';
$api->signUrl($url); // https://imagedelivery.net/2222222222222222222222/00000000-0000-0000-0000-000000000000/public?sig=8217cb17667a1f1af8ed722124d7a5da9543df9e3040a51f3de6e3023812ab3

You can find more information about serving private images in the Cloudflare documentation.

Custom IDs

Cloudflare allows you to configure a custom identifier if you wish. To do so chain withCustomId($id) onto your api instance before calling the create, createFromUrl, or getUploadUrl methods. For example:

$api->images()->withCustomId('test/image123')->create($file, $fileName);
$data->id; // test/image123

Note that images with a custom ID cannot be made private. You can find more information about custom ids in the Cloudflare documentation.

Available Image Methods

get()

Use to get details about an image such as its file name, metadata, and available variants. Returns an ImageData object.

use BenBjurstrom\CloudflareImages\Data\ImageData;
...
$id = '2cdc28f0-017a-49c4-9ed7-87056c83901'
/* @var ImageData $data */
$data = $api->images()->get($id);
$data->variants[0]; // https://imagedelivery.net/Vi7wi5KSItxGFsWRG2Us6Q/2cdc28f0-017a-49c4-9ed7-87056c83901/public

list()

Use to get a paginated list of images. Returns an ImagesData object.

use BenBjurstrom\CloudflareImages\Data\ImagesData
...

/* @var ImagesData $data */
$data = $api->images()->list(
    page: 1, // optional
    perPage: 25, // optional
);

$data->images[0]->id; // 00000000-0000-0000-0000-000000000000

create()

Use to upload an image from an image string. Returns an ImageData object.

use BenBjurstrom\CloudflareImages\Data\ImageData;
...
$fileName = 'example.jpg';
$file = file_get_contents($fileName);

/* @var ImageData $data */
$data = $api->images()
    ->private(false) // optional
    ->withCustomId('test/image123') // optional
    ->withMetadata(['user_id' => '123']) // optional
    ->create($file, $fileName);
$data->id; // test/image123

createFromUrl()

Use to add an image to Cloudflare from a given url. Returns an ImageData object.

use BenBjurstrom\CloudflareImages\Data\ImageData
...

$url = 'https://en.wikipedia.org/wiki/File:Example.jpg'

/* @var ImageData $data */
$data = $api->images()
    ->private(false) // optional
    ->withMetadata(['user_id' => '123']) // optional
    ->createFromUrl($id);

update()

Use to update an image's metadata or privacy setting. Returns an ImageData object.

⚠️ WARNING - Modifying an image's privacy setting will change the image's identifier.

use BenBjurstrom\CloudflareImages\Data\ImageData
...

$id = 'd63a6953-12b9-4d89-b8d6-083c86289b93'

/* @var ImageData $data */
$data = $api->images()
    ->private(false) // optional
    ->withMetadata(['user_id' => '123']) // optional
    ->update($id);

$data->id; // Contains a new id if the privacy setting was changed. If you are tracking IDs be sure to update your database.

delete()

Use to delete an image. Returns a boolean.

$id = 'd63a6953-12b9-4d89-b8d6-083c86289b93'
$data = $api->images()->delete($id);
$data // true

getUploadUrl()

Use to generate a one time upload url that lets your users upload images directly to cloudflare without exposing your api key. Returns an UploadUrlData object.

use BenBjurstrom\CloudflareImages\Data\UploadUrlData;
...

/* @var UploadUrlData $data */
$data = $api->images()
    ->withMetadata(['user_id' => '123']) // optional
    ->getUploadUrl();
$data->uploadUrl; // https://upload.imagedelivery.net/Vi7wi5KSItxGFsWRG2Us6Q/d63a6953-12b9-4d89-b8d6-083c86289b93

You can find more information about direct creator uploads in the Cloudflare Docs.

Variant Methods

list()

Use to get a list of all variants. Returns a VariantsData object.

use BenBjurstrom\CloudflareImages\Data\VariantsData
...

/* @var VariantsData $data */
$data = $api->variants()->all()
$data->variants[0]->id; // public
$data->variants[0]->width; // 1366
$data->variants[0]->height; // 768

benbjurstrom/cloudflare-images-php 适用场景与选型建议

benbjurstrom/cloudflare-images-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.74k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2023 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 benbjurstrom/cloudflare-images-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 23.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-01