I have working code, which pulls all images from a dynamically created folder. But I want to display only 1 image from a particular folder in my display page. Any suggestions?
My code:
<?php
$search_dir = "$directory/{$row['name']}{$row['hotel_address']}";
$images = glob("$search_dir/*.jpg");
sort($images);
//display images
foreach ($images as $img) {
echo "<img src='$img' height='150' width='150' /> ";
}
?>
You can display a single image with:
<?php
$search_dir = "$directory/{$row['name']}{$row['hotel_address']}";
$images = glob("$search_dir/*.jpg");
sort($images);
// Image selection and display:
//display first image
if (count($images) > 0) { // make sure at least one image exists
$img = $images[0]; // first image
echo "<img src='$img' height='150' width='150' /> ";
} else {
// possibly display a placeholder image?
}
?>
If you want a random image, do this:
// Image selection and display:
//display random image
if (count($images) > 0) { // make sure at least one image exists
// Get a random index in the array with rand(min, max) which is inclusive
$randomImageIndex = rand(0, count($images)-1);
$img = $images[$randomImageIndex]; // random image
echo "<img src='$img' height='150' width='150' /> ";
} else {
// possibly display a placeholder image
}
You can use current to get the first image from the array.
<?php
$search_dir = "$directory/{$row['name']}{$row['hotel_address']}";
$images = glob("$search_dir/*.jpg");
sort($images);
//display one image:
echo "<img src='current($images)' height='150' width='150' /> ";
?>
Related
What I am trying to do is something like below:
The buttons at the side can be ignored. So the idea is that the big square is the main video playing and the little ones underneath will be the thumbnails of the remaining videos. The main video playing is randomly picked when the site is loaded and then the ones that are not playing, the thumbnails will be shown underneath. I am doing this like so:
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
echo $video;
array_push($items, $video);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $items[0];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
foreach($restVideos as $videoThumbnail) {
try {
$stmt = $dbconn->query("SELECT thumbnail, videoTitle FROM videos WHERE video = '$videoThumbnail'");
while($row = $stmt->fetch()){
$thumbnail = $row['thumbnail'];
array_push($thumbnails, $thumbnail);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
foreach($thumbnails as $nonPlaying) {
?>
<img src=<?php echo $nonPlaying ?> id="thumbnails" width="200" height="100">
<?php } ?>
what is happening at the moment is below:
Each image is just stacking up against each other how can I change it so this is not the case?
Also is there a more efficient way of doing this than what I am currently doing?
Edit:
$thumbnails = array();
$videoTitles = array();
$test = 700;
echo "<div>";
foreach($restVideos as $rVideo) {
$test = 200 + $test;
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100" style= "left: <?php echo $test?>px">
<?php
echo "</div>";
}
You not required to two query two times to db .
Use key value and store the entire video. While printing thumbnail based on the key (unique id ex. videoid). You can retrieve the content when you print the thumbnail list and can avoid querying again in a loop.
Ref to code snipp
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoMap[$row['videoid']] = $row;
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
if (count($items) == 0) echo $video;
array_push($items, $row['videoid']);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $videoMap[$items[0]]['video'];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
echo "<div>";
foreach($restVideos as $rVideo) {
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100">
<?php
}
echo "</div>";
}
?>
Use a div tag and print the image thumbnails.
I want to make a product gallery like 5 products, each has their own background-image attribute
I use loop to insert the product image, so I want to make it each loop will have different background-image
I'm thinking of using IF statement like so
<?php
$bg = 0;
$bg1 = "url('img1.jpg')";
$bg2 = "url('img2.jpg')";
$bg3 = "url('img3.jpg')";
$bg4 = "url('img4.jpg')";
$bg5 = "url('img5.jpg')";
if ($bg = 0){
echo " <div style='background-image :$bg1 ;'>" ;
$bg = 1;
} else if ($bg= 1) {
echo " <div style='background-image :$bg2 ;'>" ;
$bg = 2;
} else if ($bg= 2 ) {
echo " <div style='background-image :$bg3 ;'>" ;
$bg = 3;
} else if ($bg= 3 ) {
echo " <div style='background-image :$bg4 ;'>" ;
$bg = 4;
} else if ($bg= 4 ) {
echo " <div style='background-image :$bg5 ;'>" ;
$bg = 0;
}
echo " </div> " ;
code for product images
?>
above is the simplified code I wrote, it doesn't work.
if anyone has a different but much simpler solution it will be appreciated
note : the image files are in the same directory with this php file
thank you
Would you be open to using img tags? This, in my opinion, would be a better solution:
Code:
<?php
$images=Array(
"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg",
"img5.jpg"
);
//
print "\n<br> Code: \n<pre>\n".RenderThoseImages($images)."\n</pre>";
//
function RenderThoseImages($images)
{
//
$s="";
//
foreach($images as $image){
$s.="\n<div><img src=\"{$image}\"></div>";
}
return $s;
}
?>
Outputs:
<br> Code:
<pre>
<div><img src="img1.jpg"></div>
<div><img src="img2.jpg"></div>
<div><img src="img3.jpg"></div>
<div><img src="img4.jpg"></div>
<div><img src="img5.jpg"></div>
</pre>
The main reason being that when you use the background-image CSS, you're also responsible for grabbing the image dimensions in PHP and rendering height/width into the div CSS as well, or possibly creating some javascript to fix it after loading, creating unneeded headache.
I have the following situation.
If there isn't an image in the DB, the page it's on shows a big image placeholder. What is the best way to hide the image placeholder if an image doesn't exist?
<img src="<?php echo '../img/artists/' . $row_rsAccents['artistPhoto']; ?>" width="100%"/>
http://westerndesignconference.com/intheloop/
You can do this with a simple if/else statement like so:
//I prefer to set things with variables
$placeholder_img = "../img/artists/placeholder.jpg";
$db_img = $row_rsAccents['artistPhoto'];
if($db_img){
$img_src = $db_img;
} else {
$img_src = $placeholder_img;
}
echo "<img src='$img_src' alt='' width='100%' />";
If there is a value returned - show an image. If the condition fails, no <img> will be displayed, preventing the blank gap
if (isset($row_rsAccents['artistPhoto'])) {
echo '<img src="../img/artists/' . $row_rsAccents['artistPhoto'] . '" width="100%"/>'
}
if (file_exists('artist.jpg') {
echo "<img src='artist.jpg'>";
}
else {
echo "<img src='default.jpg'>";
}
I'm trying to display image in 5 by 3 table.
I'm able to display the images if all empty(blank.png).
Here is the code
<?PHP
$ds ='\image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
echo "<pre>"; print_r($imagefile);
$file = 'blank.png';
$d = $ds.$file;
echo "<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">";
for($row=1;$row<=5;$row++){
echo "<tr>";
for($col=1;$col<=3;$col++){
// echo"<td height=60px>W$row</td>";
//if()
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
echo "</tr>";
}
echo "</table>";
?>
I want to display the images base on middle file name array $imagefile eg W1, W2 and if not in array, I will display the blank.png.
I was able to get the middle file name by this code, but I cannot display the images in correct row/col.
for($i=0;$i<count($imagefile); $i++) {
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
}
Can you try this,
Based on your code add these when you echo the image,
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
if($wd == *the increment either row or col*)
{
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
else
{
echo"<td height=60px>No image</td>" .PHP_EOL;
}
See if it works.
Here it is
<?php
$ds ='/image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
//echo "<pre>"; print_r($imagefile);
$default = 'blank.png';
?>
<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">
<?php
for($row=1;$row<=5;$row++){
?>
<tr>
<?php
for($col=1;$col<=3;$col++){
// construct the file name
$filename = 'EX_W' . $row . '_0' . $col . '.png';
// set the default image file
$imgPath = $ds . '/' . $default;
// in case the file name exists in your array with images,
// set the correct path to the image
if (in_array($filename, $imagefile)) {
$imgPath = $ds . '/' . $filename;
}
?>
<td height=60px>
<img border="1" height="120" width="120" src="<?php echo $imgPath; ?>"/>
</td>
<?php } ?>
</tr>
<?php
}
?>
</table>
As you can see I also prefer to "embed" the php code in the HTML. It makes no sense to me outputting HTML code through the PHP engine if it can be parsed as is ;-)
Ok, so I'm loading a list of images by directory using PHP:
<?php
# fetch image details
$images = getImages("marc/img/livingrooms/");
# display on page
foreach($images as $img) {
echo "<li><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
}
?>
Then I'm using the Galleria JQuery plugin to style those items into a gallery:
$(document).ready(function(){
$('.dynolist').addClass('gallery_group'); // adds new class name to maintain degradability
$('ul.gallery_group').galleria({
history : true, // activates the history object for bookmarking, back-button etc.
clickNext : true, // helper for making the image clickable
insert : '#main_image', // the containing selector for our main image
onImage : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
// fade in the image & caption
image.css('display','none').fadeIn(1000);
caption.css('display','none').fadeIn(1000);
// fetch the thumbnail container
var _li = thumb.parents('li');
// fade out inactive thumbnail
_li.siblings().children('img.selected').fadeTo(500,0.3);
// fade in active thumbnail
thumb.fadeTo('fast',1).addClass('selected');
// add a title for the clickable image
image.attr('title','Next image >>');
},
onThumb : function(thumb) { // thumbnail effects goes here
// fetch the thumbnail container
var _li = thumb.parents('li');
// if thumbnail is active, fade all the way.
var _fadeTo = _li.is('.active') ? '1' : '0.3';
// fade in the thumbnail when finnished loading
thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
// hover effects
thumb.hover(
function() { thumb.fadeTo('fast',1); },
function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
)
}
});
});
The issue is that I need to give the first item pulled from that list a class of "active", so the first image will be loaded into the slideshow. Everything else works right now, other than getting that first image loaded.
Any suggestions?
$('selector:first').addClass('active');
Maybe?
Or what about
# display on page
$class = 'class="active" ';
foreach($images as $img) {
echo "<li><img ".$class."src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
$class="";
}
Try this:
# display on page
$counter = 0;
foreach($images as $img) {
if ($counter == 0) { // first
echo "<li class=\"active\"><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
}
else
{
echo "<li><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
}
$counter++;
}
If you want to do that on the PHP side, you could count, in your foreach loop, how many images have been displayed -- and if 0 have been displayed, add a class="active" to the one you are currently echoing.
For instance, something like this should do the trick :
$counter = 0;
foreach($images as $img) {
echo "<li><img src=\"{$img['file']}\" ";
if ($counter === 0) {
echo 'class="active"';
}
echo " title=\"\" alt=\"\"></li>\n";
$counter++;
}
The first time you'll loop, $counter will be 0, and class="active" will be echoed ; next times, it won't be 0 anymore, and no addtionnal active class will be added.
In jquery:
$(".gallery_group li:first-child").addClass("active");
In php:
# display on page
$first = true;
foreach($images as $img) {
echo "<li " . ($first ? "class='active'" : "") . "><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
$first = false;
}