2010-12-20 97 views
16

我想從表單字段中獲取文本字符串的樣式,然後將其轉換爲透明的.PNG(alpha BG)。將文本轉換爲圖像php

這可能與PHP?如果是的話,你會好心告訴我如何做到這一點實現

+0

你可以試試這個:http://www.daftlogic.com/projects-text-to-image.htm – 2015-10-12 23:05:10

回答

19

是的,它很可能,你要遵循同樣的技術我們在生成驗證碼圖像時執行此操作。

要求:GD庫應該在你的php中啓用。

代碼(從PHP的幫助文件拍攝;)

<?php 
// Set the content-type 
header('Content-type: image/png'); 

// Create the image 
$im = imagecreatetruecolor(400, 30); 

// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 

// The text to draw 
$text = 'Testing...'; 
// Replace path by your own font path 
$font = 'arial.ttf'; 

// Add some shadow to the text 
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

// Add the text 
imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
?> 
0
$image = imagecreate(widthyouwant,heightyouwant); 
$bg = imagecolorallocatealpha($image,255,255,255,0); 
$black = imagecolorallocate($image,255,255,255); 
imagettftext($image,fontsize, 0, x, y, $black, "fontlocation", $_GET['text']); 
header('Content-Type: image/png'); 
imagepng($image); 

然後,您也可以前往 顯示圖像我認爲是正確的,一段時間以來我已經做到了這一點。

12

文本轉換成圖像(PHP代碼):

首先,你需要確保,該主機已啓用GD庫(在一個新的PHP文件,執行phpinfo();並在輸出中看到/發現如果GD庫被啓用)。

溶液1(自動大小輸出):

TextToImage_my($text='Helloooo! my unicode words: ǩ Ƥ Ў ض ط Ⴓ '); 
    // ==== other parameters can be used too, see the function arguments below 

功能的代碼:

function TextToImage_my(
    $text, 
    $separate_line_after_chars=40, 
    $font='./myfont.ttf', 
    $size=24, 
    $rotate=0, 
    $padding=2, 
    $transparent=true, 
    $color=array('red'=>0,'grn'=>0,'blu'=>0), 
    $bg_color=array('red'=>255,'grn'=>255,'blu'=>255) 
){ 
    $amount_of_lines= ceil(strlen($text)/$separate_line_after_chars); 
    $x=explode("\n", $text); $final=''; 
    foreach($x as $key=>$value){ 
     $returnes=''; 
     do{ $first_part=mb_substr($value, 0, $separate_line_after_chars, 'utf-8'); 
      $value= "\n".mb_substr($value, $separate_line_after_chars, null, 'utf-8'); 
      $returnes .=$first_part; 
     } while(mb_strlen($value,'utf-8')>$separate_line_after_chars); 
     $final .= $returnes."\n"; 
    } 
    $text=$final; 
    Header("Content-type: image/png"); 
    $width=$height=$offset_x=$offset_y = 0; 
    if(!is_file($font)) { file_put_contents($font,file_get_contents('https://github.com/edx/edx-certificates/raw/master/template_data/fonts/Arial%20Unicode.ttf')); } 

    // get the font height. 
    $bounds = ImageTTFBBox($size, $rotate, $font, "W"); 
    if ($rotate < 0)  {$font_height = abs($bounds[7]-$bounds[1]); } 
    elseif ($rotate > 0) {$font_height = abs($bounds[1]-$bounds[7]); } 
    else { $font_height = abs($bounds[7]-$bounds[1]);} 
    // determine bounding box. 
    $bounds = ImageTTFBBox($size, $rotate, $font, $text); 
    if ($rotate < 0){  $width = abs($bounds[4]-$bounds[0]);     $height = abs($bounds[3]-$bounds[7]); 
          $offset_y = $font_height;        $offset_x = 0; 
    } 
    elseif ($rotate > 0) { $width = abs($bounds[2]-$bounds[6]);     $height = abs($bounds[1]-$bounds[5]); 
          $offset_y = abs($bounds[7]-$bounds[5])+$font_height; $offset_x = abs($bounds[0]-$bounds[6]); 
    } 
    else{     $width = abs($bounds[4]-$bounds[6]);     $height = abs($bounds[7]-$bounds[1]); 
          $offset_y = $font_height;        $offset_x = 0; 
    } 

    $image = imagecreate($width+($padding*2)+1,$height+($padding*2)+1); 

    $background = ImageColorAllocate($image, $bg_color['red'], $bg_color['grn'], $bg_color['blu']); 
    $foreground = ImageColorAllocate($image, $color['red'], $color['grn'], $color['blu']); 

    if ($transparent) ImageColorTransparent($image, $background); 
    ImageInterlace($image, true); 
    // render the image 
    ImageTTFText($image, $size, $rotate, $offset_x+$padding, $offset_y+$padding, $foreground, $font, $text); 
    imagealphablending($image, true); 
    imagesavealpha($image, true); 
    // output PNG object. 
    imagePNG($image); 
} 
    //======helper function========== 
    if(!function_exists('mb_substr_replace')){ 
     function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = "UTF-8") { 
     if (extension_loaded('mbstring') === true){ 
      $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding); 
      if ($start < 0) { $start = max(0, $string_length + $start); } 
      else if ($start > $string_length) {$start = $string_length; } 
      if ($length < 0){ $length = max(0, $string_length - $start + $length); } 
      else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; } 
      if (($start + $length) > $string_length){$length = $string_length - $start;} 
      if (is_null($encoding) === true) { return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); } 
      return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding); 
     } 
     return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length); 
     } 
    } 


溶液2(手動大小輸出):

(非常簡單,但你應該設置寬度&高度的輸出 - 更長字符串將被切除)..

<?php 
$text = "YOUR texttttttttttttttt"; 

$my_img = imagecreate(200, 80);        //width & height 
$background = imagecolorallocate($my_img, 0, 0, 255); 
$text_colour = imagecolorallocate($my_img, 255, 255, 0); 
$line_colour = imagecolorallocate($my_img, 128, 255, 0); 
imagestring($my_img, 4, 30, 25, $text, $text_colour); 
imagesetthickness ($my_img, 5); 
imageline($my_img, 30, 45, 165, 45, $line_colour); 

header("Content-type: image/png"); 
imagepng($my_img); 
imagecolordeallocate($line_color); 
imagecolordeallocate($text_color); 
imagecolordeallocate($background); 
imagedestroy($my_img); 
?> 
+1

感謝您的自動調整解決方案:) – 2016-09-02 15:05:19

+0

有一個問題......是否有可能在輸入文本中斷行? – 2017-08-03 12:20:33

+0

解決方案1非常棒! – JohnA10 2017-11-16 18:42:39