使用PHP创建映像,似乎只接受png文件

时间:2013-05-25 11:43:04

标签: php image png jpeg

所以我找到了这个脚本,然后我编辑了它。它还没有完成,并且需要从中删除内容,但它的作用是从html创建一个图片。我无法弄清楚如何让它接受除png之外的其他图像类型。它只是抛出一个错误。我已经尝试了所有我能想到的东西,如果陈述的话有些复杂,那么这些陈述可能也会导致错误。关于如何接受jpeg,png甚至gifs的任何想法?

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

0 个答案:

没有答案