diff --git a/CHANGES.md b/CHANGES.md index 7a0c177..2d344a7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## Unreleased + +* Added `Format` constants for the media types `convert` accepts, including JPEG XL (`image/jxl`). + ## 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. diff --git a/lib/Tinify/Format.php b/lib/Tinify/Format.php new file mode 100644 index 0000000..3cee741 --- /dev/null +++ b/lib/Tinify/Format.php @@ -0,0 +1,43 @@ +convert(array("type" => \Tinify\Format::JXL)) + * ->toFile("output.jxl"); + * + * Multiple types may be supplied, in which case the API returns the smallest + * result: + * + * array("type" => array(\Tinify\Format::JXL, \Tinify\Format::WEBP)) + */ +final class Format { + /** WebP. */ + const WEBP = "image/webp"; + + /** PNG. */ + const PNG = "image/png"; + + /** JPEG. */ + const JPEG = "image/jpeg"; + + /** JPEG, an alias of self::JPEG. */ + const JPG = "image/jpg"; + + /** AVIF. */ + const AVIF = "image/avif"; + + /** JPEG XL. */ + const JXL = "image/jxl"; + + /** Wildcard, returns the smallest of the supported types. */ + const ANY = "*/*"; + + private function __construct() { + } +} diff --git a/test/TinifyFormatTest.php b/test/TinifyFormatTest.php new file mode 100644 index 0000000..090bf76 --- /dev/null +++ b/test/TinifyFormatTest.php @@ -0,0 +1,50 @@ +assertSame("image/webp", Tinify\Format::WEBP); + $this->assertSame("image/png", Tinify\Format::PNG); + $this->assertSame("image/jpeg", Tinify\Format::JPEG); + $this->assertSame("image/jpg", Tinify\Format::JPG); + $this->assertSame("image/avif", Tinify\Format::AVIF); + $this->assertSame("image/jxl", Tinify\Format::JXL); + $this->assertSame("*/*", Tinify\Format::ANY); + } + + public function testConvertWithFormatShouldSerializeMediaType() { + 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" => "converted file" + )); + + Tinify\Source::fromBuffer("png file")->convert(array("type" => Tinify\Format::JXL))->toBuffer(); + $this->assertSame("{\"convert\":{\"type\":\"image\/jxl\"}}", CurlMock::last(CURLOPT_POSTFIELDS)); + } + + public function testConvertWithMultipleFormatsShouldSerializeMediaTypes() { + 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" => "converted file" + )); + + Tinify\Source::fromBuffer("png file") + ->convert(array("type" => array(Tinify\Format::JXL, Tinify\Format::WEBP))) + ->toBuffer(); + $this->assertSame( + "{\"convert\":{\"type\":[\"image\/jxl\",\"image\/webp\"]}}", + CurlMock::last(CURLOPT_POSTFIELDS) + ); + } +}