Last active
October 27, 2022 14:03
-
-
Save syofyanzuhad/6cc7144278568ac2c92d982fdcd53e40 to your computer and use it in GitHub Desktop.
Laravel Helper to get logo wilayah Indonesia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Helpers; | |
class LogoWilayah | |
{ | |
public static function handle($kodeWilayah) { | |
$logo = self::getLogo($kodeWilayah); | |
// if $kode is array, loop it | |
if (is_array($kodeWilayah)) { | |
$logo = []; | |
foreach ($kodeWilayah as $kode) { | |
$logo[] = self::getLogo($kode); | |
} | |
} | |
return $logo; | |
} | |
private static function getLogo($kodeWilayah) | |
{ | |
if(is_array($kodeWilayah)) { | |
$urls = []; | |
foreach ($kodeWilayah as $kode) { | |
$ext = ['png', 'jpg', 'jpeg', 'gif']; | |
// check if file exists | |
foreach ($ext as $value) { | |
$url = "https://cdn.btekno.id/assets/logo/$kode.$value"; | |
if (self::checkFile($url)) { | |
// push to array | |
$urls[] = $url; | |
echo $url . PHP_EOL; | |
} | |
} | |
} | |
return $urls; | |
} else { | |
$ext = ['png', 'jpg', 'jpeg', 'gif']; | |
// check if file exists | |
foreach ($ext as $value) { | |
$url = "https://cdn.btekno.id/assets/logo/$kodeWilayah.$value"; | |
if (self::checkFile($url)) { | |
return $url; | |
} | |
} | |
} | |
} | |
private static function checkFile($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_NOBODY, 1); | |
curl_setopt($ch, CURLOPT_FAILONERROR, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
// return extension of file | |
return $result!==false ? pathinfo($url, PATHINFO_EXTENSION) : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment