Skip to content

Commit

Permalink
MacOS support added
Browse files Browse the repository at this point in the history
  • Loading branch information
MaestroError committed Feb 26, 2023
1 parent c154cc7 commit 870e214
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Maestroerror\HeicToJpg::convert("image1.heic")->saveAs("image1.jpg");
// 2. get content (binary) of converted JPG
$jpg = Maestroerror\HeicToJpg::convert("image1.heic")->get();
```
### For MacOS users
It should detect the OS itself, but if you want to specify architecture, it is recommended to use `convertOnMac` instead. The second argument is architecture of your system, by default set as "amd64", but you can specify "arm64" (aarm64, M1)
```php
// By default
Maestroerror\HeicToJpg::convertOnMac("image1.heic", "arm64")->saveAs("image1.jpg");
```

## Credits
I would like to say thanks to these people. Their work helped me to build heicToJpg file with Go:
Expand Down
Binary file added bin/php-heic-to-jpg-macos.zip
Binary file not shown.
95 changes: 93 additions & 2 deletions src/HeicToJpg.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ class HeicToJpg {
*/
protected string $heic;

/**
* 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";

/**
* Takes full location of file as a string
*
Expand All @@ -35,6 +56,18 @@ public function convertImage(string $source) {
return $this;
}

/**
* The same as convertImage but for MacOS users
*
* @param string $source
*/
public function convertImageMac(string $source, $arch = "amd64") {
$this->setDarwinExe($arch);
$this->processImage($source);
$this->extractBinary();
return $this;
}

/**
* Saves JPG file as $path (Full location is preferable)
*
Expand All @@ -56,6 +89,30 @@ public function get() {
return $this->binary;
}

/**
* Checks is used on macOS or not
*
* @return void
*/
public function checkMacOS() {
$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')) {
$this->os = "darwin";
}

if (str_contains($arch, "x86_64") || str_contains($arch, "amd64")) {
$this->arch = "amd64";
}

if (str_contains($arch, "arm")) {
$this->arch = "arm64";
}

$this->checkDarwinExe();
}

/**
* Runs heicToJpg CLI tool to convert file
*
Expand All @@ -65,7 +122,8 @@ public function get() {
protected function processImage(string $source) {
$this->heic = $source;
$newFileName = $source . "-" . uniqid(rand(), true);
exec(__DIR__."/../bin/heicToJpg $source $newFileName", $output);
$exeName = $this->exeName;
exec(__DIR__."/../bin/$exeName $source $newFileName", $output);
foreach ($output as $line) {
$parsed = $this->getStringBetween($line, '--', '--');
if (!empty($parsed)) {
Expand Down Expand Up @@ -118,8 +176,41 @@ private function exit() {
}
}

/**
* Check os and arch properties to set executable name correctly
*
* @return void
*/
private function checkDarwinExe() {
if ($this->os == "darwin" && $this->arch == "amd64") {
$this->exeName = "php-heic-to-jpg-darwin-amd64";
}
if ($this->os == "darwin" && $this->arch == "arm64") {
$this->exeName = "php-heic-to-jpg-darwin-arm64";
}
}

/**
* Sets macOS executable by architecture
*
* @param string $arch
* @return void
*/
private function setDarwinExe(string $arch) {
if ($arch == "arm64") {
$this->exeName = "php-heic-to-jpg-darwin-arm64";
} else {
$this->exeName = "php-heic-to-jpg-darwin-amd64";
}
}

public static function convert(string $source)
{
return (new self)->convertImage($source);
return (new self)->checkMacOS()->convertImage($source);
}

public static function convertOnMac(string $source, $arch = "amd64")
{
return (new self)->convertImageMac($source, $arch);
}
}
7 changes: 7 additions & 0 deletions test-os.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

echo php_uname('s');
echo "\n";
echo php_uname('v');
echo "\n";
echo php_uname('m');

0 comments on commit 870e214

Please sign in to comment.