displaying jp2 images stored in mysql database into web page - php

Here 'm trying for displaying jp2 image in a web page.
But I'm not able to converting it.
The simple code is like this
<html>
<body>
<table><tr><td>
<?php
echo ' PHOT:'.getPhotoFromMysqlDB();
?>
</td></tr></table>
</body>
</html?>
getPhotoFromMysqlDB() will return jp2 image.
*If i run above example the web page displaying junk like "bytecode" *

use below code:
<html>
<body>
<table>
<tr><td>
<img src="<?php echo ' PHOT:'.getPhotoFromMysqlDB();?>">
</td></tr>
</table>
</body>
</html>

Related

About PHP QR Code library (part 1)

I am using PHP QR Code library.
Below are my code:
testqr.php
<?php
include('phpqrcode/qrlib.php');
?>
<html>
<body>
<?php
$studentno = 123456;
$image = $studentno.'.png';
QRcode::png($studentno, $image);
?>
<img src="<?php echo $image; ?>" />
</body>
</html>
From the above code, it will display a qr code on the web page and save an image file.
My question is can I just display a qr code on the web page without save an image file?

echo text alongside a generated value PHP

I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>

Print an image in html using session variable from php

i have this code trying to print an image in an html using
session variable from a php.Here is my code:
</head>
<body>
<?php
session_id(1);
session_start();
echo $_SESSION['phname'];
?>
<img src="../uploads/$_SESSION['phname']" alt="photo" width="498" height="720" border="0" />
here:
session_id(1);
session_start();
echo $_SESSION['phname'];
i'm checking if my variable has passed
from the php and its ok.
and here
?>
<img src="../uploads/$_SESSION['phname']" alt="photo" width="498" height="720" border="0" />
i'm using php to print the image from the following source
src="../uploads/$_SESSION['phname']"
when $_SESSION['phname'] is my image's name but i'm not getting the image. Is there something wrong with my code or
is there any other way to print my image?
i try a lot of things and a lot of codes
i found on the net but nothing help me more.
You need to do your session_start before you output anything to the browser as it sends some headers.
And of course when you want to echo something in php, you need php tags and an echo statement...
<?session_start();?>
</head>
<body>
<?php
session_id(1);
echo $_SESSION['phname'];
echo "<img src=\"../uploads/". $_SESSION['phname']."\" alt=\"photo\" width=\"498\" height="720" border="0" />";
?>

PHP file in <img> tag cannot be referenced using query string

i m trying to display an image stored in a database by passing the image_id(an auto increment field) into the query string and then including imagedisplay.php(file used to display the image) in image tag. The problem is that this file, imagedisplay.php is not access ie the control never seems to go into the file. The code for both the main file and the imagedisplay.php file are below. Please note that i m not able to write the head part of the code here due to some formatting issue. The head section is standard html section pre-formatted in Dreamweaver.
<body>
<div class="page shadow-round">
<div id="header">
<div id="logo">
<script type="text/javascript" src="../js/header.js"></script>
</div>
</div>
<div id="menu">
<script type="text/javascript" src="../js/navmenu.js"></script>
<script type="text/javascript">
</script>
</div>
<div class="content overflow" style="height:900px;">
<?php
require_once 'login.php'; //contains the classes for connecting to databases
$dbh=new DB_Mysql; //executing queries
$func=new DB_Mysql_code_functions;
session_start();
if(isset($_SESSION['username']))
{
echo<<<_END
<form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
<table width="990">
<tbody>
<tr><td>Select an image file to be uploaded:</td></tr>
<tr><td><input type="submit" value="UPLOAD" /></td></tr>
</tbody>
</table>
</form>
_END;
if(isset($_FILES['imagefile']['tmp_name']))
{
$imagefile=$_FILES['imagefile']['tmp_name'];
$image_size=$_FILES['imagefile']['size'];
$image_name=addslashes($_FILES['imagefile']['name']);
$image_data = addslashes(file_get_contents($imagefile));
$image_array=getimagesize($imagefile);
$image_type=$image_array['mime'];
$image_height=$image_array[1];
$image_width=$image_array[0];
$maxfilesize=2000000;
if($maxfilesize<$image_size)
{
echo "Please upload a smaller image. The size of the image is too large.";
}
else
{
$query="INSERT INTO gallery(image_name,image_type,image,image_size) VALUES ('". $image_name."','".$image_type."','".$image_data."','".$image_size."')";
$stmt=$dbh->execute($query);
$lastimageid=mysql_insert_id();
echo "<p>You uploaded this image</p>";
echo "<img src='imagedisplay.php?imageid=".$lastimageid."' />";
}
}
}
else
echo "<br/><br/> Your are <span class=\"red\"><b>not Authorized</b></span> to view this page. If you are the Admin, please login with your credentials again. <a href='login_page.php'>Click here to continue</a>";
?>
</div>
</body>
</html>
Now the problem is that the control never goes to imagedisplay.php ie. it fails to reference imagedisplay.php altogether.
the code for imagedisplay.php is below:
<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
$id=$_GET['imageid'];
$query="SELECT * FROM gallery where image_id=".$id;
$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
$imagedata=$row[3];
header("Content-type:image/jpeg");
echo $imagedata;
?>
I have tried all permutation combinations with the quotes, tried echo statements to see if control enters the file....but it does not...it stays in the main file only...i dont understand the reason...please help...please don't worry about SQL injections as i plan to deal with them later, once the code starts working....thanks
Your echo command should be like this:
echo '<img src="imagedisplay.php?imageid=' . $lastimageid . ' " />';
Look at your generated HTML source and make sure it's right. Check your error log to see if you are getting any errors from your image script.

