Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Latest commit

 

History

History
93 lines (72 loc) · 2.93 KB

README.md

File metadata and controls

93 lines (72 loc) · 2.93 KB

Watermarker PHP client

Code Inc. Tests

Important

This client is deprecated and is replaced by the Document Cloud PHP Client

This repository contains a PHP 8.2+ client library for watermarking images using the watermarker service.

Installation

The library is available on Packagist. The recommended way to install it is via Composer:

composer require codeinc/watermarker-client

Usage

This client requires a running instance of the watermarker service. The service can be run locally using Docker or deployed to a server.

Examples

A simple scenario to apply a watermark to an image and display the result:

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
}

A mire complex scenario to apply a watermark to an image with options and save the result to a file:

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
}

License

The library is published under the MIT license (see LICENSE file).