2012-05-26 131 views
0

我正在从现有的PNG生成一个简单的图像,并添加文本。但质量非常差,结果很糟糕,请参见here。结果不一定是超高质量,只是比它好一点。我一定在做错事,因为这很糟糕。任何建议都会很棒。php GD图像质量差

非常感谢你,

托德

// customizable variables 
    $common_font_file = 'fonts/giddyup.ttf'; 
    $latin_font_file = 'fonts/comic-sans-ms.ttf'; 
    $font_color  = '#000000' ; 
    $image_file  = 'images/oval_template_large.png'; 
    $mime_type   = 'image/png' ; 
    $extension   = '.png' ; 
    $s_end_buffer_size = 4096 ; 

    $common_name = html_entity_decode($_POST["Cname"]); 
    $latin_name = html_entity_decode($_POST["Lname"]); 
    $common_left_indent = html_entity_decode($_POST["cleftIndent"]); 
    $latin_left_indent = html_entity_decode($_POST["lleftIndent"]); 
    $common_top_indent = html_entity_decode($_POST["ctopIndent"]); 
    $latin_top_indent = html_entity_decode($_POST["ltopIndent"]); 
    $common_font_size = html_entity_decode($_POST["cFontSize"])* 72/96; 
    $latin_font_size = html_entity_decode($_POST["lFontSize"])* 72/96; 

    $font_rgb = hex_to_rgb($font_color) ; 

    // create and measure the common text 
    $common_box = @ImageTTFBBox($common_font_size,0,$common_font_file,$common_name) ; 
    $common_text_width = abs($common_box[2]-$common_box[0]); 
    $common_text_height = abs($common_box[5]-$common_box[3]); 

    // create and measure the latin text 
    $latin_box = @ImageTTFBBox($latin_font_size,0,$latin_font_file,$latin_name) ; 
    $latin_text_width = abs($latin_box[2]-$latin_box[0]); 
    $latin_text_height = abs($latin_box[5]-$latin_box[3]); 

    $image = imagecreatefrompng($image_file); 

    if(!$image || (!$common_box && !$latin_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_common_text_x = $common_left_indent; 
    $put_common_text_y = $common_top_indent + $common_text_height; 

    $put_latin_text_x = $latin_left_indent; 
    $put_latin_text_y = $latin_text_height + $latin_top_indent ; 


    // Write the text 
    imagettftext($image, $common_font_size, 0, $put_common_text_x, $put_common_text_y, $font_color, $common_font_file, $common_name); 
    // Write the text 
    imagettftext($image, $latin_font_size, 0, $put_latin_text_x, $put_latin_text_y, $font_color, $latin_font_file, $latin_name); 


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

    ImageDestroy($image) ; 
    exit ; 
+0

我添加了一个函数来水平镜像图像,质量好得多。 – maddogandnoriko

回答

2

GD的文本函数不抗锯齿他们正在绘制的文本。解决这个问题的最简单方法是让图像的尺寸至少比最终产品大2倍或4倍,稍后再进行调整大小,作为调整大小的副作用。

0

也许您需要增加原始的'椭圆'图像,然后在该图像上渲染.ttf并在输出上呈现较小的版本。