PHP code example of acetonesoft / acetone-api-php

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

    

acetonesoft / acetone-api-php example snippets


use AcetoneSoft\Acetone\AcetoneApi;

$acetone = new AcetoneApi($apiKey);
$acetone->fromFile($sourceImageFile)->save($targetImageFile);

// Исходное изображение по URL
$acetone->fromUrl($imageUrl)->save($outFile);

// Исходное изображение из файла
$acetone->fromFile($imageFile)->save($outFile);

// Изображение из бинарной строки
$acetone->fromString($imageString)->save($outFile);

// Изображение из base64-строки
$acetone->fromBase64($base64)->save($outFile);

// Просто удалить фон
$acetone->fromFile($imageFile)->bgRemove()->save($outFile);

// Задать цвет фона (hex с "#" или без, короткий hex либо RGB-массив)
$acetone->fromFile($imageFile)->bgColor('f00')->save($outFile);
$acetone->fromFile($imageFile)->bgColor('#f00')->save($outFile);
$acetone->fromFile($imageFile)->bgColor([255, 0, 0])->save($outFile);

// Залить фон линейным градиентом (второй аргумент — угол вектора градиента)
$colors = ['f00', '33c'];
$vector = -30;
$acetone->fromFile($imageFile)->bgGradient($colors, $vector)->save($outFile);

// Залить фон радиальным градиентом (второй аргумент — центр [x, y])
$colors = ['f00', '33c'];
$center = [120, 240];
$acetone->fromFile($imageFile)->bgRadialGradient($colors, $center)->save($outFile);

// Размыть фон (степень размытия)
$acetone->fromFile($imageFile)->bgBlur(20)->save($outFile);

// Обесцветить фон
$acetone->fromFile($imageFile)->bgGrayscale()->save($outFile);

// Задать фоновое изображение из бинарной строки
$bgImage = file_get_contents('path/to/new/background');
$acetone->fromFile($imageFile)->bgImage($bgImage)->save($outFile);

// Либо задать фоновое изображение из файла
$acetone->fromFile($imageFile)->bgImageFile('path/to/new/background')->save($outFile);

$acetone->fromFile($imageFile)
    ->size(800, 600, AcetoneApi::IMG_FIT_COVER, AcetoneApi::IMG_FIT_COVER)
    ->bgImageFile($imageFileBg)
    ->save($outFile);

$acetone->fromFile($imageFile)
    ->crop()
    ->get();

// Задать качество (1-100) и включить режим точного вырезания
$acetone->fromFile($imageFile)
    ->quality(90)
    ->exact(true)
    ->save($outFile);

// Добавить падающую тень под объектом
// shadow(power, offsetX, offsetY, color) — цвет должен быть в hex-формате
$acetone->fromFile($imageFile)
    ->shadow(50, 15, 15, '#999999')
    ->save($outFile);

// Наложить логотип из файла с «сырыми» опциями logo_*
$acetone->fromFile($imageFile)
    ->logoImageFile($logoFile, ['logo_position' => 0, 'logo_size' => 10, 'logo_opacity' => 1])
    ->save($outFile);

// Логотип из бинарной строки
$acetone->fromFile($imageFile)
    ->logoImage($logoBinary)
    ->save($outFile);

// Fluent-стиль — цвет заливки должен быть в hex-формате
$acetone->fromFile($imageFile)
    ->maskFile($maskFile)
    ->objectBgColor('#ffffff')
    ->saveObject($outFile);

// Маску можно передать и бинарной строкой
$acetone->fromFile($imageFile)
    ->mask(file_get_contents($maskFile))
    ->getObject('png');

// One-shot стиль (принимает бинарные строки)
$result = $acetone->objectRemove(
    file_get_contents($imageFile),
    file_get_contents($maskFile)
);

// Fluent-стиль
$acetone->fromFile($imageFile)
    ->enhanceMode('solo')
    ->saveEnhanced($outFile);

// С целевым изображением (из файла или бинарной строки)
$acetone->fromFile($imageFile)
    ->targetImageFile($targetFile)
    ->saveEnhanced($outFile);

// One-shot стиль
$result = $acetone->enhanceImage(file_get_contents($imageFile));

// Получить результат бинарной строкой
$imageStr = $acetone->fromFile($imageFile)->getEnhanced('png');

// Можно указать формат вывода — png, jpg или webp (по умолчанию png)
$imageStr = $acetone->fromFile($imageFile)->get('webp');
$im = imagecreatefromstring($imageStr);
// Какие-то манипуляции
imagejpeg($im, 'image.jpg');

$acetone->fromFile($imageFile)
    ->options(['some_api_param' => 'value'])
    ->save($outFile);

$acetone->fromFile($imageFile)->save($outFile);
echo $acetone->getTime(); // например, 1.234

$src = file_get_contents($imageFile);

$acetone->backgroundRemove($src);
$acetone->backgroundColor($src, '#ff0000');
$acetone->backgroundGradient($src, ['f00', '33c'], -30);        // линейный, угол вектора
$acetone->backgroundRadialGradient($src, ['f00', '33c'], [120, 240]); // радиальный, центр
$acetone->backgroundBlur($src, 20);
$acetone->backgroundGrayscale($src);
$acetone->backgroundImage($src, file_get_contents($bgFile));
$acetone->backgroundReplace($src, file_get_contents($bgFile));  // псевдоним backgroundImage
$acetone->objectRemove($src, file_get_contents($maskFile));
$acetone->enhanceImage($src);

composer