回答
我認爲你可以做到這一點使用PHP GD庫:http://php.net/manual/en/ref.image.php
爲什麼它不是在評論? – NaveenDA 2017-06-01 09:57:53
是的,它很可能,你要遵循同樣的技術我們在生成驗證碼圖像時執行此操作。
要求: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);
?>
$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);
然後,您也可以前往 顯示圖像我認爲是正確的,一段時間以來我已經做到了這一點。
文本轉換成圖像(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);
?>
感謝您的自動調整解決方案:) – 2016-09-02 15:05:19
有一個問題......是否有可能在輸入文本中斷行? – 2017-08-03 12:20:33
解決方案1非常棒! – JohnA10 2017-11-16 18:42:39
- 1. 將文本轉換爲圖像 - PHP/GD - 保存圖像
- 2. SPen將圖像轉換爲文本
- 3. 將文本轉換爲圖像
- 4. 將動態文本轉換爲圖像
- 5. 將圖像轉換爲文本 - Java
- 6. ABCpdf將文本轉換爲圖像
- 7. WPF將圖像轉換爲文本
- 8. 如何將文本轉換爲圖像?
- 9. 將圖像轉換爲文本
- 10. 如何將文本轉換爲圖像?
- 11. 將文本轉換爲圖像
- 12. 使用php腳本將文本轉換爲圖像
- 13. PHP將HTML轉換爲PHP圖像
- 14. 使用php將文本轉換爲圖像
- 15. 如何將文本轉換爲使用php的imagick的圖像?
- 16. 通過Javascript或PHP將圖像轉換爲文本?
- 17. 如何將文本圖像轉換爲字轉換器?
- 18. 如何將圖像轉換爲android中的文本轉換器?
- 19. 將文本從圖像轉換爲文本文件
- 20. 圖像識別將圖像轉換爲android中的文本
- 21. 將PDF轉換爲php中的圖像
- 22. PHP:將blob內容轉換爲圖像
- 23. 用PHP將圖像轉換爲數字?
- 24. PHP將POST圖像轉換爲PNG
- 25. PHP將PDF轉換爲圖像-dUseCropBox
- 26. 將圖像轉換爲PDF php
- 27. 將包含文本的圖像轉換爲可編輯文本
- 28. 將圖像轉換爲文本形式(壓縮),然後從文本轉換爲圖像在Android
- 29. 圖像轉換爲Base64 PHP
- 30. 將本地圖像轉換爲base64 javascript
你可以試試這個:http://www.daftlogic.com/projects-text-to-image.htm – 2015-10-12 23:05:10