I know that the German umlauts output with PHP, can get fixed by using the
header('Content-Type: text/html; charset=utf-8');
at the top of the PHP as shown below.
<?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">';
?>
But there is trouble displaying them on an imagepng code shown below for your review and test.
File: 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);
?>
Files/Links:
screen_shot
temp_card
Updated working image.php:
Thanks to Bernard for his guiding comment.
<?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);
Hope it helps others as well.
Screenshot:
Related
Still on imagettftext topic, now I have question how to display blob image on the page that created from imagecreatefrompng.
<?php
include('../db.php');
$id = $_GET['id'];
$sql = "SELECT * FROM data WHERE id=$id";
$result = $conn->query($sql);
$row = $result->fetch_array();
$id = $row['id']; //varchar
$name = $row['name']; //varchar
$address = $row['address']; //varchar
$photo = base64_encode($row['photo']); //blob image
$im = imagecreatefrompng('../img/idcard.png');
$black = imagecolorallocate($im, 0, 0, 0);
$font = "../fonts/Ubuntu-R.ttf";
imagettftext($im, 15, 0, 200, 175, $black, $font, $id);
imagettftext($im, 15, 0, 240, 200, $black, $font, $name);
imagettftext($im, 15, 0, 280, 275, $black, $font, $address);
imagettftext($im, 15, 0, 350, 315, $black, $font, $photo);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
My code display id card, text, but image/photo just display as text character
$im2 = imagecreatefromstring($photo);
imagecopyresized ($im,$im2,2,2,0,0,46,46,183,173);//see parameters at: http://php.net/manual/en/function.imagecopyresized.php
(if You not need resizing You can use imagecopy too)
<?php
$dest = imagecreatefrompng($im);
$src = imagecreatefromjpeg('profile_pic.jpg');
imagecopymerge($dest, $src, 250, 650, 0, 0, imagesx($src), imagesx($src), 100);
imagecopyresampled($dest, $src, 250, 650, 0, 0, 577, 540, imagesx($src), imagesy($src));
imagejpeg($dest, $new_image_name, 100);
imagedestroy($dest);
?>
background is comming in white i want to remove white and put transparent.please help
this is an image: http://funbusy.com/fbtest/user_image.php
?php
// Create the image
$im = imagecreatetruecolor(400, 25);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 22, 125, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = ucwords('tanuja sree');
// Replace path by your own font path
$font = 'OpenSans-Italic.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
ob_start();
imagepng($im);
$image_data = ob_get_clean();
$image_code = '<img id="image_code" src="data:image/png;base64,'. base64_encode($image_data) . '">';
imagedestroy($im);
?>
Try adding this piece of code after imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagecolortransparent($im, $white);
imagesavealpha($im,true);
imagealphablending($im, true);
I have this script which generates an image:
$im = imagecreatetruecolor(110, 34);
$red = imagecolorallocate($im, 245, 245, 245);
imagefill($im, 0, 0, $red);
$text_color = imagecolorallocate($im, 80, 80, 80);
imagestring($im, 8, 15, 9, image, $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
Now I want to save that image. How can I do that? Here is what I have tried so far:
file_put_contents("../img/name.jpg", file_get_contents($im));
What's wrong?
I have this PHP code:
<?php
//Other part of code
Header("Content-type: image/png");
$im = imagecreatefromPng("./images/signatures/background.png");
$red = ImageColorAllocate($im, 255, 0, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
ImageString($im, 5, 15, 5, "$callsign", $black);
ImageString($im, 5, 15, 20, "$name $surname", $black);
ImageString($im, 5, 15, 35, "Location: $location", $black);
ImageString($im, 5, 15, 50, "HUB: $hub", $black);
ImageString($im, 5, 15, 65, "Hours: $hours", $black);
$font_width = ImageFontWidth(5);
ImagePng($im);
?>
I want to change the font that PHP uses to write in the image. How can i do that?? I try but I can´t.
Assuming that you mean font:
http://www.php.net/manual/en/function.imageloadfont.php
http://php.net/manual/en/function.imagettftext.php this may help if your using ttf fonts
You can use imageloadfont ();
syntax:
int imageloadfont ( string $file )
Example:
// Create a new image instance
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background white
imagefilledrectangle($im, 0, 0, 49, 19, $white);
// Load the gd font and write 'Hello'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'Hello', $black);
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
so change your code to:
//Other part of code
Header("Content-type: image/png");
$im = imagecreatefromPng("./images/signatures/background.png");
$font = imageloadfont('./fonts/arial.gdf');//change the parameter based on your font file name
$red = ImageColorAllocate($im, 255, 0, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
ImageString($im, $font, 15, 5, "$callsign", $black);
ImageString($im, $font, 15, 20, "$name $surname", $black);
ImageString($im, $font, 15, 35, "Location: $location", $black);
ImageString($im, $font, 15, 50, "HUB: $hub", $black);
ImageString($im, $font, 15, 65, "Hours: $hours", $black);
$font_width = ImageFontWidth(5);
ImagePng($im);
I'm trying to use the GD graphics library to make this captcha image, and the box shows up how Id like it to, but the text isn't being displayed over the image at all. I can't figure out why it's not showing up.
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.') );
$font = 'Molle';
header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 40);
$white = imagecolorallocate($im, 255, 255,255);
$grey = imagecolorallocate($im, 215, 215, 215);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 3, 3, 255, 34, $grey);
$text = 'Testing';
$_SESSION["captcha"] = $text;
imagettftext( $im, 20, 0, 16, 26, $white, $font, $text );
imagettftext( $im, 20, 0, 15, 25, $black, $font, $text );
imagepng($im);
imagedestroy($im);
?>