簡體   English   中英

將文本轉換為圖像 - PHP / GD - 保存圖像

[英]turning text into image - PHP/GD - save image

我正在使用此腳本來簡單地從文本創建圖像。 我想知道的是如何保存圖像而不是直接打印到瀏覽器;

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

將文件名傳遞給適當的圖像生成image*()函數:

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

看看imagepng() (或imagegif,imagejpeg ...) - 如果你添加一個文件名作為第二個參數,圖像將保存為文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM