PHP creating a png image - php

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 :)

Related

How to display blob image combined with imagettftext

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);
?>

imagefttext get text from database

I want to make ID card for members that registered on my web. After user register they can click print and ID card generated (png or jpg). I use PHP function imagettftext but it's not working. I don't know where is wrong code. My code:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/ektp/header.php');//Database config and session login
$id = $_GET['id'];
$sql = "SELECT * FROM data WHERE noktp=$id";
$result = $conn->query($sql);
$row = $result->fetch_array();
$noid = $row['id'];
$name = $row['name'];
$addr = $row['address'];
$im = imagecreatefrompng('./ektp.png');
$black = imagecolorallocate($im, 0, 0, 0);
$font = "./Ubuntu-R.ttf";
imagettftext($im, 15, 0, 200, 35, $black, $font, $noid);
imagettftext($im, 15, 0, 200, 65, $black, $font, $name);
imagettftext($im, 15, 0, 200, 85, $black, $font, $addr);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
Solved by self, my problem is
include($_SERVER['DOCUMENT_ROOT'].'/ektp/header.php');
Work for several file but not working with imagettftext, so I change to
include('../header.php')
and I have some html elements in header.php like
<style>
<script>
so I split to another file. In header.php just database config and..
Image displayed with text from database :)

Working with Multiline Texts on imagettftext

My code is here, Im trying to create png widget for a website. I do correct others but when im trying to fit my long text in to my cards 181*312 px
I must fit my text in to text area, and its impossible to change my widget sizes
So how can wrap my text?
$kartquery = mysql_query("SELECT * FROM status");
While($kartqueryyaz=mysql_fetch_array($kartquery)){
// Q & A
$id = $kartqueryyaz['id'];
$longdesc = $kartqueryyaz['longdesc'];
$correct = $kartqueryyaz['correct'];
$wrong1 = $kartqueryyaz['wrong1'];
$wrong2 = $kartqueryyaz['wrong2'];
$wrong3 = $kartqueryyaz['wrong3'];
$wrong4 = $kartqueryyaz['wrong4'];
// carttype
$png_image = imagecreatefrompng('card1.png');
// Text Color #white ffffff
$white = imagecolorallocate($png_image, 255, 255, 255);
// Font
$font_path = 'GothamBook.ttf';
$font_pathdogru = 'GothamRoundedBold.ttf';
imagettftext($png_image, 25, 0, 55, 230, $white, $font_path, $longdesc);
imagettftext($png_image, 25, 0, 150, 730, $white, $font_pathdogru, $correct);
imagettftext($png_image, 25, 0, 150, 850, $white, $font_path, $wrong1);
imagettftext($png_image, 25, 0, 150, 970, $white, $font_path, $wrong2);
imagettftext($png_image, 25, 0, 150, 1100, $white, $font_path, $wrong3);
imagettftext($png_image, 25, 0, 150, 1220, $white, $font_path, $wrong4);
//imagepng($png_image);
imagepng($png_image, "img/".$id.".png");
imagedestroy($png_image);
}
?>

Resize image before using in imagecopymerge

I have a prorgam that uses imagecopymerge. Here, I'm using two images. First image for background and second one to be placed inside it. I am able to do so, but now I want the second image to be on a certain size. this image is uploaded by user and saved at my server so i dont have control on it's size. How can I resize the second image before using it in my imagecopymerge? Thanks a lot for answering. Please also consider that resizing it during upload is my least option. I want the size to be 255x175.
This is what I have:
$uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])
$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
$newtitle = wordwrap($title, 28, "\n", true);
$newertitle = explode("\n", $newtitle);
imagettftext($im, 28, 0, 7, 270, $black, $font, $newertitle[0]);
imagettftext($im, 28, 0, 7, 320, $black, $font, $newertitle[1]);
imagettftext($im, 10, 0, 320, 362, $black, $font, $namehere);
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
imagedestroy($im);
http://php.net/manual/en/function.imagecopyresampled.php
Take your code...
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
And change it to...
imagecopyresampled($im, $img2, 10, 350, 0, 0, 255, 175, imagesx($img2), imagesy($img2));
Just simply use php function
given as:
$img = imagecreatefromjpeg("source_of_img.jpg");
$imgresize = imagescale($dp,200,200);

PHP don't respond after imagedestroy

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

Categories