Generate image with initials of the name, like Hotmail, Skype, Gmail

0

I am making a code to generate image from text to the customer registry, for clients that did not upload image, be the initials of the name and surname as image, same as gmail, hotmail and so on. The problem is that we have $nome that contains the person's full name, I can not get only the first letter of the 1st and 2nd names, for example, "Thomas Franklin" only "TH" and save as image in the bank and hosp. The image generation code I already have, but the part where you make the selection of the letters and save the image I can not.

Code:

<?php
class GerarAvatar {
  function __construct()
  {
    $nome = $_GET['nome'];
    $model = $_GET['model'];
    $this->nome_para_avatar = $nome;
    $this->mod_para_avatar = $model;
  }
  public function gerar() 
  {
    header("Content-Type: image/jpeg");  
    $texto = $this->nome_para_avatar;
    $mod_escolhido = $this->mod_para_avatar;
    $add_extensao = '../imgs/' . $mod_escolhido . '.jpg';
    $font_path = '../fonts/Poppins-Bold.ttf';
    if ($mod_escolhido == 'vermelho') {
      $modelo = imagecreatefromjpeg($add_extensao);
      $bg_default = imagecolorallocate($modelo, 255, 255, 255);
      imagettftext($modelo, 20, 0, 30, 55, $bg_default, $font_path, $texto);
    }elseif ($mod_escolhido == 'azul') {
      $modelo = imagecreatefromjpeg($add_extensao);
      $bg_default = imagecolorallocate($modelo, 0, 0, 0);
      imagettftext($modelo, 12, 0, 10, 200, $bg_default, $font_path, $texto);
    }elseif($mod_escolhido !== 'vermelho'){
     $modelo = imagecreatefromjpeg('../imgs/cinza.jpg');
     $bg_default = imagecolorallocate($modelo, 255, 255, 255);
     imagettftext($modelo, 20, 0, 30, 55, $bg_default, $font_path, $texto);
   }
   imagejpeg($modelo);
   imagedestroy($modelo);
 }
}
$gerar = new GerarAvatar();
$gerar->gerar();   
?>
    
asked by anonymous 15.12.2017 / 22:55

1 answer

2

I could do this by using the explode function and picking up the 1 character of the first and second array

$array = explode(" ", $nome);

$iniciais = $array[0][0] . $array[1][0];

To send a file, it is advisable to store only the relative path of the images in the database, for example:

 $nome =  md5($_FILES['imagem']['name']);

 $arquivo_tmp = $_FILES['imagem']['tmp_name'];

 $destino = "/var/www/html/" . $nome;

 move_uploaded_file( $arquivo_tmp, $destino  ); 

And in the database save only the variable $nome

    
15.12.2017 / 23:05