Created
May 23, 2020 03:59
-
-
Save StalinMazaEpn/de3d2350d5a6d34361f096480f61c424 to your computer and use it in GitHub Desktop.
Laravel FileSystem Helpers
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 | |
/** | |
* Guarda la imagen de una publicacion y retorna el nombre de la imagen guardada | |
* @param string $base64IMG | |
* @param mixed $previous_name | |
* | |
* @return string | |
*/ | |
public function savePostImageApi($base64IMG, $previous_name = null) | |
{ | |
try { | |
$img_file = $this->getB64Image($base64IMG); | |
$img_extension = $this->getB64Extension($base64IMG); | |
$img_name = ($previous_name) ? $previous_name : 'post' . time() . '.' . $img_extension; | |
$this->saveImageInDisk($this->diskImage, $img_name, $img_file); | |
return $img_name; | |
} catch (Error $e) { | |
echo $e->getMessage(); | |
} | |
} | |
/** | |
* Devuelve la extension de una imagen Base64 | |
* @param string $base64_image | |
* @param boolean $full | |
* | |
* @return string | |
*/ | |
private function getB64Extension($base64_image, $full = null) | |
{ | |
try { | |
$img = explode(',', $base64_image); | |
$ini = substr($img[0], 11); | |
$img_extension = explode(';', $ini); | |
if ($full) { | |
return "image/" . $img_extension[0]; | |
} else { | |
return $img_extension[0]; | |
} | |
} catch (Error $e) { | |
echo $e->getMessage(); | |
} | |
} | |
/** | |
* Devuelve una imagen Base64 Decodificada | |
* @param string $base64_image_encoded | |
* | |
* @return string | |
*/ | |
private function getB64Image($base64_image_encoded) | |
{ | |
try { | |
// Obtener el String base-64 de los datos | |
$image_service_str = substr($base64_image_encoded, strpos($base64_image_encoded, ",") + 1); | |
// Decodificar ese string y devolver los datos de la imagen | |
$image = base64_decode($image_service_str); | |
return $image; | |
} catch (Error $e) { | |
echo $e->getMessage(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment