GithubHelp home page GithubHelp logo

tinify / tinify-php Goto Github PK

View Code? Open in Web Editor NEW
241.0 18.0 53.0 531 KB

PHP client for the Tinify API.

Home Page: https://tinypng.com/developers

License: MIT License

PHP 99.31% Shell 0.69%
tinify-api image-compression image-optimization tinypng tinyjpg php

tinify-php's Introduction

MIT License CI_CD Packagist Version Packagist PHP Version Support

Tinify API client for PHP

PHP client for the Tinify API, used for TinyPNG and TinyJPG. Tinify compresses your images intelligently. Read more at http://tinify.com.

Documentation

Go to the documentation for the PHP client.

Installation

Install the API client with Composer. Add this to your composer.json:

{
  "require": {
    "tinify/tinify": "*"
  }
}

Then install with:

composer install

Use autoloading to make the client available in PHP:

require_once("vendor/autoload.php");

Usage

Tinify\setKey("YOUR_API_KEY");
Tinify\fromFile("unoptimized.png")->toFile("optimized.png");

Running tests

composer install
vendor/bin/phpunit

Integration tests

composer install
TINIFY_KEY=$YOUR_API_KEY vendor/bin/phpunit --no-configuration test/integration.php

License

This software is licensed under the MIT License. View the license.

tinify-php's People

Contributors

eddyh avatar michielverkoijen avatar rkoopmans avatar rolftimmermans avatar yektaturan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tinify-php's Issues

PHP 8.2: Deprecated ${} string interpolation.

${var} String Interpolation Deprecated
PHP has always supported string variable interpolation with "foo {$bar}" pattern, and along with an alternative syntax that the dollar sign is placed outside the curly braces (e.g. "foo ${bar}").
In PHP 8.2, the alternative syntax of placing the dollar sign outside the curly braces is deprecated.

Deprecated in PHP 8.2 Recommended syntax for PHP 8.2
Hello ${name}; Hello {$name}
echo "Hello ${$var}"; echo "Hello {$var}";

Source: https://php.watch/versions/8.2

For detailed information see PHP RFC: https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation

Get content type and length with store requests

Hi there,

I noticed, based on the documentation, that a call to ->store() will not return content-length and content-type with the HTTP response. Of course this is related to the fact that binary data is not being returned, so these headers would not perform their normal purpose - is there a chance the API could return this information in some other header for these calls though? as it is, the Tinify\Result object returned by the call to ->store() cannot return meaningful values for the methods: ->size(), ->mediaType() or ->contentType(). Obviously these would need to be modified if the data were somewhere other than the content-length and content-type headers, but right now there is no way, for instance, to tell the compression ratio of an image uploaded directly to S3 without using an extra compression and extra bandwidth to download the whole image directly, just to get the few bits in those headers (Or to download it from Amazon after the call to ->store() succeeds and parse the data from there). Either of these workarounds negate the benefit of being able to store compressed images directly to Amazon though.

S3 bucket name cannot include dot (.)

Hi! Amazing job you've done! I am using this library almost daily :) Now I tried to implement the S3 upload, which is integrated into the PHP API you provide. It works quite well, with one exception - if the bucket includes dot, the upload to S3 fails with 500 HTTP error code. Example:

$source->store([
            'service' => 's3',
            'aws_access_key_id' => $this->awsId,
            'aws_secret_access_key' => $this->awsKey,
            'path' => 'example/file.png'
        ]); // OK

$source->store([
            'service' => 's3',
            'aws_access_key_id' => $this->awsId,
            'aws_secret_access_key' => $this->awsKey,
            'path' => 'example.com/file.png' // bucket name example.com does not work!
        ]); // 500 ERROR

Add padding to resized image

It would be great to be able to pass a top-bottom left-right padding value (by pixels or percent maybe), to be added around the image when resizing it, and also the HEX color value to fill the padded area.

SSL Certificate Error

Hello everyone, hope y'all doing well.
Yesterday, I started to getting this error:

Uncaught PHP Exception Tinify\ConnectionException: "Error while connecting: SSL certificate problem: certificate has expired (#60)" at /var/www/mywebsite/vendor/tinify/tinify/lib/Tinify/Client.php line 140
Level
CRITICAL

Since my project is really old and have a lots of dependencies, I can not run composer update command. Then I checked the difference between latest update-cacert.sh and mine. I replaced my update-cacert.sh with the new one and run the command. But I still got the error. What can I do? Is there any ideas?

Best Regards.

Image Compression not working

I had done below code:

include('../includes/tinify-php-master/lib/Tinify/Exception.php');
include('../includes/tinify-php-master/lib/Tinify/ResultMeta.php');
include('../includes/tinify-php-master/lib/Tinify/Result.php');
include('../includes/tinify-php-master/lib/Tinify/Source.php');
include('../includes/tinify-php-master/lib/Tinify/Client.php');
include('../includes/tinify-php-master/lib/Tinify.php');
\Tinify\setKey("API Key");

