PHP code example of storyblok / php-image-service

1. Go to this page and download the library: Download storyblok/php-image-service 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/ */

    

storyblok / php-image-service example snippets


use Storyblok\ImageService\Image;

$image = new Image('https://a.storyblok.com/f/287488/1400x900/2fc896c892/image.jpg');

// Chain multiple operations
$url = $image
    ->resize(800, 600)
    ->format('webp')
    ->quality(80)
    ->toString();

use Storyblok\ImageService\Image;

$image = new Image('https://a.storyblok.com/f/287488/1400x900/2fc896c892/image.jpg');

// Chain as many operations as needed
$url = $image
    ->crop(100, 50, 800, 600)
    ->resize(400, 300)
    ->flipX()
    ->rotate(90)
    ->brightness(10)
    ->quality(80)
    ->format('webp')
    ->grayscale()
    ->toString();

// The original image remains unchanged (immutability)
$original = new Image('https://a.storyblok.com/f/287488/1400x900/2fc896c892/image.jpg');
$resized = $original->resize(800, 600);
$withQuality = $resized->quality(80);

// Each variable holds a different state:
// $original    -> original URL
// $resized     -> resized URL
// $withQuality -> resized + quality URL

use Storyblok\ImageService\Image;

$image = new Image('https://a.storyblok.com/f/287488/1400x900/2fc896c892/image.jpg');

// Resize to specific dimensions
$image->resize(800, 600);

// Resize by width only (height auto-calculated)
$image->resize(800, 0);

// Resize by height only (width auto-calculated)
$image->resize(0, 600);

$image->fitIn(800, 600);

// Crop from position (100, 100) to (500, 400)
$image->crop(100, 100, 500, 400);

// Crop from top-left (0, 0) to specific point
$image->crop(0, 0, 500, 400);

$image->format('webp');
$image->format('jpeg');
$image->format('png');
$image->format('avif');

$image->quality(80);

// Apply blur with radius only
$image->blur(10);

// Apply blur with radius and sigma
$image->blur(10, 5);

// Increase brightness
$image->brightness(50);

// Decrease brightness
$image->brightness(-30);

$image->rotate(90);
$image->rotate(180);
$image->rotate(270);

// Flip horizontally
$image->flipX();

// Flip vertically
$image->flipY();

// Flip both
$image->flipX()->flipY();

$image->grayscale();

// Set focal point coordinates
$image->focalPoint('100x100:300x300');

// Use value from Storyblok asset focus field
$image->focalPoint('719x153:720x154');

// Simple rounded corners with radius
$image->roundedCorners(20);

// With ellipsis for elliptical corners
$image->roundedCorners(20, 10);

// With custom background color (RGB)
$image->roundedCorners(20, null, 255, 0, 0);

// With transparent background
$image->roundedCorners(20, null, 255, 255, 255, true);

// Fill with hex color
$image->fitIn(800, 600)->fill('#FF0000');
$image->fitIn(800, 600)->fill('FF0000');
$image->fitIn(800, 600)->fill('#F00');

// Fill with transparent
$image->fitIn(800, 600)->fill('transparent');

$image->resize(2000, 2000)->noUpscale();

$image = new Image('https://a.storyblok.com/f/287488/1400x900/2fc896c892/my-image.jpg');

// Get dimensions
$image->getWidth();     // 1400
$image->getHeight();    // 900

// Get file info
$image->getName();      // "my-image"
$image->getExtension(); // "jpg"

// Extension updates when format changes
$formatted = $image->format('webp');
$formatted->getExtension(); // "webp"
bash
composer