GithubHelp home page GithubHelp logo

mengene-php's Introduction

Mengene PHP

Mengene PHP is a PHP library for Mengene API.

Installation

You can install the library via Composer. Run the following command:

$ composer require mengene-io/mengene-php

Then require the vendor/autoload.php file to enable auto-loading provided by Composer.

Usage

Processing Local Image

To process an image file on your local machine use setLocalImage() method. This will upload the file to Mengene API service.

<?php
use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setFile('sample.jpg');
$result = $client->process($options);

Result array will contain information about optimization process and also download url of the optimized image.

{
    "success": true,
    "source_name": "sample.png",
    "source_size": 5667,
    "options": {
        "compression_level": "high",
        "optimization_mode": "lossy",
        "quality": null,
        "sampling_scheme": null
    },
    "output_size": 2086,
    "output_saving": 3581,
    "output_ratio": 0.36809599435327,
    "output_percentage": 63.190400564673,
    "output_url": "https://download.mengene.io/03c3d602d5643ed484e282ee76910cce.png"
}

You can use $result['output_url'] to download optimized image manually. There is also download() helper method to make it downloading easy. Also note that, optimized images are available for download for only 1 hour.

Processing Remote Image

To process an image file from remote location you can use setRemoteImage() method. Mengene API service will download the file from given url to make it ready for processing. The url must be publicly accessible.

<?php
use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setUrl('http://example.com/sample.png');
$result = $client->process($options);

Download Processed Image

$result['output_url'] gives download url of the optimized image. PHP library provides simple download() helper method. Note that, optimized images are available for download for only 1 hour.

use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setFile('sample.jpg');
$result = $client->process($options);
$client->download($result, 'path/to/processed-image.png');

Getting User Status

<?php
use Mengene\Client;

$client = new Client('your-api-key');
$client->status();

Options

Compression Level

Default compression level is high if not specified. Available compression levels are:

  • ImageOptions::COMPRESSION_HIGH slow but the smaller file size
  • ImageOptions::COMPRESSION_MEDIUM
  • ImageOptions::COMPRESSION_LOW fast but the larger file size
<?php
use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setFile('sample.jpg')
    ->setCompressionLevel(ImageOptions::COMPRESSION_LOW);
$result = $client->process($options);

Optimization Mode

Default optimization mode is lossy if not specified. Available optimization modes are:

  • ImageOptions::OPTIMIZATION_LOSSY for smaller file size
  • ImageOptions::OPTIMIZATION_LOSSLESS
<?php
use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setFile('sample.jpg')
    ->setOptimizationMode(ImageOptions::OPTIMIZATION_LOSSLESS);
$response = $client->process($options);

Quality

Only available if the image file is JPEG and lossy optimization mode is used. Default quality level is automatically calculated by using input image file quality. Quality must be between 1 and 100.

<?php
use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setFile('sample.jpg')
    ->setQuality(80);
$response = $client->process($options);

Chroma Sub-Sampling

Only available if the image file is JPEG and lossy optimization mode is used. Default sampling scheme is 4:2:0. Available sampling schemes are:

  • ImageOptions::SAMPLING_SCHEME_444
  • ImageOptions::SAMPLING_SCHEME_422
  • ImageOptions::SAMPLING_SCHEME_420

For more information about chroma sub-sampling, see Wikipedia article about it.

<?php
use Mengene\Client;
use Mengene\ImageOptions;

$client = new Client('your-api-key');
$options = (new ImageOptions())
    ->setFile('sample.jpg')
    ->setSamplingScheme(ImageOptions::SAMPLING_SCHEME_444);
$response = $client->process($options);

Author

Hidayet Doğan - [email protected] - https://twitter.com/hdogan

License

Mengene PHP is licensed under the MIT license. See the LICENSE file for more details.

mengene-php's People

Contributors

hdogan avatar hkulekci avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.