$source = \Tinify\fromFile($fromFile);
$source->toFile($uploadPath);

Above code gives me wrong output

The output is not compresses

Resize same image more than once

I'm trying to upload an image and create 3 variations of the same image. One is the main image, a medium thumbnail and a smaller thumbnail. But when I'm trying to generate the second and third image, usually I get the error:

Maximum execution time of 30 seconds exceeded

This is the code I'm trying to use:

$source = \Tinify\fromFile($filePath);

$resize = $source->resize(array(
    "method" => "cover",
    "width"  => 1920,
    "height" => 1080
));
$resize->toFile($savePath);

$thumbM = $source->resize(array(
    "method" => "fit",
    "width"  => 400,
    "height" => 350
));
$thumbM->toFile($saveThumbM);

$thumbS = $source->resize(array(
    "method" => "fit",
    "width"  => 80,
    "height" => 80
));
$thumbS->toFile($saveThumbS);

The only difference from each of the images is the name and size. Is there a way to achieve this result?

tinify API always requires application/json

Tinify master and V1.1 will always result in error:

PHP Fatal error: Uncaught exception 'Tinify\ClientException' wi
th message 'Please use application/json as Content-Type (HTTP 41
5/UnsupportedMediaType)' in C:\php-tiny\vendor
\tinify\tinify\lib\Tinify\Exception.php:18

require_once("vendor/autoload.php");
\Tinify\setKey("dev key"); 
$source = \Tinify\fromFile('C:\file.jpg');
$source->toFile('C:\target_file.jpg');

Failures

Hi,

I use this lib quite a lot to compress images on the CLI. But I get quite a lot of failures like:


PHP Fatal error:  Uncaught Tinify\ConnectionException: Error while connecting: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) (#92) in /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Client.php:140
Stack trace:
#0 /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Source.php(13): Tinify\Client->request('post', 'https://api.tin...', '\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x00\x00...')
#1 /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Source.php(9): Tinify\Source::fromBuffer('\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x00\x00...')
#2 /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify.php(76): Tinify\Source::fromFile('57_basic_large....')
#3 /Users/petah/work/commands/tinyjpg(11): Tinify\fromFile('57_basic_large....')
#4 /Users/petah/work/commands/tinyjpg(22): tinyjpg('57_basic_large....', '57_basic_large....')
#5 {main}
  thrown in /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Client.php on line 140

Fatal error: Uncaught Tinify\ConnectionException: Error while connecting: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) (#92) in /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Client.php on line 140

Tinify\ConnectionException: Error while connecting: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) (#92) in /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Client.php on line 140

Call Stack:
    0.0004     400480   1. {main}() /Users/petah/work/commands/tinyjpg:0
   11.6526     561040   2. tinyjpg() /Users/petah/work/commands/tinyjpg:22
   11.6526     560480   3. Tinify\fromFile() /Users/petah/work/commands/tinyjpg:11
   11.6526     560480   4. Tinify\Source::fromFile() /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify.php:76
   11.6553    1363296   5. Tinify\Source::fromBuffer() /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Source.php:9
   11.6553    1363856   6. Tinify\Client->request() /Users/petah/work/commands/vendor/tinify/tinify/lib/Tinify/Source.php:13

Test

This is clearly a test issue.

Its throwing an error

Fatal error: Uncaught exception 'Tinify\ClientException' with message 'Server responded with 403 (HTTP 400/Source not found)' in *****/tinify-php-master/lib/Tinify/Exception.php on line 18

Tinify\ClientException: Server responded with 403 (HTTP 400/Source not found) in *****/tinify-php-master/lib/Tinify/Exception.php on line 18

PHP Exploit Warning

Our hosting provider scans their servers for known exploits from time to time. warned us about a know exploit and blocked access to the following file within minify-php:

test/integration.php

The reason given is

# Known exploit = [Fingerprint Match (fp)] [PHP RFI Exploit [P2060]]

Did anyone else noticed this as well? Anything we can do? Or can this be updated/fixed in the lib?

Cheers.

lib-curl dependency

Hi there,

tinify/tinify 1.5.2 requires lib-curl >=7.20.0 -> the requested linked library curl has the wrong version installed or is missing from your system, make sure to have the extension providing it.

Does tinify actually require 7.20.0, or could 7.19.0 be run?

Cheers!

Set image headers on S3 upload

Hi there,

I was wondering whether there was a way of setting a header with the image upload to S3? For example, setting a "has-been-tinyfied" header on the image would be handy if using the library for more complex projects

Cheers

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.