Немецкий умлаутс с imagepng плохой вывод - PullRequest
0 голосов
/ 04 июня 2018

Я знаю, что вывод umlauts на немецком языке с помощью 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);
?>

Файлы / ссылки:

снимок экрана german_imagepng.jpg

temp_card tempcard1.png

1 Ответ

0 голосов
/ 04 июня 2018

Обновлен рабочий 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
$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

...