PHP code example of robertpainsi / sanity-image-url-php

1. Go to this page and download the library: Download robertpainsi/sanity-image-url-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

robertpainsi / sanity-image-url-php example snippets




use function SanityImageUrl\urlBuilder;

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

function urlFor( $source ) {
    global $builder;

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

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

// author.php
 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 = 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

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

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

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