From 43ef038fdf6424f8dfc9a4c22a27489812431c2f Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Fri, 4 Apr 2025 14:52:05 +0200 Subject: [PATCH 1/6] Set minimum TLS version to v1.2 --- lib/Tinify/Client.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Tinify/Client.php b/lib/Tinify/Client.php index 4dc820c..531b830 100644 --- a/lib/Tinify/Client.php +++ b/lib/Tinify/Client.php @@ -31,6 +31,11 @@ function __construct($key, $app_identifier = NULL, $proxy = NULL) { throw new ClientException("Your curl version {$version} is outdated; please upgrade to 7.18.1 or higher"); } + # Set minimum TLS version to 1.2, CURL_SSLVERSION_TLSv1_2 is not available in curl < 7.34.0 + # Additionally old PHP versions may not support this constant + $tlsVersion = ($curl["version_number"] < 0x072200) + ? 6 + : (defined('CURL_SSLVERSION_TLSv1_2') ? CURL_SSLVERSION_TLSv1_2 : 6); $this->options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, @@ -38,6 +43,7 @@ function __construct($key, $app_identifier = NULL, $proxy = NULL) { CURLOPT_CAINFO => self::caBundle(), CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => join(" ", array_filter(array(self::userAgent(), $app_identifier))), + CURLOPT_SSLVERSION => $tlsVersion, ); if ($proxy) { From aee1582a9b8874ca5ecead0aa54b294058c464df Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Mon, 7 Apr 2025 13:02:50 +0200 Subject: [PATCH 2/6] Update changelog --- CHANGES.md | 3 +++ lib/Tinify.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index fec117f..b72e531 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## 1.6.3 +* Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky. + ## 1.6.2 * Remove deprecated curl constant (https://php.watch/versions/8.4/CURLOPT_BINARYTRANSFER-deprecated) diff --git a/lib/Tinify.php b/lib/Tinify.php index e2a970f..9e34876 100644 --- a/lib/Tinify.php +++ b/lib/Tinify.php @@ -2,7 +2,7 @@ namespace Tinify; -const VERSION = "1.6.2"; +const VERSION = "1.6.3"; class Tinify { private static $key = NULL; From 0b09830f3bce3ff9298fb1d33fa10a485ca72fc0 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Wed, 10 Dec 2025 05:43:48 -0500 Subject: [PATCH 3/6] Php 8.5 Support (#42) * Avoid depreciation notice in PHP 8.5 * Add PHP 8.5 to tests * Remove dynamic property depreciation notice * Anticipate V1.6.4 --- .github/workflows/ci-cd.yaml | 2 +- lib/Tinify.php | 2 +- lib/Tinify/Client.php | 8 ++++++-- test/curl_mock.php | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci-cd.yaml b/.github/workflows/ci-cd.yaml index d0ba89f..214caa1 100644 --- a/.github/workflows/ci-cd.yaml +++ b/.github/workflows/ci-cd.yaml @@ -16,7 +16,7 @@ jobs: "5.6", "7.4", "8.0", - "8.4", + "8.5", ] os: [ubuntu-latest, macOS-latest, windows-latest] steps: diff --git a/lib/Tinify.php b/lib/Tinify.php index 9e34876..80f533f 100644 --- a/lib/Tinify.php +++ b/lib/Tinify.php @@ -2,7 +2,7 @@ namespace Tinify; -const VERSION = "1.6.3"; +const VERSION = "1.6.4"; class Tinify { private static $key = NULL; diff --git a/lib/Tinify/Client.php b/lib/Tinify/Client.php index 531b830..93bd513 100644 --- a/lib/Tinify/Client.php +++ b/lib/Tinify/Client.php @@ -112,7 +112,9 @@ function request($method, $url, $body = NULL) { if (is_string($response)) { $status = curl_getinfo($request, CURLINFO_HTTP_CODE); $headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE); - curl_close($request); + if (PHP_VERSION_ID < 80000) { + curl_close($request); + } $headers = self::parseHeaders(substr($response, 0, $headerSize)); $responseBody = substr($response, $headerSize); @@ -140,7 +142,9 @@ function request($method, $url, $body = NULL) { throw Exception::create($details->message, $details->error, $status); } else { $message = sprintf("%s (#%d)", curl_error($request), curl_errno($request)); - curl_close($request); + if (PHP_VERSION_ID < 80000) { + curl_close($request); + } if ($retries > 0) continue; throw new ConnectionException("Error while connecting: " . $message); } diff --git a/test/curl_mock.php b/test/curl_mock.php index 9008924..8fe6a00 100644 --- a/test/curl_mock.php +++ b/test/curl_mock.php @@ -17,6 +17,7 @@ class CurlMock { private static $version = array(); public $options = array(); + public $request; public $response; public $closed = false; From 763414d42a42a4882c7d550d7dcd746e20fac3f7 Mon Sep 17 00:00:00 2001 From: Tijmen Date: Wed, 10 Dec 2025 11:50:56 +0100 Subject: [PATCH 4/6] Use HTTP Post when retrieving result (#41) * Use http post when retrieving result * 1.6.4 and changelog * When commands is empty, do a get with body null * Change condition to use empty * fix: correct HTTP method logic for Source result requests - Fix inverted boolean logic for has_commands check - Ensure GET is used when no commands are present - Ensure POST is used when commands are present - Add comprehensive tests for both GET and POST scenarios --- CHANGES.md | 3 +++ lib/Tinify/Source.php | 5 ++++- test/TinifySourceTest.php | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index b72e531..ad87373 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## 1.6.4 +* Will use POST instead of GET when retrieving result. + ## 1.6.3 * Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky. diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 304040a..ef7c6a1 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,7 +53,10 @@ public function transform($options) { } public function result() { - $response = Tinify::getClient()->request("get", $this->url, $this->commands); + $has_commands = !empty($this->commands); + $method = $has_commands ? "post" : "get"; + $body = $has_commands ? $this->commands : null; + $response = Tinify::getClient()->request($method, $this->url, $body); return new Result($response->headers, $response->body); } diff --git a/test/TinifySourceTest.php b/test/TinifySourceTest.php index 80c5fd8..de4584d 100644 --- a/test/TinifySourceTest.php +++ b/test/TinifySourceTest.php @@ -132,6 +132,31 @@ public function testResultShouldReturnResult() { )); $this->assertInstanceOf("Tinify\Result", Tinify\Source::fromBuffer("png file")->result()); + $this->assertSame("GET", CurlMock::last(CURLOPT_CUSTOMREQUEST)); + } + + /** + * When request does not contain commands, it should use method GET + * when it contains commands, it should have a body and method POST + */ + public function testResultWithCommandsShouldReturnResultUsingPost() { + Tinify\setKey("valid"); + + CurlMock::register("https://api.tinify.com/shrink", array( + "status" => 201, + "headers" => array("Location" => "https://api.tinify.com/some/location"), + )); + + CurlMock::register("https://api.tinify.com/some/location", array( + "status" => 200, "body" => "resized file" + )); + + $source = Tinify\Source::fromBuffer("png file")->resize(array("width" => 400)); + $result = $source->result(); + + $this->assertInstanceOf("Tinify\Result", $result); + $this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST)); + $this->assertSame('{"resize":{"width":400}}', CurlMock::last(CURLOPT_POSTFIELDS)); } public function testPreserveShouldReturnSource() { @@ -162,6 +187,7 @@ public function testPreserveShouldReturnSourceWithData() { $this->assertSame("copyrighted file", Tinify\Source::fromBuffer("png file")->preserve("copyright", "location")->toBuffer()); $this->assertSame("{\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS)); + $this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST)); } public function testPreserveShouldReturnSourceWithDataForArray() { From ebab928447850d0ceb7f8195ed9bf5609415b0a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Mari=C4=87?= Date: Wed, 10 Dec 2025 11:54:24 +0100 Subject: [PATCH 5/6] docs(changelog): add missing changes to `1.6.4` --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index ad87373..7a0c177 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,7 @@ ## 1.6.4 * Will use POST instead of GET when retrieving result. +* Remove deprecated `curl_close` for PHP version 8. https://www.php.net/manual/en/function.curl-close.php. +* Support PHP `8.5`. ## 1.6.3 * Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky. From 9b2085ba41f2eb7419dc372606917e82d304abfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Mari=C4=87?= Date: Wed, 10 Dec 2025 12:14:08 +0100 Subject: [PATCH 6/6] fix(client): explicitly unset connection handler to free connection when PHP>8.5 --- lib/Tinify/Client.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Tinify/Client.php b/lib/Tinify/Client.php index 93bd513..0d8ed0c 100644 --- a/lib/Tinify/Client.php +++ b/lib/Tinify/Client.php @@ -114,6 +114,8 @@ function request($method, $url, $body = NULL) { $headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE); if (PHP_VERSION_ID < 80000) { curl_close($request); + } else { + unset($request); } $headers = self::parseHeaders(substr($response, 0, $headerSize)); @@ -144,6 +146,8 @@ function request($method, $url, $body = NULL) { $message = sprintf("%s (#%d)", curl_error($request), curl_errno($request)); if (PHP_VERSION_ID < 80000) { curl_close($request); + } else { + unset($request); } if ($retries > 0) continue; throw new ConnectionException("Error while connecting: " . $message);