Generate image with initials of name, same as Hotmail, Skype, Gmail

Asked

Viewed 142 times

0

I am making a code to generate image from text for client registration, for clients who did not upload image, was the initials of the name and surname as image, equal to gmail, Hotmail and etc. The problem is that, we have the $nome which contains the full name of the person, I am not able to take only the first letter of the first and second name, for example, "Thomas Franklin" take only "TH" and save as image in the bank and Hosp. The image generation code I already have, but the part where you select the letters and save the image I’m not getting.

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();   
?>

1 answer

2


You could do it this way, using the function explodes and catching the 1 character of the first and second array

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

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

For sending 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

  • You are too expensive. Algora just need to be able to save in Hosp. kaksaks

  • Solved 50% of the problems, adushds but I will mark yes.

  • save image to folder

  • Dude, I got kaskask

  • @Thomasfranklin, if you have another problem, ask another question.

  • @vmsouza, I know, this question encompasses these two problems.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.