tinify-php
tinify-php copied to clipboard
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
In order to try to reproduce the issue, could you provide an overview of your setup of php including which versions? Does the problem always appear with specific images or certain size?
It happens on images around 800kb, but not all images (attached a sample). PHP version:
PHP 7.3.14 (cli) (built: Jan 24 2020 03:04:31) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.14, Copyright (c) 1998-2018 Zend Technologies with Xdebug v2.9.1, Copyright (c) 2002-2020, by Derick Rethans with Zend OPcache v7.3.14, Copyright (c) 1999-2018, by Zend Technologies
This is my script:
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
$inputFile = $_SERVER['argv'][1] ?? null;
function tinyjpg(string $inputFile, string $outputFile) {
echo 'Processing: ' . $inputFile . ' ' . number_format(filesize($inputFile)) . PHP_EOL;
\Tinify\setKey('MYAPIKEY');
$source = \Tinify\fromFile($inputFile);
$output = $source->toBuffer();
file_put_contents($outputFile, $output);
clearstatcache();
echo 'Output: ' . number_format(filesize($outputFile)) . PHP_EOL;
}
if (!is_file($inputFile)) {
if ($inputFile === null) {
foreach (glob('*.jpg') as $file) {
tinyjpg($file, $file);
}
return;
}
die('Could not find file: ' . $inputFile . PHP_EOL);
}
tinyjpg($inputFile, $_SERVER['argv'][2] ?? $inputFile);
Thanks for the info. I wasn't able to reproduce the problem with the information above, which curl version are you using?
We were able to reproduce the issue and it is random when this error occurs. If you even retry the same image it could pass with no errors. We are not quite sure why, we are still working on a solution. The issue is only on http2. If you switch to http1 you can avoid this from happening. You could potentially fork the repository and make the change to http1 locally.
I am closing this issue as it is quite old. Please reopen in case you still notice errors related to HTTP2.