PHP code example of codeinc / watermarker-client

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

    

codeinc / watermarker-client example snippets


use CodeInc\WatermarkerClient\WatermarkerClient;
use CodeInc\WatermarkerClient\Exception;

$apiBaseUri = 'http://localhost:3000/';
$anImage = '/path/to/local/image.png';
$theWatermark = '/path/to/local/watermark.png';

try {
    $client = new WatermarkerClient($apiBaseUri);

    // apply the watermark
    $watermarkedImageStream = $client->apply(
        $client->createStreamFromFile($anImage),
        $client->createStreamFromFile($theWatermark),
    );
    
    // display the watermarked image
    header('Content-Type: image/png');
    echo (string)$watermarkedImageStream;
}
catch (Exception $e) {
    // handle exception
}

use CodeInc\WatermarkerClient\WatermarkerClient;
use CodeInc\WatermarkerClient\ConvertOptions;
use CodeInc\WatermarkerClient\Position;
use CodeInc\WatermarkerClient\Format;

$apiBaseUri = 'http://localhost:3000/';
$theImageStream = '/path/to/local/image.png';
$theWatermarkStream = '/path/to/local/watermark.png';
$theDestinationFile = '/path/to/local/destination.png';
$convertOption = new ConvertOptions(
    size: 50,
    position: Position::topRight,
    format: Format::jpg,
    quality: 80,
    blur: 3,
    opacity: 75
);

try {
    $streamFactory = Psr17FactoryDiscovery::findStreamFactory();
    $client = new WatermarkerClient($apiBaseUri);

    // apply the watermark
    $watermarkedImageStream = $client->apply(
        $client->createStreamFromFile($theImageStream),
        $client->createStreamFromFile($theWatermarkStream),
        $convertOption
    );
    
    // save the watermarked image
    $client->saveStreamToFile($watermarkedImageStream, $theDestinationFile);
}
catch (Exception $e) {
    // handle exception
}