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;
}
}
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 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.
I got a bunch of images (225 in total). Example of their names:
4n27e.png
4n28e.png
4n29e.png
4n30e.png
5n12e.png
5n25e.png
5n26e.png
5n27e.png
5n28e.png
I need to form one big picture out of all these images. For example first line of images would be 4n27e (2nd image 4n28e, 3rd image 4n29e and so on). Second line of images would start from 5n12e (2nd image 5n25e and so on). What methods do i need to search for to solve this?
Note: i need to do this only with php and maybe some javascript.
Test case for logic, you can replace the echo with an img tag, and /or use div.
<?php
$images=array();
$images[]="4n27e.png";
$images[]="4n28e.png";
$images[]="4n29e.png";
$images[]="4n30e.png";
$images[]="5n12e.png";
$images[]="5n25e.png";
$images[]="5n26e.png";
$images[]="5n27e.png";
$images[]="5n28e.png";
echo "<table border='1'>";
$oldIndex=0;
$row=1;
foreach($images as $image)
{
if(substr($image,0,1)!=$oldIndex)
{
if($row>1){echo "</tr>";}
echo "<tr>";
$oldIndex=substr($image,0,1);
$row++;
}
echo "<td>$image</td>";
}
echo "</table>";
?>
very new to PHP to please bear with.
As you can see from my snippet of code I am simply displaying product information one line under each other then repeating the loop using while. This then obviously displays my relevant data in just one column, one under each other.
while ($row = mysqli_fetch_row($result)){
echo "<img src=\"images/album1.jpg\"/><br>"; //this will eventually show the product image
echo "$row[1] <br>"; //this shows the product name
echo "<strong>£$row[2]</strong><br>"; //this shows the product price
}
How would I go about creating a grid view, for example using columns to display my data? I presume it would be some kind of loop to be added and maybe using tables to display my data?
Any help is greatly appreciated.
you can dot this by creating div and float that div to left this will create grid view
while ($row = mysqli_fetch_row($result)){
echo "<div class='container'>";
echo "<img src=\"images/album1.jpg\"/><br>"; //this will eventually show the product image
echo "$row[1] <br>"; //this shows the product name
echo "<strong>£$row[2]</strong><br>"; //this shows the product price
echo "</div>";
}
and css
.container{
float:left;
}
you can
also set the max height and width of div
I have been working on a project but I have reached a point where I am stuck. I have a database that contains the the working status of some mahcines. The values for the status go from 1-5. I need to be able to display a different image for each machine in a webpage based off of the value that appears in the database for that Mahcine. I am drawing a big blank on how to do this. Im using a MySQL DB and everything is written in PHP.
Basically it this. If a machine has a status value of 1 then it shows a green image. If the value is 2 then it would be yellow and so on. . .
Hope you guys can help
You can try something like this:
// your mysql select, wich contains the machine data.
$query = mysql_query("select the data about machines...");
// you iterate on the result set and fetch each row to $data
while($data = mysql_fetch_array($query))
{
switch($data['machine'])
{
case "machine type 1": // you can put integer values here as well, like case 1:
echo '<img src="first_machine.jpg" alt = "first machine" />'
break;
case "machine type 2":
echo '<img src="second_machine.jpg" alt = "second machine" />'
break;
default: // undefinied
echo '<img src = "undefinied.jpg" alt = "undefinied" />'
}
}
Don't use the img tag, instead create a div for which you apply a style class same as the machine status value
<div class="machine status<?php echo $status;?>" ></div>
now in your css,
.status1{
background-image:url(red.jpg);
}
.status2{
background-image:url(green.jpg);
}
.status3{
background-image:url(jpg.jpg);
}
.machine{
width:50px;
height:50px;
}
Ok you can't display multiple images within a image/jpeg page...
You're telling the browser that the page is image/jpeg (in other words, the page is AN IMAGE) but you're echoing out multiple image data
You should rather use the gallery page to show all images like this:
<?php
// $images = result from database of all image rows
foreach ($images as $img) echo '<img src="img.php?id='.$img["id"].'">';
?>
and in img.php:
// Load the image data for id in $_GET['id'];
header("Content-type: image/jpeg");
echo $data;