From 5e6f233171ea76d46dde1bb1918c08e9b86ca49c Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Wed, 27 Jul 2022 15:34:02 +0200 Subject: [PATCH 1/5] Add transcoding API to client --- lib/Tinify/Source.php | 10 +++++++ test/TinifySourceTest.php | 60 +++++++++++++++++++++++++++++++++++++++ test/integration.php | 11 +++++++ 3 files changed, 81 insertions(+) diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 01c013f..5c20e9c 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -42,6 +42,16 @@ public function store($options) { return new Result($response->headers, $response->body); } + public function transcode($types) { + $commands = array_merge($this->commands, array("type" => $types)); + return new self($this->url, $commands); + } + + public function transform($options) { + $commands = array_merge($this->commands, array("transform" => $options)); + return new self($this->url, $commands); + } + public function result() { $response = Tinify::getClient()->request("get", $this->url, $this->commands); return new Result($response->headers, $response->body); diff --git a/test/TinifySourceTest.php b/test/TinifySourceTest.php index 67c8e07..0833179 100644 --- a/test/TinifySourceTest.php +++ b/test/TinifySourceTest.php @@ -226,6 +226,66 @@ public function testWithValidApiKeyResizeShouldReturnSourceWithData() { $this->assertSame("{\"resize\":{\"width\":400}}", CurlMock::last(CURLOPT_POSTFIELDS)); } + public function testTranscodeShouldReturnSource() { + 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" => "transcoded file" + )); + + $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromBuffer("png file")->transcode("image/webp")); + $this->assertSame("png file", CurlMock::last(CURLOPT_POSTFIELDS)); + } + + public function testTranscodeShouldReturnSourceWithData() { + 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" => "transcoded file" + )); + + $this->assertSame("transcoded file", Tinify\Source::fromBuffer("png file")->transcode("image/webp")->toBuffer()); + $this->assertSame("{\"type\":\"image\/webp\"}", CurlMock::last(CURLOPT_POSTFIELDS)); + } + + public function testTransformShouldReturnSource() { + 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" => "transformed file" + )); + + $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromBuffer("png file")->transform(array("background" => "black"))); + $this->assertSame("png file", CurlMock::last(CURLOPT_POSTFIELDS)); + } + + public function testTransformShouldReturnSourceWithData() { + 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" => "transformd file" + )); + + $this->assertSame("transformd file", Tinify\Source::fromBuffer("png file")->transform(array("background" => "black"))->toBuffer()); + $this->assertSame("{\"transform\":{\"background\":\"black\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); + } + public function testWithValidApiKeyStoreShouldReturnResultMeta() { Tinify\setKey("valid"); diff --git a/test/integration.php b/test/integration.php index bf780bd..7167315 100644 --- a/test/integration.php +++ b/test/integration.php @@ -77,4 +77,15 @@ public function testShouldPreserveMetadata() { $this->assertStringContainsString("\0\0\0\x89", $contents); $this->assertStringContainsString("Copyright Voormedia", $contents); } + + public function testShouldTranscode() { + $path = tempnam(sys_get_temp_dir(), "tinify-php"); + self::$optimized->transcode(["image/webp"])->toFile($path); + + $size = filesize($path); + $contents = fread(fopen($path, "rb"), $size); + + $this->assertEquals(substr($contents, 0, 4), "RIFF"); + $this->assertEquals(substr($contents, 8, 4), "WEBP"); + } } From 420702fff22172206bc591df9f7650f950f4ded2 Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Wed, 27 Jul 2022 15:34:59 +0200 Subject: [PATCH 2/5] Simplify test names --- test/TinifySourceTest.php | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/TinifySourceTest.php b/test/TinifySourceTest.php index 0833179..786bc81 100644 --- a/test/TinifySourceTest.php +++ b/test/TinifySourceTest.php @@ -36,7 +36,7 @@ public function testWithInvalidApiKeyFromUrlShouldThrowAccountException() { Tinify\Source::fromUrl("http://example.com/test.jpg"); } - public function testWithValidApiKeyFromFileShouldReturnSource() { + public function testFromFileShouldReturnSource() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -46,7 +46,7 @@ public function testWithValidApiKeyFromFileShouldReturnSource() { $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromFile(DUMMY_FILE_LOCATION)); } - public function testWithValidApiKeyFromFileShouldReturnSourceWithData() { + public function testFromFileShouldReturnSourceWithData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -60,7 +60,7 @@ public function testWithValidApiKeyFromFileShouldReturnSourceWithData() { $this->assertSame("compressed file", Tinify\Source::fromFile(DUMMY_FILE_LOCATION)->toBuffer()); } - public function testWithValidApiKeyFromBufferShouldReturnSource() { + public function testFromBufferShouldReturnSource() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -70,7 +70,7 @@ public function testWithValidApiKeyFromBufferShouldReturnSource() { $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromBuffer("png file")); } - public function testWithValidApiKeyFromBufferShouldReturnSourceWithData() { + public function testFromBufferShouldReturnSourceWithData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -84,17 +84,17 @@ public function testWithValidApiKeyFromBufferShouldReturnSourceWithData() { $this->assertSame("compressed file", Tinify\Source::fromBuffer("png file")->toBuffer()); } - public function testWithValidApiKeyFromUrlShouldReturnSource() { + public function testFromUrlShouldReturnSource() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( "status" => 201, "headers" => array("Location" => "https://api.tinify.com/some/location") )); - $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromUrl("http://example.com/testWithValidApiKey.jpg")); + $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromUrl("http://example.com/test.jpg")); } - public function testWithValidApiKeyFromUrlShouldReturnSourceWithData() { + public function testFromUrlShouldReturnSourceWithData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -105,10 +105,10 @@ public function testWithValidApiKeyFromUrlShouldReturnSourceWithData() { "status" => 200, "body" => "compressed file" )); - $this->assertSame("compressed file", Tinify\Source::fromUrl("http://example.com/testWithValidApiKey.jpg")->toBuffer()); + $this->assertSame("compressed file", Tinify\Source::fromUrl("http://example.com/test.jpg")->toBuffer()); } - public function testWithValidApiKeyFromUrlShouldThrowExceptionIfRequestIsNotOK() { + public function testFromUrlShouldThrowExceptionIfRequestIsNotOK() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -119,7 +119,7 @@ public function testWithValidApiKeyFromUrlShouldThrowExceptionIfRequestIsNotOK() Tinify\Source::fromUrl("file://wrong"); } - public function testWithValidApiKeyResultShouldReturnResult() { + public function testResultShouldReturnResult() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -134,7 +134,7 @@ public function testWithValidApiKeyResultShouldReturnResult() { $this->assertInstanceOf("Tinify\Result", Tinify\Source::fromBuffer("png file")->result()); } - public function testWithValidApiKeyPreserveShouldReturnSource() { + public function testPreserveShouldReturnSource() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -149,7 +149,7 @@ public function testWithValidApiKeyPreserveShouldReturnSource() { $this->assertSame("png file", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyPreserveShouldReturnSourceWithData() { + public function testPreserveShouldReturnSourceWithData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -164,7 +164,7 @@ public function testWithValidApiKeyPreserveShouldReturnSourceWithData() { $this->assertSame("{\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyPreserveShouldReturnSourceWithDataForArray() { + public function testPreserveShouldReturnSourceWithDataForArray() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -179,7 +179,7 @@ public function testWithValidApiKeyPreserveShouldReturnSourceWithDataForArray() $this->assertSame("{\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyPreserveShouldIncludeOtherOptionsIfSet() { + public function testPreserveShouldIncludeOtherOptionsIfSet() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -196,7 +196,7 @@ public function testWithValidApiKeyPreserveShouldIncludeOtherOptionsIfSet() { $this->assertSame("{\"resize\":{\"width\":400},\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyResizeShouldReturnSource() { + public function testResizeShouldReturnSource() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -211,7 +211,7 @@ public function testWithValidApiKeyResizeShouldReturnSource() { $this->assertSame("png file", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyResizeShouldReturnSourceWithData() { + public function testResizeShouldReturnSourceWithData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -286,7 +286,7 @@ public function testTransformShouldReturnSourceWithData() { $this->assertSame("{\"transform\":{\"background\":\"black\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyStoreShouldReturnResultMeta() { + public function testStoreShouldReturnResultMeta() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -303,7 +303,7 @@ public function testWithValidApiKeyStoreShouldReturnResultMeta() { $this->assertSame("{\"store\":{\"service\":\"s3\",\"aws_secret_access_key\":\"abcde\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyStoreShouldReturnResultMetaWithLocation() { + public function testStoreShouldReturnResultMetaWithLocation() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -323,7 +323,7 @@ public function testWithValidApiKeyStoreShouldReturnResultMetaWithLocation() { $this->assertSame("{\"store\":{\"service\":\"s3\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyStoreShouldIncludeOtherOptionsIfSet() { + public function testStoreShouldIncludeOtherOptionsIfSet() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -340,7 +340,7 @@ public function testWithValidApiKeyStoreShouldIncludeOtherOptionsIfSet() { $this->assertSame("{\"resize\":{\"width\":300},\"store\":{\"service\":\"s3\",\"aws_secret_access_key\":\"abcde\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testWithValidApiKeyToBufferShouldReturnImageData() { + public function testToBufferShouldReturnImageData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -353,7 +353,7 @@ public function testWithValidApiKeyToBufferShouldReturnImageData() { $this->assertSame("compressed file", Tinify\Source::fromBuffer("png file")->toBuffer()); } - public function testWithValidApiKeyToFileShouldStoreImageData() { + public function testToFileShouldStoreImageData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( From a0644b5b80b2fe3e9b3ed1ae9e863b4fddaa825d Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Mon, 15 Aug 2022 15:05:15 +0200 Subject: [PATCH 3/5] Update CHANGELOG and bump version --- CHANGES.md | 4 ++++ lib/Tinify.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index c0c95e0..3322978 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## 1.6.0 +* Support to run the unittests on newer versions of PHP (5.5 +) +* Add API methods for transcoding and transformation + ## 1.5.2 * Fail early if version of curl/openssl is too old. diff --git a/lib/Tinify.php b/lib/Tinify.php index 8d44685..f6f9b0c 100644 --- a/lib/Tinify.php +++ b/lib/Tinify.php @@ -2,7 +2,7 @@ namespace Tinify; -const VERSION = "1.5.2"; +const VERSION = "1.6.0"; class Tinify { private static $key = NULL; From b52710696d27bfef9ffc5ead0899d0e33fdf3abf Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Tue, 20 Sep 2022 14:11:32 +0200 Subject: [PATCH 4/5] Change transcode to convert --- lib/Tinify/Source.php | 4 ++-- test/TinifySourceTest.php | 14 +++++++------- test/integration.php | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 5c20e9c..304040a 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -42,8 +42,8 @@ public function store($options) { return new Result($response->headers, $response->body); } - public function transcode($types) { - $commands = array_merge($this->commands, array("type" => $types)); + public function convert($options) { + $commands = array_merge($this->commands, array("convert" => $options)); return new self($this->url, $commands); } diff --git a/test/TinifySourceTest.php b/test/TinifySourceTest.php index 786bc81..80c5fd8 100644 --- a/test/TinifySourceTest.php +++ b/test/TinifySourceTest.php @@ -226,7 +226,7 @@ public function testResizeShouldReturnSourceWithData() { $this->assertSame("{\"resize\":{\"width\":400}}", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testTranscodeShouldReturnSource() { + public function testConvertShouldReturnSource() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -234,14 +234,14 @@ public function testTranscodeShouldReturnSource() { )); CurlMock::register("https://api.tinify.com/some/location", array( - "status" => 200, "body" => "transcoded file" + "status" => 200, "body" => "Convertd file" )); - $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromBuffer("png file")->transcode("image/webp")); + $this->assertInstanceOf("Tinify\Source", Tinify\Source::fromBuffer("png file")->Convert(array("type" =>"image/webp"))); $this->assertSame("png file", CurlMock::last(CURLOPT_POSTFIELDS)); } - public function testTranscodeShouldReturnSourceWithData() { + public function testConvertShouldReturnSourceWithData() { Tinify\setKey("valid"); CurlMock::register("https://api.tinify.com/shrink", array( @@ -249,11 +249,11 @@ public function testTranscodeShouldReturnSourceWithData() { )); CurlMock::register("https://api.tinify.com/some/location", array( - "status" => 200, "body" => "transcoded file" + "status" => 200, "body" => "Convertd file" )); - $this->assertSame("transcoded file", Tinify\Source::fromBuffer("png file")->transcode("image/webp")->toBuffer()); - $this->assertSame("{\"type\":\"image\/webp\"}", CurlMock::last(CURLOPT_POSTFIELDS)); + $this->assertSame("Convertd file", Tinify\Source::fromBuffer("png file")->convert(array("type" => "image/webp"))->toBuffer()); + $this->assertSame("{\"convert\":{\"type\":\"image\/webp\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); } public function testTransformShouldReturnSource() { diff --git a/test/integration.php b/test/integration.php index 7167315..32226ac 100644 --- a/test/integration.php +++ b/test/integration.php @@ -78,9 +78,9 @@ public function testShouldPreserveMetadata() { $this->assertStringContainsString("Copyright Voormedia", $contents); } - public function testShouldTranscode() { + public function testShouldConvert() { $path = tempnam(sys_get_temp_dir(), "tinify-php"); - self::$optimized->transcode(["image/webp"])->toFile($path); + self::$optimized->convert(array("type" => ["image/webp"]))->toFile($path); $size = filesize($path); $contents = fread(fopen($path, "rb"), $size); From 2165f0b60c19492852d054510dcefb48a783481a Mon Sep 17 00:00:00 2001 From: Remco Koopmans Date: Tue, 20 Sep 2022 14:18:59 +0200 Subject: [PATCH 5/5] Add method for returning file extension --- CHANGES.md | 3 ++- lib/Tinify/ResultMeta.php | 8 ++++++++ test/TinifyResultTest.php | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 3322978..75d5cc8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ ## 1.6.0 * Support to run the unittests on newer versions of PHP (5.5 +) -* Add API methods for transcoding and transformation +* Add API methods for converting/transcoding and transformation +* Add helper function for returning the compressed file extension ## 1.5.2 * Fail early if version of curl/openssl is too old. diff --git a/lib/Tinify/ResultMeta.php b/lib/Tinify/ResultMeta.php index 337a48f..27aba0e 100644 --- a/lib/Tinify/ResultMeta.php +++ b/lib/Tinify/ResultMeta.php @@ -20,4 +20,12 @@ public function height() { public function location() { return isset($this->meta["location"]) ? $this->meta["location"] : null; } + + public function extension() { + if (isset($this->meta["content-type"])) { + $parts = explode("/", $this->meta["content-type"]); + return end($parts); + } + return null; + } } diff --git a/test/TinifyResultTest.php b/test/TinifyResultTest.php index 81687c5..94e3406 100644 --- a/test/TinifyResultTest.php +++ b/test/TinifyResultTest.php @@ -32,4 +32,14 @@ public function testWithMetaAndDataToBufferShouldReturnImageData() { $result = new Tinify\Result(array(), "image data"); $this->assertSame("image data", $result->toBuffer()); } + + public function testWithMetadataReturnsExtension() { + $result = new Tinify\Result(array("content-type" => "image/png"), "image data"); + $this->assertSame($result->extension(), "png"); + } + + public function testWithoutMetadataReturnsExtensionAsNull() { + $result = new Tinify\ResultMeta(array(), "image data"); + $this->assertSame($result->extension(), null); + } }