I am trying to get my image to display when clicked via the link. However when I click on the link it finds the image id as displayed in the url link ok but does not display any image. Can you please help?
<?php
//sets up thisPage
$pageSize=10;
if (isset($_POST["thisPage"])) $thisPage=$_POST["thisPage"];
else $thisPage=1;
//selects all distinct expenses that have been uploaded
$dbQuery="SELECT * FROM images WHERE user_id = '$userID' ";
$dbResult=mysqli_query($db_connection, $dbQuery) or die(mysqli_error($db_connection));
echo "<table cellspacing=\"5\" class=\"recordsTableBG\"> <thead
class=\"recordsTableHeader\">";
echo '<tr> <th>ID</th><th>Amount</th><th>Description</th><th>Filename</th>
<th>Project ID</th><th>Status</th></tr></thead>';
echo '<tr class="alternateRowColor">';
'<tr>';
while ($dbRow=mysqli_fetch_array($dbResult)){
echo "<img src = 'uploaded/$image' width = '200' height = '200'>";
// display row with expense
echo '<td>'. $dbRow['id'] .'</td>';
echo '<td>'. $dbRow['user_id']. '</td>';
echo '<td><a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>
Click here to view image</a></td>';
}
echo "</table>";
echo "</form>";
?>
<!-- add submitt button
close form -->
</div>
You have a mixture of two approaches going on here, and that's where the confusion lies.
First, notice your
<img src='uploaded/$image' width = '200' height = '200'>
The browser is going to see something like
<img src='uploaded/picture_of_cat.jpg' width='200' height='200'>
and render a 200px by 200px image of a cat. I'm assuming that's your thumbnail. (As an aside, I notice that it's not enclosed in <td></td> even though it's in the table row).
In another part of the table, you have
<a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>Click here to view image</a>
which is going to render as something like
<a href='http://example.com/index.php?imageid=1234'>Click here to view image</a>
When the user clicks that link, it's going to make a GET request to the server with imageid equal to 1234. What is the server going to do with that imageid? In the code you've posted, nothing.
You have two choices:
if you want to keep the link as it is, you'll have to write some code that will take the imageid value, find the appropriate image, and return it to the browser as image data - that means setting the appropriate headers and sending the data back as binary data.
the simpler way to do it would be to replace the URL in the link with the same one you have in your <img> tag - when the user clicks on it, the server will just return the image.
Related
I don't know if I'm doing it right, but I have a bunch of images I'm retrieving from the page and since I don't wan a page to have too many images of big sizes, I have displayed them with a much smaller size but I have attached each of them to a link so that when a user click on a picture it opens that image with its original size. The problem is that those images are really big and my client wants the ability to zoom in and out which I don't know how to do. The client thought about resizing the size of the window (in the browser) but sadly it resizes all other windows (for the application) and this is not ok because he needs to see the image and compare it with some information on the app. SO Below is the code of the images displayed and after the user have clicked on the image.
small images
$count = 0;
echo " <div class=\"row\">";
while($row = $result->fetch_assoc()) {
$ext = $row['Extension'];
$ImageID=$row['ImageID'];
if(($count%3) ==0){
echo "</div>";
echo " <div class=\"row\">";
echo " <div class=\"col-sm-2\">";
echo " <a href=\"viewimage.php?ImageID=$ImageID\" class=\"thumbnail\">";
echo '<img id=\"myImg\" src="data:image/$ext;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}else{
echo " <div class=\"col-sm-2\">";
echo " <a href=\"viewimage.php?ImageID=$ImageID\" class=\"thumbnail\">";
echo '<img id=\"myImg\" src="data:image/$ext;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}
}
echo "</div>" ;
Image after link is clicked
<?php
$ImageID = $_GET['ImageID'];
$query = "Select * from $dbname.Images where ImageID = $ImageID";
$result = mysqli_query($conn,$query);
$row = $result->fetch_assoc();
$ext = $row['Extension'];
echo '<img src="data:image/$ext;base64,'.base64_encode( $row['Image'] ).'"/>';
?>
I don't know what to do at this point, how can I provide that zoom in/out functionality?
First things first: Generally don't add base64 encoded images directly into your html. Link to them, and host them on your server. It is quite an expensive way of making images appear, both for the server, database, and for the client. It also makes it impossible for the client to cache the images, and it means that each repeated page visit causes the entire data to be sent.
Make two folders on your webservers:
images/
thumbnails/
Put your small images in "thumbnails" and large images in "images"
And if you need to, store the image-names in your database, so you can do something more like this:
echo '<img src="images/'+$imageName+'">'
If you want to, you can do an on-demand resizing of your images, using gd-lib.
The basic idea being, in pseudocode:
//Before the echo command, but after fetching the filename from database
if thumbnails/$imageName exists
then use gdlib to read images/$imageName and save a small version to thumbnails/$imageName
This approach is also applicable if you want to use client-side javascript to show larger versions on the same page. See my page finalkey.net for an example http://finalkey.net/gallery
I am trying to link a MySQL DB to an SVG image to dynamically change the SVG elements with Raphael JS.
I have a MySQL DB where I query using PHP and display the results in table form to an html page: (The script below works and displays the username and a picture only when the condition of the timestamp is met.)
<?php
mysql_connect("","","");
mysql_select_db("");
$res=mysql_query("select username, picture from 'table' WHERE status > UNIX_TIMESTAMP(NOW()) - 300");
echo "<table>";
if (!$res) {
die("Query to show fields failed");
}
$fields_num = mysql_num_fields($res);
echo "<h1>Table:Status</h1>";
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($res);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>"; echo $row["username"]; echo "</td>";
echo "<td>"; ?> <img src=" <?php echo $row["picture"]; ?>" height="50">
<?php
How can I take the similar concept above of displaying the results in table form to an SVG image where the SVG elements will change/update only when the query condition is met?
Here is my sample SVG image with 5 elements:
<polygon fill="#B2B2B2" points="150.3,8.8 203.8,31.7 169.8,91.4 133.4,75.8 "/>
<circle id="circleT3" circle fill="#FFFFFF" cx="163.1" cy="53.6" r="7.3"/>
<circle id="circle3_1" circle fill="#CCCCCC" cx="184.5" cy="82.4" r="7.3"/>
<circle id="circle3_5" circle fill="#CCCCCC" cx="136.6" cy="27.2" r="7.3"/>
<circle id="circle3_4" circle fill="#CCCCCC" cx="166.4" cy="7.3" r="7.3"/>
Can someone point me to some sample code or tutorial? Or is there a better way to do this? Thanks.
EDIT:
In MySQL DB I have a column for username, password and timestamp. When a user logs into webpage the timestamp updates. The PHP code above is used to query who has logged within 5 minutes ago from current time.
What I would like to do with this information with SVGs is create a graphical representation of the login.
So each username will have their own SVG element (a circle) associated with them and when they log in/out, that SVG element (code above) will change color.
Right now I do not know how to link the username with my SVG elements so the SVG element will dynamically update like my table I query from MySQL when the timestamp changes.
The answer will depend on further information that isn't really available until the rest is written.
You could combine Snap (to modify existing inline SVG or create it) or Raphael (to create new SVG only, you can't use it to modify inline SVG), or another SVG library of choice (eg svg.js or jquery.svg maybe).
Assuming you already have something to use on the page, that is showing the logged in user, you could do something like in pseudocode...
loop user;
if( document.getElementById( userId ) ) Snap('#' + userId + '_image').attr({ fill: 'green' });
(The svg reference may be the same as the circles, but somewhere you would need some type of lookup to know which circle is which userid)
This assumes the svg is on the page. If its not, you could create it with
paper.circle(x,y,r).attr({ fill: 'green' });
If you want it dynamic (so status changes without a refresh), you may need to tie ajax calls to get status from the mysql db, but if you already have a user name displaying on the page, I'm assuming that is already taken care of.
I have successfully update the svg element color to when the user logs in, the corresponding circle will change color. So I have my svg code from illustrator. I then put in this script in my php file:
window.onload = function () {
if(document.body.innerHTML.toString().indexOf('username') > -1){
circle1_1.setAttribute("fill", "yellow");
};
};
Whenever a user logs in, the info is populated on the table in the html from the MYSQL query and the script looks to see if that username is on the page and if it is, change color of SVG element.
So it basically links the SVG element to any value/variable.
Not the prettiest code or logic out there but for anyone else doing something similar, enjoy.
I have an image gallery displaying all photos from my file directory with php. I have defined what type of file to use etc but the main line of code is as below:
echo '<img src="'.$dir.'/'.$file.'" alt="'.$file.'" width="600px" />';
I know by changing the width=" " above I can alter how many images I have in a row but if i had a drop-down box, with options stating how many columns i want the photos displayed in and a button to refresh the page to show the columns option i have selected.
So for example: I select a drop down option "15 columns" and hit the refresh button, the page should then refresh and the images be redisplayed in columns of 15. How would i go about this?
You should be using HTML Tables for it. Anyhow here is how you should do it.
$allowed_per_line = $_GET['columns'];
$counter = 0;
while($image = fetch...) {
$counter++;
echo "<img src='{$dir}/{$file}' alt='{$file}' />";
if($counter==$allowed_per_line) {
echo '<br />';
$counter=0;
}
}
I'm trying to display an image that is stored in my database. I know it is not best practice to store an image in the database, but just for this purpose its what I need. My problem is when I go to display the image, all I get is a load of scrambled code, which I guess is the image in code but not visually what I want. When I use the header tag to identify the image all I get is a thumbnail to mean it isn't reading it in right. Any help would be great.
Below is code I'm using. I've tried it in both ways using it to display in either the php or in html but can't display the image:
while($row = mysql_fetch_array($result))
{
$Long = $row['Long'];
$Lat = $row['Lat'];
$img = $row['file'];
echo "<b><center>Database Output</b><br><br>";
echo "<td>" .$row['file']."</td>";
echo "----";
echo "<td>" .$row['Lat']."</td></center>";
//echo "<td>" .$row['file']."</td>";
}
?>
<div id="map">
<img src="<? $img?>" alt="">
Thanks for any input
Either you embed the image into your html page as a data URI
<img src="data:image/jpeg;base64,<?php echo base64_encode($img) ?>" />
which is hideously NASTILY horrible for 'large' files - you make it impossible for the browser to cache the image, forcing the user download the base64-encoded data EVERY time they load the page.
or you have a sub-script to serve up the actual image, and have something more like:
<img src="getimage.jpg?id=XXX">
and
<?php
$data = get_image_from_db($_GET['id']);
header('Content-type: image/jpeg');
echo $data;
I am creating a web application where users can upload/download/view online pdfs. I want to change the name of the pdf file to view in new tab link (like we see in all websites).
Does anyone know how to make any field in the database that gives a link that when user clicks, it opens the pdf in a new tab?
if($result) {
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
<td><b>view><b/></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['type']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
The solution doesn't have to be within the database.
You can use an anchor tag to redirect the user to the link :
<a href="{$path_to_pdf}" target="_blank" >Click here to view the PDF</a>
Put that in a cell in the table you are generating.
I could be mistaken - but I do believe that the users browser is also a factor here. The browser has to be able to display pdf's. The latest and greatest* browsers have it baked in but user preferences' might also be a factor.
* Let your imagination run wild
You can use target="_blank" in your a tag
PDF
This works in Google Chrome, not sure if it works in other browsers.
You can look here http://www.allaccessliving.com/residences/floorplans#1bed15bath:35A
There's a pdf link and you see for yourself if you inspect that element.
When outputting the link to the PDF, use this:
Visit StackOverflow
setting target will tell the browser to open the link in a new tab.