PHPackages                             robertpainsi/sanity-image-url-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. robertpainsi/sanity-image-url-php

ActiveLibrary[Image &amp; Media](/categories/media)

robertpainsi/sanity-image-url-php
=================================

Tools to generate image urls from Sanity content in PHP

v1.0.0(1y ago)41.5k↑164.3%[1 issues](https://github.com/robertpainsi/sanity-image-url-php/issues)MITPHPPHP &gt;=7.4

Since Sep 15Pushed 1y ago1 watchersCompare

[ Source](https://github.com/robertpainsi/sanity-image-url-php)[ Packagist](https://packagist.org/packages/robertpainsi/sanity-image-url-php)[ RSS](/packages/robertpainsi-sanity-image-url-php/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

PHP port of `@sanity/image-url`
===============================

[](#php-port-of-sanityimage-url)

[![Packagist](https://camo.githubusercontent.com/8dd92b6bbb2ab2019e1b92ac4e399c7da93ebf8371651079741520b19aecf931/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f626572747061696e73692f73616e6974792d696d6167652d75726c2d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robertpainsi/sanity-image-url-php)[![Build Status](https://camo.githubusercontent.com/03c53a8991d4b7ffea38a7d29120a2800de507bccf4739d11c83af750a561d4b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f626572747061696e73692f73616e6974792d696d6167652d75726c2d7068702f63692e796d6c3f7374796c653d666c61742d737175617265266272616e63683d6d61696e)](https://github.com/robertpainsi/sanity-image-url-php/actions)

This PHP library is a direct port of the official Sanity JavaScript library [@sanity/image-url](https://github.com/sanity-io/image-url). Version maps to [@sanity/image-url @fff9fc1](https://github.com/sanity-io/image-url/commit/fff9fc1b77f334be195a65394bed71aeffa8f3bb) 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
------------

[](#requirements)

sanity-image-url-php requires PHP &gt;= 7.4.

Composer
--------

[](#composer)

You can install the library via [Composer](https://getcomposer.org/). Run the following command:

```
composer require robertpainsi/sanity-image-url-php
```

To use the library, use Composer's [autoload](https://getcomposer.org/doc/00-intro.md#autoloading):

```
require_once 'vendor/autoload.php';
```

Usage
-----

[](#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](https://github.com/sanity-io/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:

```