Cannot display image stored in MySQL database after connecting to database through php

I am a complete novice and have just begin working with images in PHP and MySQL database. Here is the problem:
The image is being successfully stored in the database table in BLOB field. I can write that in a file(new.jpg) and display the image. Now here is the twsiter - the imagedisplay.php file i created to display the image on the web page can read the new.jpg file from the folder directory. But as soon as i connect to the database to fetch the image data directly from the database (so that i do not have to include new.jpg in the code), it start showing the error "The image contains errors....."
The code used for uploading the image is
<?php
require_once 'login.php'; //contains the classes for connecting to databases
$dbh=new DB_Mysql; //executing queries
$func=new DB_Mysql_code_functions;
session_start();
if(isset($_SESSION['username']))
{
echo<<<_END
<form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
<table width="990">
<tbody>
<tr><td>Select an image file to be uploaded:</td></tr>
<tr><td><input type="submit" value="UPLOAD" /></td></tr>
</tbody>
</table>
</form>
_END;
if(isset($_FILES['imagefile']['tmp_name']))
{
$imagefile=$_FILES['imagefile']['tmp_name'];
$image_size=$_FILES['imagefile']['size'];
$image_name=addslashes($_FILES['imagefile']['name']);
$image_data = addslashes(file_get_contents($imagefile));
$image_array=getimagesize($imagefile);
$image_type=$image_array['mime'];
$image_height=$image_array[1];
$image_width=$image_array[0];
$maxfilesize=2000000;
if($maxfilesize<$image_size)
{
echo "Please upload a smaller image. The size of the image is too large.";
}
else
{
$query="INSERT INTO allery(image_name,image_type,image,image_size) VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
$stmt=$dbh->execute($query);
$lastimageid=mysql_insert_id();
$query="select * from gallery where mage_id=".$lastimageid;
$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
if(file_exists("new.jpg"))
unlink("new.jpg");
$handle=fopen("new.jpg",'wb');
fwrite($handle,$row[3]);
fclose($handle);
echo "<p>You uploaded this image</p><img src='imagedisplay.php' height=".($image_height/2)." width=".($image_width/2).">";
}
}
}
and the current code for imagedisplay.php file is below and it displays the image fine:
<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("new.jpg");
imagejpeg($image);
imagedestroy($image);
?>
It stops displaying image as soon as i include the connection query in imagedisplay.php
<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("new.jpg");
imagejpeg($image);
imagedestroy($image);
?>
I m stuck on this since days...please help..
OK...so i changed my approach...i m now passing the image_id into the query string and then including imagedisplay.php image tag:
Please note that i m not able to write the head part of the code here due to some formating issue. The head section is standard html section pre formatted in Dreamweaver.
<body>
<div class="page shadow-round">
<div id="header">
<div id="logo">
<script type="text/javascript" src="../js/header.js"></script>
</div>
</div>
<div id="menu">
<script type="text/javascript" src="../js/navmenu.js"></script>
<script type="text/javascript">
</script>
</div>
<div class="content overflow" style="height:900px;">
<?php
require_once 'login.php'; //contains the classes for connecting to databases
$dbh=new DB_Mysql; //executing queries
$func=new DB_Mysql_code_functions;
session_start();
if(isset($_SESSION['username']))
{
echo<<<_END
<form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
<table width="990">
<tbody>
<tr><td>Select an image file to be uploaded:</td></tr>
<tr><td><input type="submit" value="UPLOAD" /></td></tr>
</tbody>
</table>
</form>
_END;
if(isset($_FILES['imagefile']['tmp_name']))
{
$imagefile=$_FILES['imagefile']['tmp_name'];
$image_size=$_FILES['imagefile']['size'];
$image_name=addslashes($_FILES['imagefile']['name']);
$image_data = addslashes(file_get_contents($imagefile));
$image_array=getimagesize($imagefile);
$image_type=$image_array['mime'];
$image_height=$image_array[1];
$image_width=$image_array[0];
$maxfilesize=2000000;
if($maxfilesize<$image_size)
{
echo "Please upload a smaller image. The size of the image is too large.";
}
else
{
$query="INSERT INTO gallery(image_name,image_type,image,image_size) VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
$stmt=$dbh->execute($query);
$lastimageid=mysql_insert_id();
echo "<p>You uploaded this image</p>";
echo "<img src='imagedisplay.php?imageid=".$lastimageid."' />";
}
}
}
else
echo "<br/><br/> Your are <span class=\"red\"><b>not Authorized</b></span> to view this page. If you are the Admin, please login with your credentials again. <a href='login_page.php'>Click here to continue</a>";
?>
</div>
</body>
</html>
Now the problem is that the control never goes to imagedisplay.php ie. it fails to reference imagedisplay.php altogether.
the code for imagedisplay.php is below:
<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
$id=$_GET['imageid'];
$query="SELECT * FROM gallery where image_id=".$id;
$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
$imagedata=$row[3];
header("Content-type:image/jpeg");
echo $imagedata;
?>
I have tried all permutation combinations with the quotes, tried echo statements to see if control enters the file....but it does not...it stays in the main file only...i dont understand the reason...please help...
You have some errors in your queries ("allery", "mage_id").
$query="INSERT INTO allery(image_name,image_type,image,image_size) VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
...
$query="select * from gallery where mage_id=".$lastimageid;
If you have errors anywhere in your code in imagedisplay.php, they will be output and end up in the image data, corrupting it. See what imagedisplay.php outputs, if you remove imagejpeg() and don't send the image headers. This will give you the necessary information.

Categories