From 56dfb329c4cb4bece8025427a38d35a3788500db Mon Sep 17 00:00:00 2001 From: Joe Devine Date: Thu, 21 Mar 2024 17:41:56 -0600 Subject: [PATCH 1/5] added .idea directory to .gitignore (the directory JetBrains IDEs project config in) --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 035a31c..cd6bf99 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,7 @@ !bin/heicToJpg.exe !tests/Unit/images/* -vendor \ No newline at end of file +vendor + +# JetBrains IDE config +.idea \ No newline at end of file From 3b856657aa77a43a384cddb9032286a4797ca51d Mon Sep 17 00:00:00 2001 From: Joe Devine Date: Thu, 21 Mar 2024 17:42:56 -0600 Subject: [PATCH 2/5] removed unused parameter --- src/HeicToJpg.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/HeicToJpg.php b/src/HeicToJpg.php index 9b0c13e..9a3c295 100644 --- a/src/HeicToJpg.php +++ b/src/HeicToJpg.php @@ -17,13 +17,6 @@ class HeicToJpg { */ private string $jpg; - /** - * Stores original HEIC image path - * - * @var string - */ - protected string $heic; - /** * Executable file name from bin folder * @@ -186,7 +179,6 @@ public function checkOS($forceArm = false) { */ protected function processImage(string $source) { $source = htmlspecialchars($source); - $this->heic = $source; $newFileName = $source . "-" . uniqid(rand(), true); $exeName = $this->exeName; $command = __DIR__.'/../bin/'.$exeName.' "'.$source.'" "'.$newFileName.'" 2>&1'; From 63f71db184a9b24c6b4c8cc172128832168194fd Mon Sep 17 00:00:00 2001 From: Joe Devine Date: Thu, 21 Mar 2024 17:48:05 -0600 Subject: [PATCH 3/5] replaced calls to str_contains() with smiple custom static stringContains() method; this library does not specify a required version of PHP but str_contains() is only available for PHP >= 8.0 --- src/HeicToJpg.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/HeicToJpg.php b/src/HeicToJpg.php index 9a3c295..f84b4bd 100644 --- a/src/HeicToJpg.php +++ b/src/HeicToJpg.php @@ -114,15 +114,19 @@ public function checkMacOS(): self { $os = strtolower(php_uname('s')); $arch = strtolower(php_uname('m')); - if (str_contains($os, 'macos') || str_contains($os, 'os x') || str_contains($os, 'darwin') || str_contains($os, 'macintosh')) { + if (self::stringContains($os, 'macos') + || self::stringContains($os, 'os x') + || self::stringContains($os, 'darwin') + || self::stringContains($os, 'macintosh') + ) { $this->os = "darwin"; } - if (str_contains($arch, "x86_64") || str_contains($arch, "amd64")) { + if (self::stringContains($arch, "x86_64") || self::stringContains($arch, "amd64")) { $this->arch = "amd64"; } - if (str_contains($arch, "arm")) { + if (self::stringContains($arch, "arm")) { $this->arch = "arm64"; } @@ -135,11 +139,11 @@ public function checkLinuxOS(): self { $os = strtolower(php_uname('s')); $arch = strtolower(php_uname('m')); - if (str_contains($os, 'linux')) { + if (self::stringContains($os, 'linux')) { $this->os = "linux"; } - if (str_contains($arch, "aarch64") || str_contains($arch, "arm64")){ + if (self::stringContains($arch, "aarch64") || self::stringContains($arch, "arm64")){ $this->arch = "arm64"; } @@ -157,7 +161,7 @@ public function checkWindowsOS(): self { $os = strtolower(php_uname('s')); $arch = strtolower(php_uname('m')); - if (str_contains($os, 'windows') || str_contains($os, 'win')) { + if (self::stringContains($os, 'windows') || self::stringContains($os, 'win')) { $this->os = "windows"; } @@ -366,4 +370,8 @@ public static function isHeic(string $path): bool return false; } + private static function stringContains(string $haystack, string $needle): bool + { + return strpos($haystack, $needle) !== false; + } } From 4d2460e936f038935c9b9fd2da9c2422b01c0fca Mon Sep 17 00:00:00 2001 From: Joe Devine Date: Thu, 21 Mar 2024 17:58:21 -0600 Subject: [PATCH 4/5] added missing type definitions and removed redundant PHPDoc comments --- src/HeicToJpg.php | 83 ++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 63 deletions(-) diff --git a/src/HeicToJpg.php b/src/HeicToJpg.php index f84b4bd..93b98f3 100644 --- a/src/HeicToJpg.php +++ b/src/HeicToJpg.php @@ -6,49 +6,36 @@ class HeicToJpg { /** * Stores binary content of JPG file - * */ - private $binary; + private string $binary; /** * Stores converted JPG file location - * - * @var string */ private string $jpg; /** * Executable file name from bin folder - * - * @var string */ protected string $exeName = "heicToJpg"; /** * OS of server - * - * @var string */ protected string $os = "linux"; /** * Architecture of server - * - * @var string */ protected string $arch = "amd64"; /** * Force arm64 - * - * @var bool */ protected bool $forceArm = false; /** * Location of the "heif-converter-image" package's executable - * - * @var string */ protected string $libheifConverterLocation = ""; @@ -56,10 +43,8 @@ class HeicToJpg { /** * Takes full location of file as a string - * - * @param string $source */ - public function convertImage(string $source) { + public function convertImage(string $source): self { $this->checkLinuxOS(); $this->processImage($source); $this->extractBinary(); @@ -68,10 +53,8 @@ public function convertImage(string $source) { /** * The same as convertImage but for MacOS users - * - * @param string $source */ - public function convertImageMac(string $source, $arch = "amd64") { + public function convertImageMac(string $source, string $arch = "amd64"): self { $this->setDarwinExe($arch); $this->processImage($source); $this->extractBinary(); @@ -80,35 +63,27 @@ public function convertImageMac(string $source, $arch = "amd64") { /** * Saves JPG file as $path (Full location is preferable) - * - * @param string $path - * @return bool */ - public function saveAs(string $path) { + public function saveAs(string $path): bool { file_put_contents($path, $this->binary); return $this->exit(); } /** - * Removes temporary JPG file and returns it's content (binary) - * - * @return string + * Removes temporary JPG file and returns its content (binary) */ - public function get() { + public function get(): string { $this->exit(); return $this->binary; } - - public function setConverterLocation(string $path) { + public function setConverterLocation(string $path): self { $this->libheifConverterLocation = $path; return $this; } /** * Checks is used on macOS or not - * - * @return self */ public function checkMacOS(): self { $os = strtolower(php_uname('s')); @@ -170,18 +145,15 @@ public function checkWindowsOS(): self { return $this; } - public function checkOS($forceArm = false) { + public function checkOS($forceArm = false): self { $this->forceArm = $forceArm; return $this->checkWindowsOS()->checkLinuxOS()->checkMacOS(); } /** * Runs heicToJpg CLI tool to convert file - * - * @param string $source - * @return void */ - protected function processImage(string $source) { + protected function processImage(string $source): void { $source = htmlspecialchars($source); $newFileName = $source . "-" . uniqid(rand(), true); $exeName = $this->exeName; @@ -203,7 +175,7 @@ protected function processImage(string $source) { } } - protected function tryToConvertWithLibheif($source, $newFile) { + protected function tryToConvertWithLibheif(string $source, string $newFile): bool { // ./vendor/bin/heif-converter-linux heic input.heic output.png if (empty($this->libheifConverterLocation)) { $this->libheifConverterLocation = __DIR__.'/../bin/' . "heif-converter-" . $this->os; @@ -228,22 +200,15 @@ protected function tryToConvertWithLibheif($source, $newFile) { /** * Read the content of file - * - * @return void */ - protected function extractBinary() { - $this->binary = file_get_contents($this->jpg); + protected function extractBinary(): void { + $this->binary = file_get_contents($this->jpg) ?: ''; } /** * Returns string between $start and $end - * - * @param string $string - * @param string $start - * @param string $end - * @return void */ - private function getStringBetween(string $string, string $start, string $end){ + private function getStringBetween(string $string, string $start, string $end): string { $string = ' ' . $string; $ini = strpos($string, $start); if ($ini == 0) return ''; @@ -255,9 +220,9 @@ private function getStringBetween(string $string, string $start, string $end){ /** * Removes converted JPG file * - * @return bool + * @throws \Exception if JPG file does not exist */ - private function exit() { + private function exit(): bool { if(file_exists($this->jpg)) { unlink($this->jpg); return true; @@ -282,10 +247,8 @@ private function checkWindowsExe(): void /** * Check os and arch properties to set executable name correctly - * - * @return void */ - private function checkDarwinExe() { + private function checkDarwinExe(): void { if ($this->os == "darwin" && $this->arch == "amd64") { $this->exeName = "php-heic-to-jpg-darwin-amd64"; } @@ -296,11 +259,8 @@ private function checkDarwinExe() { /** * Sets macOS executable by architecture - * - * @param string $arch - * @return void */ - private function setDarwinExe(string $arch) { + private function setDarwinExe(string $arch): void { if ($arch == "arm64") { $this->exeName = "php-heic-to-jpg-darwin-arm64"; } else { @@ -308,7 +268,7 @@ private function setDarwinExe(string $arch) { } } - public static function convert(string $source, string $converterPath = "", $forceArm = false) + public static function convert(string $source, string $converterPath = "", $forceArm = false): self { return (new self) ->checkOS($forceArm) @@ -316,12 +276,12 @@ public static function convert(string $source, string $converterPath = "", $forc ->convertImage($source); } - public static function convertOnMac(string $source, string $arch = "amd64", string $converterPath = "") + public static function convertOnMac(string $source, string $arch = "amd64", string $converterPath = ""): self { return (new self)->setConverterLocation($converterPath)->convertImageMac($source, $arch); } - public static function convertFromUrl(string $url, string $converterPath = "", $forceArm = false) { + public static function convertFromUrl(string $url, string $converterPath = "", $forceArm = false): self { // Download image $newFileName = "HTTP" . "-" . uniqid(rand(), true); file_put_contents($newFileName, file_get_contents($url)); @@ -340,9 +300,6 @@ public static function convertFromUrl(string $url, string $converterPath = "", $ /** * Check if file is in HEIC format. - * - * @param string $path - * @return bool */ public static function isHeic(string $path): bool { From cf928f02e83fcac444709732750682ed56b13b99 Mon Sep 17 00:00:00 2001 From: Joe Devine Date: Thu, 21 Mar 2024 17:59:25 -0600 Subject: [PATCH 5/5] removed unused parameter --- src/HeicToJpg.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/HeicToJpg.php b/src/HeicToJpg.php index 93b98f3..b4612a2 100644 --- a/src/HeicToJpg.php +++ b/src/HeicToJpg.php @@ -134,7 +134,6 @@ public function checkLinuxOS(): self { public function checkWindowsOS(): self { $os = strtolower(php_uname('s')); - $arch = strtolower(php_uname('m')); if (self::stringContains($os, 'windows') || self::stringContains($os, 'win')) { $this->os = "windows";