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 :)
Related
I am using imagejpg() to display an image on the browser and place text over the image.
I have used this before and it has worked. Now I switched domain hosts and perhaps the PHP version is different as the image is now not displaying.
It displays a small square in the middle of the screen, like a broken link, but the image is definitely there.
Could someone please assist with this?
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
Thank you in advance.
Rob
I just figured it out. Answer below
I moved the header down and it sorted out the problem.
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
header('Content-type: image/jpeg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
I had the same problem, found out that if there is a blank space before the header, the image doesn't work.
I was able to save the image on the server but not to get the browser to display it.
Removing every blank space did the trick for me.
I think it's the same reason why your script didn't work with the header at the beginning.
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'm making my own 'captcha' form and I now have a page that generates the image:
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 160, 160, 160);
imagefilledrectangle($im, 0, 0, 200, 50, $white);
$captcha = "SOMErandomTEXT";
$font = 'Chewy.ttf';
imagettftext($im, 20, 0, 0, 20, $gray, $font, $captcha);
imagepng($im);
imagedestroy($im);
?>
Now I also have another page that shows this image, inside the form. Now I want to get the value $captcha from the page shown above on the other page. How can I do it?
Using sessions did the job for me.
PHP Sessions
I want to move a file that has been created using imagettftext and saved as a png. as you can see, in the code below, i used the move_uploaded_file but to no avail. please help.
tq
// Set the content-type
header('Content-Type: image/png');
// Create the image from created image
//$im = imagecreatetruecolor(180, 180);
$im = #imagecreatefromjpeg('poloroid.jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
//$text = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = '/uploaded_files';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
$name ="$fbid.png";
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
move_uploaded_file($name,"$uploads_dir/$name");
imagedestroy($im);
You are not uploading a file, you are generating one!
imagepng has filename parameter so you can save it to your drive:
$uploads_dir = '/uploaded_files/';
$name = $uploads_dir.$fbid.'.png';
imagepng($im,$name,9);
imagedestroy($im);
try to use rename instead of move_uploaded_file
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 :)