将文本转换为图像 - PHP / GD - 保存图像

时间:2010-04-16 05:11:48

标签: php string image email gd

我正在使用此脚本简单地从文本创建图像。我想知道的是如何保存图像而不是直接打印到浏览器;

// 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);

我知道最后可能只有一行代码,但我不确定使用哪种GD功能。

任何帮助都会非常感激,问候,菲尔。

编辑:

我试过以下但只是得到一个黑色的盒子;

 // 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);

3 个答案:

答案 0 :(得分:2)

将文件名传递给适当的图像生成image*()函数:

imagepng($image, 'somefile.png');

答案 1 :(得分:1)

答案 2 :(得分:0)

查看imagepng()(或imagegif,imagejpeg ...) - 如果添加文件名作为第二个参数,图像将保存为文件。