PHP code example of mattsplat / fastresize-php

1. Go to this page and download the library: Download mattsplat/fastresize-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/ */

    

mattsplat / fastresize-php example snippets


use Fastresize\FastResize;

$bytes = file_get_contents('background.png');
[$width, $height] = FastResize::probeDimensions($bytes); // header only, no decode

$bg = FastResize::decode($bytes);
$fg = FastResize::decode(file_get_contents('foreground.png'));

// crop the decoded foreground to its non-transparent region before resizing,
// so a mostly-transparent asset only costs its drawing to cache/composite.
[$ox, $oy, $ow, $oh] = FastResize::opaqueRect($fg);
$fgCropped = FastResize::crop($fg, $ox, $oy, $ow, $oh);
$fgSmall = FastResize::resizeNearest($fgCropped, 200, 200);

// solid white canvas so a 3-channel encode has an honest background
$canvas = FastResize::newCanvas($bg->width(), $bg->height(), 255, 255, 255, 255);
FastResize::compositeOver($canvas, $bg, 0, 0);
FastResize::compositeOver($canvas, $fgSmall, 40, 40);

// encodePngRgb flattens transparency onto the background and drops the alpha
// channel - a 3-channel PNG a downstream decoder (e.g. a PDF) can take fast.
file_put_contents('out.png', FastResize::encodePngRgb($canvas));
// ...or FastResize::encodePng($canvas) for a 4-channel RGBA PNG.

// FRImage handles are native heap memory, not PHP-GC'd - free every one
// you allocated, exactly once.
FastResize::free($bg);
FastResize::free($fg);
FastResize::free($fgCropped);
FastResize::free($fgSmall);
FastResize::free($canvas);