德语变音符号与imagepng输出不良

时间:2018-06-04 05:35:03

标签: php image gd

我知道用PHP输出的德语变音符号可以通过

来修复
header('Content-Type: text/html; charset=utf-8');

位于PHP的顶部,如下所示。

<?php
header('Content-Type: text/html; charset=utf-8');

echo "<h2>German umlauts: ÄÖÜäöüß</h2>";

echo "<br />";

echo "<h2>Dr. Jörg Großhaderner</h2>";

echo "<br /><br />";

echo '<img src="image.php">';
?>

但在下面显示的imagepng代码上显示它们时,无法进行审核和测试。

文件: image.php

<?php
header('Content-Type: text/html; charset=utf-8');

$idnum = "ER-CW-R112-DOC1297";
$title = "Dr.";
$firstname = "Jörg";
$lastname = "Großhaderner";
$ward = "Cardiothoracic Ward";
$callcode = "CW894";

// load the image from the file specified as background layout
$im = imagecreatefrompng("images/tempcard1.png");

// if there's an error, stop processing the page:
if(!$im)
{
    die("Error creating the temp card!");
}

// define some colours to use with the fonts
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);

// define the font and some font sizes
$fontsize = 5;

// finally, write the string:
imagestring($im, $fontsize, 130, 105, $idnum , $red);
imagestring($im, $fontsize, 110, 135, $title , $black);
imagestring($im, $fontsize, 140, 135, $firstname , $black);
imagestring($im, $fontsize, 190, 135, $lastname , $black);
imagestring($im, $fontsize, 125, 155, $ward, $black);
imagestring($im, $fontsize, 190, 175, $callcode, $blue);

// output the image
// tell the browser what we're sending it
header('Content-type: image/png; charset=utf-8');

//header('Content-type: image/png;');

// output the image as a png
imagepng($im);

// tidy up
imagedestroy($im);
?>

文件/链接:

screen_shot german_imagepng.jpg

temp_card tempcard1.png

1 个答案:

答案 0 :(得分:0)

更新了工作 image.php

感谢Bernard的指导性评论。

<?php
header('Content-Type: text/html; charset=utf-8');

$idnum = "ER-CW-R112-DOC1297";
$title = "Dr.";
$firstname = "Jörg";
$lastname = "Großhaderner";
$ward = "Cardiothoracic Ward";
$callcode = "CW894";

// load the image from the file specified as background layout
$im = imagecreatefrompng("images/tempcard1.png");

// if there's an error, stop processing the page:
if(!$im)
{
    die("Error creating the temp card!");
}

// define some colours to use with the fonts
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);

// define the font and some font sizes
$font = 'fonts/OpenSans-Bold.ttf'; // <- This is the font supporting German umlauts
$fontsize = 17;

// finally, write the string:
/*
imagestring($im, $fontsize, 130, 105, $idnum , $red);
imagestring($im, $fontsize, 110, 135, $title , $black);
imagestring($im, $fontsize, 140, 135, $firstname , $black);
imagestring($im, $fontsize, 190, 135, $lastname , $black);
imagestring($im, $fontsize, 125, 155, $ward, $black);
imagestring($im, $fontsize, 190, 175, $callcode, $blue);
*/

/** 
*   Changed to imagettftext instead of imagestring as per Bernard's comment and works.
*   https://stackoverflow.com/users/4401439/bernhard
*/

imagettftext($im, $fontsize, 0, 90, 130, $red, $font, $idnum);
imagettftext($im, $fontsize, 0, 85, 160, $black, $font, $title);
imagettftext($im, $fontsize, 0, 125, 160, $black, $font, $firstname);
imagettftext($im, $fontsize, 0, 175, 160, $black, $font, $lastname);
imagettftext($im, $fontsize, 0, 90, 195, $black, $font, $ward);
imagettftext($im, $fontsize, 0, 160, 220, $blue, $font, $callcode);

// output the image
// tell the browser what we're sending it
header('Content-type: image/png;');

// output the image as a png
imagepng($im);

// tidy up
imagedestroy($im);

希望它也有助于其他人。

截图: german.jpg

相关问题