Skip to content

Instantly share code, notes, and snippets.

@syofyanzuhad
Last active October 27, 2022 14:03
Show Gist options
  • Save syofyanzuhad/6cc7144278568ac2c92d982fdcd53e40 to your computer and use it in GitHub Desktop.
Save syofyanzuhad/6cc7144278568ac2c92d982fdcd53e40 to your computer and use it in GitHub Desktop.
Laravel Helper to get logo wilayah Indonesia
<?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