php - 无法在图像中环绕文字

标签 php image text

我创建了一个 PHP 脚本,您可以在 HTML 输入框中输入文本,然后它会生成一个带有背景图像和特定位置文本的图像。我发现这段代码可以将文本换行为 HTML,但我如何让它在 30 个字符后创建一个新行 on the image

HTML 换行符的代码是:

$dir = html_entity_decode($_POST['sig']).' ' ;  
$sig = wordwrap($dir , 30, "<br />", true);

图片代码为:

<?php   

// clean up the input
if(empty($_GET['date']))    fatal_error('Error: No text specified.') ;


$date = Date("F d, Y, h:i A", $now);
$name = html_entity_decode($_GET['pname']).' ' ;
$email = html_entity_decode($_GET['dob']).' ' ;


if(empty($date))

    fatal_error('Error: Text not properly formatted.') ;


// customizable variables
$font_file      = 'Muli-Regular.ttf';
$font_size      = 10 ; // font size in pts
$font_color     = '#000' ;
$image_file     = 'background.png';

// x and y for the bottom right of the text
// so it expands like right aligned text
$x_finalpos     = 340;
$y_finalpos     = 250;

$xpos = 180; 
$ypos = 278;

$xpos1 = 225; 
$ypos1 = 305;


$mime_type          = 'image/png' ;
$extension          = '.png' ;
$s_end_buffer_size  = 4096 ;

// check for GD support
if(!function_exists('ImageCreate'))
    fatal_error('Error: Server does not support PHP image generation') ;

// check font availability;
if(!is_readable($font_file)) {
    fatal_error('Error: The server is missing the specified font.') ;
}

// create and measure the text
$font_rgb = hex_to_rgb($font_color) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$date) ;
$box1 = @ImageTTFBBox($font_size,0,$font_file,$pname) ;
$box2 = @ImageTTFBBox($font_size,0,$font_file,$dob) ;

$date_width = abs($box[2]-$box[0]);
$date_height = abs($box[5]-$box[3]);

$name_width = abs($box1[2]-$box1[0]);
$name_height = abs($box1[5]-$box1[3]);  

$email_width = abs($box2[2]-$box2[0]);
$email_height = abs($box2[5]-$box2[3]);


$medOther_width = abs($box10[2]-$box10[0]);
$medOther_height = abs($box10[5]-$box10[3]);

$image =  imagecreatefrompng($image_file);

if(!$image || !$box)
{
    fatal_error('Error: The server could not create this image.') ;
}

// allocate colors and measure final text position
$font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;

$image_width = imagesx($image);

$put_date_x = $image_width - ($image_width - $x_finalpos);
$put_date_y = $y_finalpos;

$put_name_x = $image_width - ($image_width - $xpos);
$put_name_y = $ypos;

$put_email_x = $image_width - ($image_width - $xpos1);
$put_email_y = $ypos1;


// Write the text
imagettftext($image, $font_size, 0, $put_date_x,  $put_date_y, $font_color, $font_file, $date);
imagettftext($image, $font_size, 0, $put_name_x,  $put_name_y, $font_color, $font_file, $name);
imagettftext($image, $font_size, 0, $put_email_x,  $put_email_y, $font_color, $font_file, $email);


header('Content-type: ' . $mime_type) ;
ImagePNG($image) ;

ImageDestroy($image) ;
exit ;


/*
    attempt to create an image containing the error message given. 
    if this works, the image is sent to the browser. if not, an error
    is logged, and passed back to the browser as a 500 code instead.
*/
function fatal_error($message)
{
    // send an image
    if(function_exists('ImageCreate'))
    {
        $width = ImageFontWidth(5) * strlen($message) + 10 ;
        $height = ImageFontHeight(5) + 10 ;
        if($image = ImageCreate($width,$height))
        {
            $background = ImageColorAllocate($image,255,255,255) ;
            $text_color = ImageColorAllocate($image,0,0,0) ;
            ImageString($image,5,5,5,$message,$text_color) ;    
            header('Content-type: image/png') ;
            ImagePNG($image) ;
            ImageDestroy($image) ;
            exit ;
        }
    }

    // send 500 code
    header("HTTP/1.0 500 Internal Server Error") ;
    print($message) ;
    exit ;
}


/* 
    decode an HTML hex-code into an array of R,G, and B values.
    accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff 
*/    
function hex_to_rgb($hex) {
    // remove '#'
    if(substr($hex,0,1) == '#')
        $hex = substr($hex,1) ;

    // expand short form ('fff') color to long form ('ffffff')
    if(strlen($hex) == 3) {
        $hex = substr($hex,0,1) . substr($hex,0,1) .
               substr($hex,1,1) . substr($hex,1,1) .
               substr($hex,2,1) . substr($hex,2,1) ;
    }

    if(strlen($hex) != 6)
        fatal_error('Error: Invalid color "'.$hex.'"') ;

    // convert from hexidecimal number systems
    $rgb['red'] = hexdec(substr($hex,0,2)) ;
    $rgb['green'] = hexdec(substr($hex,2,2)) ;
    $rgb['blue'] = hexdec(substr($hex,4,2)) ;

    return $rgb ;
}
?>

最佳答案

这很简单,试试这个代码:

<?php
header('Content-type: image/png');
$dir = html_entity_decode($_POST['sig']).' ';
$text_length = 30;
$sig = wordwrap($dir, $text_length, "<br />", true);
$fontsize = 12;
$fontfile = './arial.ttf';

$img = imagecreatetruecolor(500, 500); //there will be opened background image
$c = imagecolorallocate($img, 255, 255, 255);

//Just wrap text by newlines
$text = str_replace('<br />', "\n", $sig);
imagettftext($img, $fontsize, 0, 0, $fontsize+2, $c, $fontfile, $text); // "+2" - height of space-line

imagepng($img);
imagedestroy($img);

当然,你会改变图像的初始坐标和宽度和高度,但在这个例子中你可以看到它是如何工作的:)

关于php - 无法在图像中环绕文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168011/

相关文章:

c - 调用 FreeImage_GetPixelColor() 时出现奇怪的段错误

javascript - 自动缩放图像以匹配文本高度

regex - 使用 Notepad++ 和正则表达式从文本中提取电子邮件

java - 更改文件中的 ID

php - Mysql 搜索将句点视为空格

PHP 和 ??运算符(operator)

php - 从 Codeigniter 导出 CSV 文件

php - 选择列中的最后一个条目与其他列不同

java - 图像不会出现在 jar 中

r - 从字符串中提取允许单词变化的数字