Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-jpg into maestro
  • Loading branch information
MaestroError committed Mar 22, 2024
2 parents 27b5253 + 3836b4a commit 072037d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 79 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
!bin/heicToJpg.exe
!tests/Unit/images/*

vendor
vendor

# JetBrains IDE config
.idea
112 changes: 34 additions & 78 deletions src/HeicToJpg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,45 @@ class HeicToJpg {

/**
* Stores binary content of JPG file
*
*/
private $binary;
private string $binary;

/**
* Stores converted JPG file location
*
* @var string
*/
private string $jpg;

/**
* Stores original HEIC image path
*
* @var string
*/
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";

/**
* Force arm64
*
* @var bool
*/
protected bool $forceArm = false;

/**
* Location of the "heif-converter-image" package's executable
*
* @var string
*/
protected string $libheifConverterLocation = "";

protected string $libheifOutput = "";

/**
* 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();
Expand All @@ -75,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();
Expand All @@ -87,49 +63,45 @@ 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'));
$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";
}

Expand All @@ -142,11 +114,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";
}

Expand All @@ -162,9 +134,8 @@ public function checkLinuxOS(): self {

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";
}

Expand All @@ -173,20 +144,16 @@ 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);
$this->heic = $source;
$newFileName = $source . "-" . uniqid(rand(), true);
$exeName = $this->exeName;
$command = __DIR__.'/../bin/'.$exeName.' "'.$source.'" "'.$newFileName.'" 2>&1';
Expand All @@ -207,7 +174,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;
Expand All @@ -232,22 +199,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 '';
Expand All @@ -259,9 +219,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;
Expand All @@ -286,10 +246,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";
}
Expand All @@ -300,32 +258,29 @@ 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 {
$this->exeName = "php-heic-to-jpg-darwin-amd64";
}
}

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)
->setConverterLocation($converterPath)
->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));
Expand All @@ -344,9 +299,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
{
Expand Down Expand Up @@ -374,4 +326,8 @@ public static function isHeic(string $path): bool
return false;
}

private static function stringContains(string $haystack, string $needle): bool
{
return strpos($haystack, $needle) !== false;
}
}

0 comments on commit 072037d

Please sign in to comment.