0

所以我找到了这个脚本,并对其进行了编辑。它还没有完成,需要从中删除一些东西,但它所做的是从 html 创建图片。我不知道如何让它接受除 png 以外的其他图像类型。它只是抛出一个错误。我已经尝试了我能想到的一切,除了有些复杂的 if 语句,这些语句也可能最终会出错。关于如何使它接受 jpeg、png 甚至 gif 的任何想法?

$_GET['text']  = $row_recordset['Text'];
$_GET['text2']  = $row_recordset['Text2'];
$_GET['text3']  = $row_recordset['Text3'];
$_GET['text4']  = $row_recordset['Text4'];
$font_file     = $row_recordset['textFont'];
$image_file    = $row_recordset['Picture'];

$font_size = $row_recordset['textSize'];
$font_color = $row_recordset['textColor'];

if(empty($_GET['text']))    fatal_error('Error: You did not add any text. Hit the back button to finish it.') ;
$text = html_entity_decode($_GET['text']);
$text2 = html_entity_decode($_GET['text2']);
$text3 = html_entity_decode($_GET['text3']);
$text4 = html_entity_decode($_GET['text4']);
//$text = floatval($text);
//$text = number_format($text, 1, '.', '') . '%';

if(empty($text))
    fatal_error('Error: There was an error with the text font. Hit the back button and continue.') ;


$x_finalpos     = 775;
$y_finalpos     = 55;
$x_finalpos2     = 775;
$y_finalpos2     = 400;
$x_finalpos3     = 775;
$y_finalpos3     = 105;
$x_finalpos4     = 775;
$y_finalpos4     = 450;



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


if(!function_exists('ImageCreate'))
    fatal_error('Error: Whoops, there was an error finishing up your e-card. Hit the back button and try again.') ;


if(!is_readable($font_file)) {
    fatal_error('Error: There was an error with the text font. Hit the back button and retry.') ;
}


$font_rgb = hex_to_rgb($font_color) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
$box2 = @ImageTTFBBox($font_size,0,$font_file,$text2) ;
$box3 = @ImageTTFBBox($font_size,0,$font_file,$text3) ;
$box4 = @ImageTTFBBox($font_size,0,$font_file,$text4) ;

$text_width = abs($box[2]-$box[0]);
$text_height = abs($box[5]-$box[3]);
$text_width2 = abs($box2[2]-$box2[0]);
$text_height2 = abs($box2[5]-$box2[3]);
$text_width3 = abs($box3[2]-$box3[0]);
$text_height3 = abs($box3[5]-$box3[3]);
$text_width4 = abs($box4[2]-$box4[0]);
$text_height4 = abs($box4[5]-$box4[3]);

$image =  imagecreatefrompng($image_file);
if(!$image || !$box)
{
    fatal_error('Error: There was an error with the text font. Hit the back button and retry.') ;
}


$font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;
$image_width = imagesx($image);
$put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);
$put_text_y = $y_finalpos;

    $put_text_x2 = $image_width - $text_width2 - ($image_width - $x_finalpos2);
$put_text_y2 = $y_finalpos2;
$put_text_x3 = $image_width - $text_width3 - ($image_width - $x_finalpos3);
$put_text_y3 = $y_finalpos3;
$put_text_x4 = $image_width - $text_width4 - ($image_width - $x_finalpos4);
$put_text_y4 = $y_finalpos4;


imagettftext($image, $font_size, 0, $put_text_x,  $put_text_y, $font_color, $font_file, $text);
    imagettftext($image, $font_size, 0, $put_text_x2,  $put_text_y2, $font_color, $font_file, $text2);
    imagettftext($image, $font_size, 0, $put_text_x3,  $put_text_y3, $font_color, $font_file, $text3);
    imagettftext($image, $font_size, 0, $put_text_x4,  $put_text_y4, $font_color, $font_file, $text4);

header("Content-type: $mime_type");
ImagePNG($image) ;
ImageDestroy($image) ;
exit ;


function fatal_error($message)
{

    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 ;
        }
    }


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



function hex_to_rgb($hex) {
    // remove '#'
    if(substr($hex,0,1) == '#')
        $hex = substr($hex,1) ;


    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.'"') ;


    $rgb['red'] = hexdec(substr($hex,0,2)) ;
    $rgb['green'] = hexdec(substr($hex,2,2)) ;
    $rgb['blue'] = hexdec(substr($hex,4,2)) ;

    return $rgb ;
}
4

0 回答 0