Turn text into image - PHP / GD - save image

I use this script to just create an image from text. I would like to know how to save the image, and not print directly in the browser;

// an email address in a string $string = $post[$key]; // some variables to set $font = 4; $width = ImageFontWidth($font) * strlen($string); $height = ImageFontHeight($font); // lets begin by creating an image $im = @imagecreatetruecolor ($width,$height); //white background $background_color = imagecolorallocate ($im, 255, 255, 255); //black text $text_color = imagecolorallocate ($im, 0, 0, 0); // put it all together $image = imagestring ($im, $font, 0, 0, $string, $text_color); 

I know it, probably only one line of code at the end, but I'm not sure which GD function to use.

Any help would be greatly appreciated, Regards, Phil.

EDIT:

I tried the following, but just got a black box with beams;

  // an email address in a string $string = $post[$key]; // some variables to set $font = 4; $width = ImageFontWidth($font) * strlen($string); $height = ImageFontHeight($font); // lets begin by creating an image $im = @imagecreatetruecolor ($width,$height); //white background $background_color = imagecolorallocate ($im, 255, 255, 255); //black text $text_color = imagecolorallocate ($im, 0, 0, 0); // put it all together imagestring ($im, $font, 0, 0, $string, $text_color); imagepng($im, 'somefile.png'); imagedestroy($im); 
+4
source share
3 answers

Pass the file name to the appropriate image*() function to generate the images:

 imagepng($image, 'somefile.png'); 
+2
source

look at imagepng() (or imagegif, imagejpeg ...) - if you add the file name as the second parameter, the image is saved as a file.

0
source

Source: https://habr.com/ru/post/1307107/


All Articles