robertpainsi/sanity-image-url-php 问题修复 & 功能扩展

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

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

robertpainsi/sanity-image-url-php

Composer 安装命令:

composer require robertpainsi/sanity-image-url-php

包简介

Tools to generate image urls from Sanity content in PHP

README 文档

README

Packagist Build Status

This PHP library is a direct port of the official Sanity JavaScript library @sanity/image-url. Version maps to @sanity/image-url @fff9fc1 Aug 8, 2024.

Quickly generate image urls from Sanity image records.

This helper will by default respect any crops/hotspots specified in the Sanity content provided to it. The most typical use case for this is to give it a sanity image and specify a width, height or both and get a nice, cropped and resized image according to the wishes of the content editor and the specifications of the front end developer.

In addition to the core use case, this library provides a handy builder to access the rich selection of processing options available in the Sanity image pipeline.

Requirements

sanity-image-url-php requires PHP >= 7.4.

Composer

You can install the library via Composer. Run the following command:

composer require robertpainsi/sanity-image-url-php

To use the library, use Composer's autoload:

require_once 'vendor/autoload.php';

Usage

The most common way to use this library in your project is to configure it by passing it your sanity client configuration. That way it will automatically be preconfigured to your current project and dataset:

use function SanityImageUrl\urlBuilder;

$builder = urlBuilder( [
    'projectId' => 'zp7mbokg',
    'dataset'   => 'production',
    // ...
] );

function urlFor( $source ) {
    global $builder;

    return $builder->image( $source );
}

When working with the official sanity-php library, you can also initialize the urlBuilder by passing the Sanity\Client as parameter.

$client  = new Sanity\Client( [
    'projectId' => 'zp7mbokg',
    'dataset'   => 'production',
    // ...
] );
$builder = urlBuilder( $client );

Once the builder is initialized, you can use the handy builder syntax to generate your urls:

<img src="<?php echo urlFor( $author[ 'image' ] )->width( 200 )->url(); ?>" />

This will ensure that the author image is always 200 pixels wide, automatically applying any crop specified by the editor and cropping towards the hot-spot she drew. You can specify both width and height like this:

<img src="<?php echo urlFor( $movie[ 'poster' ] )->width( 500 )->height( 300 )->url(); ?>" />

There are a large number of useful options you can specify, like e.g. blur:

<img src="<?php echo urlFor( $mysteryPerson[ 'mugshot' ] )->width( 200 )->height( 200 )->blur( 50 )->url(); ?>" />

Note that the url() function needs to be the final one in order to output the url as a string.

Full usage example

// author.ts
export default {
    name: 'author',
    type: 'document',
    title: 'Author',
    fields: [{
        name: 'image',
        type: 'image',
        title: 'Image',
        options: {
            hotspot: true,
        }
    }]
}
// author.php
require_once 'vendor/autoload.php';

use function SanityImageUrl\urlBuilder;

const SANITY_CONFIG = [
    'projectId' => 'zp7mbokg',
    'dataset'   => 'production',
];

$client  = new Sanity\Client( SANITY_CONFIG );
$builder = urlBuilder( $client ); // Instead of $client you can also use the SANITY_CONFIG array

$authors = $client->fetch( "*[_type == 'author']" );
foreach ( $authors as $author ) {
    echo '<img src="' . $builder->image( $author[ 'image' ] )->width( 320 )->height( 640 )->url() . '" />';
}

Builder methods

image( $source )

Specify the image to be rendered. Accepts either a Sanity image record, an asset record, or just the asset id as a string. In order for hotspot/crop processing to be applied, the image record must be supplied, as well as both width and height.

dataset( $dataset ), projectId( $projectId )

Usually you should preconfigure your builder with dataset and project id, but even when you did, these let you temporarily override them if you need to render assets from other projects or datasets.

width( $pixels )

Specify the width of the rendered image in pixels.

height( $pixels )

Specify the height of the rendered image in pixels.

size( $width, $height )

Specify width and height in one go.

focalPoint( $x, $y )

Specify a center point to focus on when cropping the image. Values from 0.0 to 1.0 in fractions of the image dimensions. When specified, overrides any crop or hotspot in the image record.

blur( $amount ), sharpen( $amount ), invert()

Apply image processing.

rect( $left, $top, $width, $height )

Specify the crop in pixels. Overrides any crop/hotspot in the image record.

format( $name )

Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp'

auto( $mode )

Specify transformations to automatically apply based on browser capabilities. Supported values:

  • format - Automatically uses WebP if supported

orientation( $angle )

Rotation in degrees. Acceptable values: 0, 90, 180, 270

quality( $value )

Compression quality, where applicable. 0-100

forceDownload( $defaultFileName )

Make this an url to download the image. Specify the file name that will be suggested to the user.

flipHorizontal(), flipVertical()

Flips the image.

crop( $mode )

Specifies how to crop the image. When specified, overrides any crop or hotspot in the image record. See the documentation for details.

fit( $value )

Configures the fit mode. See the documentation for details.

dpr( $value )

Specifies device pixel ratio scaling factor. From 1 to 3.

saturation( $value )

Adjusts the saturation of the image. Currently the only supported value is -100 - meaning it grayscales the image.

ignoreImageParams()

Ignore any specifications from the image record (i.e. crop and hotspot).

url(), __toString()

Return the url as a string.

pad( $value )

Specify the number of pixels to pad the image.

frame( $value )

Specify the frame of an animated image to transform. Acceptable values:

  • 1 - Returns the first frame of the animated image as a static preview of the image.

Deprecated: minWidth( $pixels ), maxWidth( $pixels ), minHeight( $pixels ), maxHeight( $pixels )

Specifies min/max dimensions when cropping.

Deprecated: You usually want to use width/height with a fit mode of max or min instead.

Custom CDN domains

ℹ️ This feature is available to select Enterprise accounts. Get in touch with your sales executive to learn more.

You can specify a custom baseUrl in the builder options in order to override the default (https://cdn.sanity.io):

$builder = urlBuilder( [
    'baseUrl'   => 'https://my.custom.domain',
    'projectId' => 'abc123',
    'dataset'   => 'production',
] );

echo $builder->image( 'image-928ac96d53b0c9049836c86ff25fd3c009039a16-200x200-png' )
    ->auto( 'format' )
    ->fit( 'max' )
    ->width( 720 )
    ->toString();

// output: https://my.custom.domain/images/abc123/production/928ac96d53b0c9049836c86ff25fd3c009039a16-200x200.png?w=720&fit=max&auto=format

Feature requests

Currently, there are no plans to support features that aren't in the official JavaScript project. Please request new features on the official GitHub project and if accepted and implemented, this library will also implement the feature at a later point.

Issues

Before submitting a new issue, please check if it doesn't exist already. If not, verify if this issue also exists in the official JavaScript project. If the JavaScript code and PHP code behave differently, feel free to report the issue.

Pull requests

Only pull requests addressing differences in behavior between the official JavaScript project and this PHP project will be considered. The PHP code has been ported from the JavaScript code in a near line by line manner, which will make any future updates easier to implement. Changes to the structure or readability will most likely end in slower future updates and should be avoided. So let's try to keep the PHP code and JavaScript code in sync 🤝

License

MIT © Robert Painsi

robertpainsi/sanity-image-url-php 适用场景与选型建议

robertpainsi/sanity-image-url-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.38k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 robertpainsi/sanity-image-url-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-14