While developing I faced a problem. After calling imagedestroy my script won't execute some PHP nor HTML.
When I delete the header it execute the PHP / HTML after the imagedestroy, but I need that header in my script. So my question is; how can a PHP header affect the PHP script.
<?php
header('Content-Type: image/png;');
$im = #imagecreatefrompng('ticket.png') or die("Cannot select the correct image. Please contact the webmaster.");
$text_color = imagecolorallocate($im, 0,0,0);
/*
$name = $_GET['name'];
$from = $_GET['from'];
$to = $_GET['to'];
$time = $_GET['time'];
$date = $_GET['date'];
$agent = $_GET['agent'];
$sno = $_GET['sno'];
$flightno = $_GET['flightno'];
$boardingtime = $_GET['boardingtime'];
$gate = $_GET['gate'];
$seat = $_GET['seat'];
*/
$name = $_POST['name'];
$from = $_POST['from'];
$to = $_POST['to'];
$time = $_POST['time'];
$date = $_POST['date'];
$agent = $_POST['agent'];
$sno = rand(101, 199);
$flightno = $_POST['flightno'];
$boardingtime = $_POST['boardingtime'];
$gate = $_POST['gate'];
$seat = $_POST['seat'];
$text_name = "$name";
$text_from = "$from";
$text_to = "$to";
$text_time = "$time";
$text_date = "$date";
$text_agent = "$agent";
$text_sno = "$sno";
$text_flightno = "$flightno";
$text_boardingtime = "$boardingtime";
$text_gate = "$gate";
$text_seat = "$seat";
$font = 'font.ttf';
#Basis in het midden.
imagettftext($im, 12, 0, 119, 168, $text_color, $font, $text_name);
imagettftext($im, 12, 0, 119, 184, $text_color, $font, $text_from);
imagettftext($im, 12, 0, 100, 201, $text_color, $font, $text_to);
imagettftext($im, 12, 0, 185, 235, $text_color, $font, $text_time);
imagettftext($im, 12, 0, 498, 167, $text_color, $font, $text_date);
imagettftext($im, 12, 0, 509, 184, $text_color, $font, $text_agent);
imagettftext($im, 21, 0, 544, 260, $text_color, $font, $text_sno);
#Top
imagettftext($im, 14, 0, 97, 85, $text_color, $font, $text_flightno);
imagettftext($im, 14, 0, 289, 85, $text_color, $font, $text_boardingtime);
imagettftext($im, 14, 0, 398, 85, $text_color, $font, $text_gate);
imagettftext($im, 14, 0, 486, 85, $text_color, $font, $text_seat);
$rand = rand(0, 3498);
imagepng($im);
imagepng($im, 'images/' . $rand . '.png');
imagedestroy($im);
echo 'This does not display';
?>
I have a feeling that the issue is that the browser doesn't know how to handle the data it's receiving. According to the docs, if you do not supply a path to imagepng() it will output the data to the browser. If you have not specified the correct headers, it should output as text. If you have, it should output as an image. If you provide the headers that tell the browser to treat it as an image, the image parser will probably discard the extra characters 'This does not display' that are appended to the end of the image data.
Try getting rid of imagepng($im) and watch as it saves the image then outputs 'This does not display'.
http://us2.php.net/imagepng
Related
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:
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);
?>
I really searched a long time on google/stackoverflow and didn't find my answer.
I'm trying to put an image to see if my Terraria server is online or not and if yes, who's connected etc.. :p I'm really noob in php but I'm trying to learn! I got this source code from geek85 on tshock.co . Here's the source :
<?php
error_reporting(E_ERROR);
//variables
$ip = 'XXX.XXX.XXX.XXX';
$port = '7878'; //port of rest server, not terraria server
$json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/status/'), true);
header('Content-Type: image/png');
$im = #imagecreatefrompng('terraria_online.png');
$font = 'visitor1.ttf';
//$font2 = 'fonts/visitor2.ttf';
$red = imagecolorallocate($im, 255, 24, 24);
$blue = imagecolorallocate($im, 50,50,255);
$black = imagecolorallocate($im, 10,10,10);
$purple = imagecolorallocate($im, 127,0,127);
$grey = imagecolorallocate($im, 50, 50, 50);
$white = imagecolorallocate($im, 255, 255, 255);
//server found and running
if ($json['status'] == '200')
{
// text in the grey box
$playerarray = explode(', ',$json['players']);
imagettftext($im, 18, 0, 31, 16, $grey, $font, $json['name']);
imagettftext($im, 18, 0, 30, 15, $red, $font, $json['name']);
imagettftext($im, 14, 0, 31, 34, $grey, $font, 'IP: '.$ip);
imagettftext($im, 14, 0, 30, 33, $black, $font, 'IP: '.$ip);
imagettftext($im, 14, 0, 31, 49, $grey, $font, 'Port: '.$json['port']);
imagettftext($im, 14, 0, 30, 48, $black, $font, 'Port: '.$json['port']);
imagettftext($im, 14, 0, 9, 71, $grey, $font, 'Joueurs en ligne: '.$json['playercount']);
imagettftext($im, 14, 0, 8, 70, $blue, $font, 'Joueurs en ligne: '.$json['playercount']);
// show VIPs' avatar
if ($json['playercount'] > 0)
{
//array of VIP players - (next step : a txt file with names and coordinates to parse...)
$vip = array('QcSeb','Marlin','Twingo','Gaston');
$num = count($vip);
for ($i=0; $i<$num; $i++)
{
if (in_array($vip[$i], $playerarray))
{
$player = #imagecreatefrompng('Images/'.strtolower($vip[$i]).'.png');//This line doesn't work for now
$long = imageSX($player);
$haut = imageSY($player);
switch ($vip[$i])
{
//here, it's a bit difficult: these are the coordinate where to place VIP players in the image.
case 'QcSeb':
$x = 350;
$y = 133;
break;
case 'Gaston':
$x = 300;
$y = 68;
break;
case 'Marlin':
$x = 261;
$y = 68;
break;
case 'Twingo':
$x = 307;
$y = 133;
break;
}
$ok = imagecopy($im,$player,$x,$y-$haut,0,0,$long,$haut);
}
}
// name of players (VIP or not)
$i = -1;
$j = 0;
// since my server only accept 8 players, I don't have any overflow control on the number of player's name to write but this code can do the work for any number of player (< to width of image / 75)
foreach( $playerarray as $player )
{
if ($i++ >= 6)
{
$i = 0;
$j++;
}
imagettftext($im, 11, 0, 11+($j*75), 101+($i*15), $grey, $font, $player);
imagettftext($im, 11, 0, 10+($j*75), 100+($i*15), $purple, $font, $player);
}
}
}
// offline or not "statut == 200"
else
{
$im = #imagecreatefrompng('terraria_offline.png');
imagettftext($im, 18, 0, 11, 31, $purple, $font, 'IP:'.$ip.':'.$port);
imagettftext($im, 18, 0, 10, 30, $grey, $font, 'IP:'.$ip.':'.$port);
imagettftext($im, 42, 17, 16, 131, $purple, $font, 'offline');
imagettftext($im, 42, 17, 15, 130, $black, $font, 'offline');
}
imagepng($im);
imagedestroy($im);
?>
I'm using 000webhost : sebserver.comuf.com (/ServerStatus1.php).
When I go on
http://XXX.XXX.XXX.XXX:7878/status/ I get :
{
"status": "200",
"name": "TShock Server",
"port": "7777",
"playercount": "1",
"players": "QcSeb"
}
Because only me is on the server actually.
(XXX.XXX.XXX.XXX = My hidden IP :P)
Here's an example of what I'm trying to do : http://renagadez.square7.de/gen.php?ip=111.111.111.111&port=1111
Thanks for your help in advance!
I am trying to create a PHP image, although I have come across some problems.
I am trying to add a URL to the php file. The url (global.php) contains information to grab the Username of a specific user and input certain image files into the php image.
But whenever I add the global.php link, the image won't show up.
My main question is, am I able to add links to files, plus add things like echo ' ' ; to the php image file?
<?php
// MySQL Connect & Select
mysql_connect('localhost', 'root', 'password') or die('Error connecting to mysql');
mysql_select_db('database');
$result = mysql_query("SELECT * FROM users WHERE username = '".stripslashes(trim(htmlspecialchars(mysql_real_escape_string(mysql_escape_string($_GET['user'])))))."'");
$row = mysql_fetch_array($result);
$result_stats = mysql_query("SELECT * FROM user_stats WHERE id = '".$row['id']."'") or die("USER_STATS: ".mysql_error());
$row_stats = mysql_fetch_array($result_stats);
$result_info = mysql_query("SELECT * FROM user_info WHERE user_id = '".$row['id']."'") or die("USER_INFO: ".mysql_error());
$row_info = mysql_fetch_array($result_info);
$text[1] = "Hiya, ".$row['username']."!";
$text[2] = "Your last login was ".#date("d-M-Y\n H:i",$row['last_online']).". We missed you :)";
$text[3] = "Do you know the Rare! Way";
$text[4] = "".$row['username']."?";
$text[5] = "I do!";
$text[6] = "Be sure to follow the Rare! Way. \n \n When you follow the Way, you're giving \n yourself the best experience on Rare! that you \n can ever get! Stay tuned for more.";
// Colors
$image = imagecreatefrompng('userbanner/hotel.png');
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 250, 250, 250);
$grey = imagecolorallocate($image, 77, 77, 77);
// Font
$volterB = 'userbanner/Ubuntu-B.ttf';
$volder = 'userbanner/Ubuntu-M.ttf';
$conden = 'userbanner/Ubuntu-L.ttf';
$light = 'userbanner/Ubuntu-Regular.ttf';
$userimage1 = imagecreatefrompng("http://www.habbo.com/habbo-imaging/avatarimage?figure=".$row['look']."&direction=2&head_direction=2&gesture=spk&img_format=png");
imagecopy($image, $userimage1, 420, 288, 0, 0, 64, 110);
$userimage = imagecreatefrompng("http://www.habbo.com/habbo-imaging/avatarimage?figure=".$row['look']."&direction=2&head_direction=2&gesture=sml&img_format=png");
imagecopy($image, $userimage, 220, 628, 0, 0, 64, 110);
// Create image from text
imagettftext($image, 15, 0, 20, 465, $black, $volterB, $text[1]);
imagettftext($image, 8, 0, 20, 483, $black, $conden, $text[2]);
imagettftext($image, 18, 0, 510, 300, $black, $volterB, $text[3]);
imagettftext($image, 15, 0, 510, 330, $black, $volterB, $text[4]);
imagettftext($image, 15, 0, 345, 333, $black, $volder, $text[5]);
imagettftext($image, 10, 0, 520, 360, $grey, $light, $text[6]);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
If I've been unclear in any way, please let me know. Thanks :)
I have a jquery form that posts to a callback page. The callback page uses PHP's GD library to create an image.
The PHP code looks like this:
<?php
include 'inc/config.php';
$caseNum = $_POST['caseNum'];
$wish = $_POST['wish'];
$selectedWishes = join(", ", $wish);
$fname = $_POST['fname'];
$middlename = $_POST['middlei'];
$lname = $_POST['lname'];
$address1 = $_POST['adr1'];
$address2 = $_POST['adr2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$recipt = $_POST['recipt'];
$terms_accepted = $_POST['tou'];
if ($terms_accepted == 'accept') {
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_database);
$select = mysql_query('SELECT * FROM children WHERE caseNumber="'.$caseNum.'" LIMIT 1 ');
$row = mysql_fetch_array($select);
header('Content-type: image/png');
$im = imagecreatefrompng ("images/card_bg.png");
$color = imagecolorallocate($im, 0, 0, 0);
$font = 'inc/fonts/MyriadPro-Regular.otf';
$size = 14;
imagettftext($im, $size, 0, 130, 18, $color, $font, $caseNum);
imagettftext($im, $size, 0, 52, 105, $color, $font, $row['age']);
imagettftext($im, $size, 0, 108, 167, $color, $font, $row['name']);
// Print wishes and word wrap
$lines = explode('|', wordwrap($selectedWishes, 30, '|'));
$y = 238;
foreach ($lines as $line) {
imagettftext($im, $size, 0, 18, $y, $color, $font, $line);
$y += 23;
}
imagettftext($im, $size, 0, 339, 151, $color, $font, $fname.' '.$middlename.' '.$lname);
imagettftext($im, $size, 0, 351, 175, $color, $font, $address1);
imagettftext($im, $size, 0, 365, 196, $color, $font, $address2);
imagettftext($im, $size, 0, 326, 218, $color, $font, $city);
imagettftext($im, $size, 0, 474, 218, $color, $font, $zip);
imagettftext($im, $size, 0, 340, 242, $color, $font, $phone);
imagettftext($im, 9, 0, 333, 268, $color, $font, $email);
if ($row['gender'] == 'male') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 74, 3, 0, 0, 10, 15, 80);
}
else if ($row['gender'] == 'female') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 74, 38, 0, 0, 10, 15, 80);
}
if ($recipt == 'yes') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 324, 383, 0, 0, 10, 15, 80);
}
else if ($recipt == 'no') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 367, 383, 0, 0, 10, 15, 80);
}
$imageName = rand();
$ourFileName = $imageName.'.png';
$ourFilePath = 'images/created_tags/';
imagepng ( $im, $ourFilePath.$ourFileName );
imagepng($im);
imagepng($check);
imagedestroy($check);
imagedestroy($im);
readfile ( $ourFileName );
print $ourFilePath.$ourFileName;
exit ();
}
?>
and here is the jQuery:
$.post('selectChildCallback.php', $("#select_child_form").serialize(), function(data) {
$("#post_image").append('<img src="'+ data.path +'" alt="card" />');
}, 'json');
What I can't figure out is how to make the callback page not display the image, but just the path. Right now it prints the image data instead of an image path.
readfile ( $ourFileName );
This line is the problem. According to the docs, it echos the contents of a file to the screen. So PHP is returning a image file (binary) with its file name concatenated to the end of the stream back to jQuery.
Remove this line, and this should work.
EDIT:
imagepng ( $im, $ourFilePath.$ourFileName );
imagepng($im);
imagepng($check);
Why do you call imagepng 3 times? If you don't give it a second parameter, it echos the image to the screen. Also remove the last 2 imagepng lines. PHP is actually returning 3 images concatenated together as well as the file name.
It should look like this:
$imageName = rand();
$ourFileName = $imageName.'.png';
$ourFilePath = 'images/created_tags/';
imagepng($im, $ourFilePath.$ourFileName);
imagedestroy($check);
imagedestroy($im);
echo $ourFilePath.$ourFileName;
exit();