How to use jpegtran with composer

hi all

i just installed composer on xampp

i want to use jpegtran from optimizing images

so i downloaded a library folder from github

This is their sample.php for optimizing png file

<?php
//include('./vendor/autoload.php');

$advPng = new \PHPImageOptim\Tools\Png\AdvPng();
$advPng->setBinaryPath('/usr/local/bin/advpng');

$optiPng = new \PHPImageOptim\Tools\Png\OptiPng();
$optiPng->setBinaryPath('/usr/local/bin/optipng');

$pngOut = new \PHPImageOptim\Tools\Png\PngOut();
$pngOut->setBinaryPath('/usr/local/bin/pngout');

$pngCrush = new \PHPImageOptim\Tools\Png\PngCrush();
$pngCrush->setBinaryPath('/usr/local/bin/pngcrush');

$pngQuant = new \PHPImageOptim\Tools\Png\PngQuant();
$pngQuant->setBinaryPath('/usr/local/bin/pngquant');

$optim = new \PHPImageOptim\PHPImageOptim();
$optim->setImage('./tests/image/lenna-original.png');
$optim->chainCommand($pngQuant)
    ->chainCommand($advPng)
    ->chainCommand($optiPng)
    ->chainCommand($pngCrush)
    ->chainCommand($pngOut);
$optim->optimise();
?>

But there is no vendor folder and autoload file.

so i commented that line and tried with composer

there is composer.json file

{
    "name": "bensquire/php-image-optim",
    "type": "library",
    "description": "A library to aid in image optimisation",
    "keywords": ["image", "php", "optimisation", "png", "jpg", "gif"],
    "homepage": "https://github.com/bensquire/php-image-optim",
    "license": "MIT",
    "authors": [
        {
            "name": "Ben Squire",
            "email": "b.squire@gmail.com",
            "homepage": "https://github.com/bensquire/php-image-optim"
        }
    ],
    "support": {
        "issues": "https://github.com/bensquire/php-image-optim/issues",
        "wiki": "https://github.com/bensquire/php-image-optim/wiki"
    },
    "require": {
        "php": ">=5.6.0"
    },
    "require-dev": {
        "phpunit/phpunit": "5.*"
    },
    "autoload": {
        "psr-0": {
            "PHPImageOptim": "src/"
        }
    }
}

But i get this below error

Fatal error:  Class 'PHPImageOptim\Tools\Png\AdvPng' not found in E:\xampp\htdocs\myapp\jpegtran\php-image-optim-master\php-image-optim-master\sample.php on line 4

what should i do ??

vineet

you need to install your dependencies before you can use them.

hi Dormilich

in the composer.json its written

"require": {
        "php": ">=5.6.0"
    },
    "require-dev": {
        "phpunit/phpunit": "5.*"
    },

first one means php 5.6 is required

what is second one “phpunit” ?? do i have to install it separately ??

or “phpunit” will get install with running composer ??

vineet

I am getting this error. dont know why

Fatal error:  Uncaught exception 'Exception' with message 'Unable
 to locate binary file' in 
E:\xampp\htdocs\myapp\phpimageoptim53\src\PHPImageOptim\Tools\Common.php:26
Stack  trace:
#0 E:\xampp\htdocs\myapp\phpimageoptim53\sample.php(8): 
PHPImageOptim\Tools\Common->setBinaryPath('/usr/local/bin/...')
#1 {main}
  thrown in E:\xampp\htdocs\myapp\phpimageoptim53\src\PHPImageOptim\Tools\Common.php on line 26

this is common.php

<?php

namespace PHPImageOptim\Tools;
use Exception;

class Common
{
    protected $binaryPath = '';
    protected $imagePath = '';
    protected $outputPath = '';
    protected $originalFileSize = '';
    protected $finalFileSize = '';
    protected $optimisationLevel = 1;

    /**
     * Sets the path of the executable
     *
     * @param string $binaryPath
     * @return $this
     * @throws Exception
     */
    public function setBinaryPath($binaryPath = '')
    {
        if (!file_exists($binaryPath))
        {
            throw new Exception('Unable to locate binary file');
        }

        $this->binaryPath = $binaryPath;
        return $this;
    }

    /**
     * Sets the path of the image
     *
     * @param $imagePath
     * @return $this
     * @throws Exception
     */
    public function setImagePath($imagePath)
    {
        if (!file_exists($imagePath))
        {
            throw new Exception('Invald image path');
        }

        if (!is_readable($imagePath))
        {
            throw new Exception('The file cannot be read');
        }

        $this->imagePath = $imagePath;
        return $this;
    }

    /**
     * Sets the desired level of optimisation.
     *
     * @param int $level
     * @return $this
     * @throws \Exception
     */
    public function setOptimisationLevel($level = 2)
    {
        if (!is_int($level))
        {
            throw new Exception('Invalid Optimisation Level');
        }

        if ($level !== ToolsInterface::OPTIMISATION_LEVEL_BASIC &&
            $level !== ToolsInterface::OPTIMISATION_LEVEL_STANDARD &&
            $level !== ToolsInterface::OPTIMISATION_LEVEL_EXTREME
        )
        {
            throw new Exception('Invalid Optimisation level');
        }

        $this->optimisationLevel = $level;
        return $this;
    }

    /**
     * Calculates and stores the pre-optimised fileSize
     *
     * @return $this
     */
    public function determinePreOptimisedFileSize()
    {
        $this->originalFileSize = filesize($this->imagePath);
        return $this;
    }

    /**
     * Calculates and stores the post-optimised fileSize
     *
     * @return $this
     */
    public function determinePostOptimisedFileSize()
    {
        $this->finalFileSize = filesize($this->imagePath);
        return $this;
    }
}

this is sample.php

<?php
include('vendor/autoload.php');

$advPng = new \PHPImageOptim\Tools\Png\AdvPng();
$advPng->setBinaryPath('/usr/local/bin/advpng');

$optiPng = new \PHPImageOptim\Tools\Png\OptiPng();
$optiPng->setBinaryPath('/usr/local/bin/optipng');

$pngOut = new \PHPImageOptim\Tools\Png\PngOut();
$pngOut->setBinaryPath('/usr/local/bin/pngout');

$pngCrush = new \PHPImageOptim\Tools\Png\PngCrush();
$pngCrush->setBinaryPath('/usr/local/bin/pngcrush');

$pngQuant = new \PHPImageOptim\Tools\Png\PngQuant();
$pngQuant->setBinaryPath('/usr/local/bin/pngquant');

$optim = new \PHPImageOptim\PHPImageOptim();
$optim->setImage('/tests/image/lenna-original.png');
$optim->chainCommand($pngQuant)
    ->chainCommand($advPng)
    ->chainCommand($optiPng)
    ->chainCommand($pngCrush)
    ->chainCommand($pngOut);
$optim->optimise();
?>

it’s a framework for unit tests.

you do not install dependencies, composer does. (at most you define dependencies).

I am getting this error. dont know why

you’re on Windows. the path used is for Linux/Unix.

and since you’re likely to ask, advpng, optipng, pngout, pngcrush, and pngquant are command line tools you would have to install first.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.