将文本转换为图像 – PHP / 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);

解决方法:

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

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...