Подтвердить что ты не робот

Конвертировать текст в изображение в php

Я хотел бы создать текстовую строку, взятую из поля формы, а затем преобразовать ее в прозрачный .PNG(alpha BG).

Это возможно с PHP? Если да, не могли бы вы показать мне, как это будет достигнуто.

4b9b3361

Ответ 1

да, это очень возможно, вы будете следовать той же методике, что и при создании изображения captcha.

Требование: библиотека 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);
?> 

Ответ 2

конвертировать текст в изображение (php-код):

сначала вам нужно убедиться, что хостинг разрешил GD-библиотеку (в новом php файле, выполнить phpinfo(); и в выводе см. /find, если включена библиотека 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 );
?> 

Ответ 4

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

Затем вы можете отобразить изображение, пройдя Я думаю, это верно, потому что я сделал это.