Skip to content

Instantly share code, notes, and snippets.

@LucianoCharlesdeSouza
Last active November 14, 2019 12:31
Show Gist options
  • Select an option

  • Save LucianoCharlesdeSouza/09fad46aa1856d24a97348f7fa29fbed to your computer and use it in GitHub Desktop.

Select an option

Save LucianoCharlesdeSouza/09fad46aa1856d24a97348f7fa29fbed to your computer and use it in GitHub Desktop.
Create image with text in php
<?php
//Seto o Content Type
// header('Content-type: image/jpeg');
// Crio a imagem com um arquivo existente
$jpg_image = imagecreatefromjpeg('image.jpg');
// Aloco uma cor para o texto
$white = imagecolorallocate($jpg_image, 251, 22808, 10);
// Seto o arquivo de fonte existente
$font_file = 'JOKERMAN.TTF';
// Seto o texto a ser posto na imagem
$text = '"entre uma estrela e um vagalume o sol se poe."';
function makeImageWithText($image, $fontsize,$angle, $x, $y, $color, $fontfile, $widthBox, $text)
{
$words = explode(' ', $text);
$lines = array($words[0]);
$currentLine = 0;
for($i = 1; $i < count($words); $i++)
{
$lineSize = imagettfbbox($fontsize, 0, $fontfile, $lines[$currentLine] . ' ' . $words[$i]);
if($lineSize[2] - $lineSize[0] < $widthBox)
{
$lines[$currentLine] .= ' ' . $words[$i];
}
else
{
$currentLine++;
$lines[$currentLine] = $words[$i];
}
}
return imagettftext($image, $fontsize, $angle, $x, $y, $color, $fontfile, implode("\n", $lines));
}
// Escrevo o texto na imagem
makeImageWithText($jpg_image, 18, 0, 480, 50, $white, $font_file, 200, $text) ;
//Envio a imagem para o Browser
// imagejpeg($jpg_image);
//Envio a imagem para o disco com uma qualidade de 100
imagejpeg($jpg_image,'imagem.jpg',100);
//Limpo a mem贸ria, destruindo a imagem
imagedestroy($jpg_image);
header("Location:imagem.php");
//crie um arquivo chamado iamgem.php e cole isso dentro, isso fara com que ao clicar na imagem, será feito o download da mesma
//echo '<a href="http://realtrueweb.com.br/crearte/imagem.jpg" download><img src="imagem.jpg" width="600"></a>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment