Horizontal Lazy Load in container not showing images when scrolling - php

I have created a horizontal div that hold a large number of images but has a set width so you have to scroll to see all of the images. I am trying to implement lazy load to speed up load time, but I can get it to work on the first few images, but not the other images just stay gray.
When I remove white-space:nowrap; I can get the lazy load the work correctly, but I lose my single row div.
What would be a good way to have the horizontal scrolling div work with lazy load?
<div class="galleryContainer container-fluid list-inline">
<div class="row ">
<?php
$files = glob("public/img/thumbnail/*.*");
$lFiles = glob("public/img/*.*");
for ($i=0; $i<count($files); $i++)
// ($i=0; $i<count($lFiles); $i++)
{
$image = $files[$i];
$Limage = $lFiles[$i];
// print $image ."<br />";
echo "<div class='img-responsive col-md-3'>";
echo '<div class="container" width: 90%">';
echo '<a href="../'.$Limage .'">
<img width:"300px" height="400px" height="class="lazy" data-original="../'.$image .'" alt="Mural Image"/></a>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
<script>
jQuery(document).ready(function ($) {
$(function() {
$("img.lazy").lazyload();
container: $(".galleryContainer")
threshold : 200
effect : "fadeIn"
});
});
</script>
<script src="../public/scripts/jquery.lazyload.js"></script>
<script src="../public/scripts/jquery.scrollstop.js"></script>
Here is my CSS:
.galleryContainer {
margin-top: 2%;
width: 90%;
overflow-x:scroll;
overflow-y:hidden;
}
.profile {
margin-top: 5%;
}
}
.galleryContainer img {
height: 300px;
width: 400px;
}
.fullDrawing img{
height: 217.273px;
width:1440px;
}
.list-inline {
white-space:nowrap;
}
.galleryContainer > .row [class*="col-lg"], .galleryContainer > .row [class*="col-md"], .galleryContainer > .row [class*="col-md"] {
float:none;
display:inline-block;
white-space:normal;
vertical-align:top;
}
#main-content > .row {
overflow-x:scroll;
overflow-y:hidden;
white-space:nowrap;
}

First some small changes:
<img width:"300px" height="400px" height="class="lazy" data-original="../'.$image .'" alt="Mural Image"/>';
should be
<img class="lazy" data-original="../'.$image .'" width:"300" height="400" alt="Mural Image">';
Also i never included my php scripts in html, but maybe the lazylload function triggers too early.
Look at the code loaded by your browser, if the functon triggered correctly the element should look like this:
<img class="lazy" data-original="PATH" width="300" height="400" alt="Mural Image" src="PATH" style="display: inline;">
Last but not least lazyload may have problems with your link around the image, you should test it with a raw image.

Related

How to make all the images of different sizes the same height and width in a page?

I used
<h1> Products </h1>
<div style="width:300px;height:300px;">
<?php
foreach( $result as $r)
{
echo "<div style='
float: left;
width: 300px;
height: 300px;'>";
echo "<img alt='Missing Picture' src='img/". $r['product_img_name'] . "'>";
echo "<h2> Name: " . $r['product_name'] . " </h2>";
echo "<p> Description: " . $r['product_desc'] . "</p>";
echo "<p> Price: " . $r['price'] . "</p>";
echo "</div>";
}
but it gives me images that overlap and are too large or small.
Is there anything I can do to make all the different size images the same size and shape and in consistent order?
This problem is a bit more complicated than it seems, here is one way of approaching it.
First, define a div.inner container to hold the image and specify both a width and height value (100px for example).
Second, define a div.wrap container that will hold the div.inner block and then the h2 and p elements. You need to provide some vertical space for the text elements, so for example, give a height of 200px to div.wrap. You can then use float: left to create a grid pattern.
To get the images to scale either to the height or width (depending on the native aspect ratio of the image), apply max-height and max-width of 100% to the image.
Note that unless your images are exactly square (intrinsic height and width are the same), then you will have some extra white space either on the left and right edges of the image (in the case of portrait types images) or on the bottom edge (for landscape images).
div.wrap {
width: 100px;
height: 200px;
border: 1px solid blue;
float: left;
margin: 10px;
}
div.inner {
width: 100px;
height: 100px;
text-align: center;
}
img {
max-height: 100%;
max-width: 100%;
}
h2 {
font-size: 14px;
margin: 5px;
}
p {
font-size: 12px;
margin: 5px;
}
<div class="wrap">
<div class="inner">
<img src="http://placehold.it/300x300">
</div>
<h2>Title Text</h2>
<p>Some demo text goes here.</p>
</div>
<div class="wrap">
<div class="inner">
<img src="http://placehold.it/300x400">
</div>
<h2>Title Text</h2>
<p>Some demo text goes here.</p>
</div>
<div class="wrap">
<div class="inner">
<img src="http://placehold.it/400x300">
</div>
<h2>Title Text</h2>
<p>Some demo text goes here.</p>
</div>
You need to specify a width and a height on the image tag itself. Best practise is with a css style, but you can do it on the tag itself as well like this...
echo "<img style='width:300px;height:300px;' alt='Missing Picture' src='img/". $r['product_img_name'] . "'>";
If the original images are not square shaped then forcing the width and height to 300px will cause the images to be stretched which might not look so good.

Background images are overlapping while fetching from database?

I am fetching images from the database which is working but all images are overlapping. I want to display all images one by one. Would you help me in this?
.img {
position: relative;
float: left;
width: 300px;
height: 300px;
background-position: 50% 50%;
background-repeat: no-repeat;
background-size: cover;
}
<?php if ($result_related_products->num_rows > 0) { // output data of each row
while($row = $result_related_products->fetch_assoc()) {
$p_img=$row['p_img'];
?>
<div class="img" style="background-image: url('admin/assets/img/products/<?php echo $p_img; ?>')">
<?php }
}
else { echo "0 results"; } ?>
You have to close the DIV
</div>
(In your while loop)
You forgot to close the div
change this
<div class="img" style="background-image: url('admin/assets/img/products/<?php echo $p_img; ?>')">
for this (see at the end of line the </div>)
<div class="img" style="background-image: url('admin/assets/img/products/<?php echo $p_img; ?>')"></div>

CSS: First row of image gallery coming out diagonal?

I have an array of images in php that I am trying to display as a gallery. It is working pretty well, except that the first row of the gallery comes out diagonal, with each image slightly below the one before it.
Here is the code I am using:
CSS
.gallery {
margin: 5px;
border: 1px solid #ccc;
float: left;
display: inline;
width:180px;
height:180px;
}
.gallery:hover {
border: 1px solid #777;
}
.gallery img {
max-height:100%;
max-width:100%;
margin:0 auto;
}
HTML/PHP
<div class="gallery-container">
<?php
$count = 0;
foreach($images as $image) {
echo "<div class='gallery'><a href=''><img src='".$dir.$image."'/></a></div>"; //$dir is the path to the image
if(count % 4 == 0) {
echo "<br>";
}
$count = $count + 1;
}
?>
</div>
Here is a screenshot of the result on the first row:
All the other rows come out fine. Thank you very much for any help.
If you want to do it using float, you would better do that like this:
CSS
.gallery{
margin: 5px;
border: 1px solid #ccc;
float: left;
display: block;
width:180px;
height:180px;
}
.gallery.clear-left{
clear: left;
}
PHP/HTML
<div class="gallery-container">
<?php
$count = 0;
foreach( $images as $image ){?>
<div class="gallery <?php echo( 0 == $count % 4 ? 'clear-left' : '' );?>">
<a href="">
<img src="<?php echo $dir.$image;?>">
</a>
</div>
<?php
$count++;
}
?>
</div>
But there is an even better solution to this, called flex:
Instead of br element render "clear" div
<div style="clear:both"></div>
Have you tried starting the count at 1 instead of 0?
Checkout the resulting html. It is likely that you count is off and the br is being put to the dom too often. This fiddle shows that your desired html and css should work.
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<br>
Though, I would consider not using the br, but instead using a containing div:
like this

By default one item selected in webpage

When i hover mouse at that time i got the effect but i want to set one image as a by default select when page is load.
Anyone Help me.
Here is my css.
.simpleLens-thumbnails-container a{
display: inline-block;
}
.simpleLens-thumbnails-container a img{
display: block;
}
.all-thumb{
width:50px !important;
}
#demo .img{
border: 5px solid #000;
}
.all-thumb:hover{
box-shadow: 0px 0px 10px #BFCBD5;
}
Here is my PHP code.
<a href="#" id="demo" class="simpleLens-thumbnail-wrapper" data-lens-image="<?php echo $img; ?>" data-bigimage="<?php echo $img; ?>">
<img class="all-thumb" id="demo" src="<?php echo $img; ?>">
</a>
add a class selected to the desired img element like this
$first_item = true;
foreach (............)
{
if($first_item){
<img class="all-thumb selected" id="demo" src="<?php echo $img; ?>">
$first_item = false;
}
else
<img class="all-thumb" id="demo" src="<?php echo $img; ?>">
}
and use this css
.all-thumb.selected, .all-thumb:hover{
box-shadow: 0px 0px 10px #BFCBD5;
}
this will work :)
Add selected to your defult image's class. Desired output for the default image:
<img class="all-thumb selected" id="demo" src="<?php echo $img; ?>">
If you can't hardcode it or do it from the PHP side, use this JQuery code to add the class selected to the first image:
$('.all-thumb:first').addClass('selected');
Then add this JQuery code to your page:
<script>
$(".all-thumb").not(".selected").hover(function(){
$(".all-thumb selected").css("box-shadow","none");
},function(){
$(".all-thumb selected").css("box-shadow","0px 0px 10px #BFCBD5");
});
</script>
It should work. JQuery must be included.
Try this:
Need to add any minimum jquery.
<script src="js/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function(){
$('.simpleLens-thumbnail-wrapper .all-thumb:first').css('box-shadow', '0px 0px 10px #BFCBD5');
$('.simpleLens-thumbnail-wrapper .all-thumb').mouseover(function(){
$('.simpleLens-thumbnail-wrapper .all-thumb:first').css('box-shadow', 'none');
});
$('.simpleLens-thumbnail-wrapper .all-thumb:first').mouseover(function(){
$('.simpleLens-thumbnail-wrapper .all-thumb:first').css('box-shadow', '0px 0px 10px #BFCBD5');
});
});
</script>
you can also use .hover instead of .mouseover

Apply link to play button added to media gallery plugin

I am building a site that uses a plugin to create a gallery view a video feed. In the PHP file that generates the gallery view, I added a play button to display on top of the video thumbnails. While the button displays correctly, the plugin applies the video link directly to the thumbnail image, so if a user clicks directly on the button, it does not open the video modal.
<div class="media-thumb videoPreviewBox">
<div class="videoPreview">
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
<img class="playButtonHover" src="<?php echo $this->getThemePath()?>/images/playButtonHover.png">
<img class="playButton" src="<?php echo $this->getThemePath()?>/images/playButton.png">
</div>
</div>
While I'm not sure why there is a closing tag placed where it is, the plugin seems to work fine. I think the issue is that href="#popup_prep" is applied to the image. I tried to wrap the thumbnail image and play button states in < a href="#popup_prep"> (space added to show tag in post), both with and without the href applied to the image.
I'm stumped and don't want to screw anything up. Hoping a PHP person here can point out what I'm doing wrong. Many thanks!
The rest of this post is information I'm including for due diligence, since I'm not sure how much and what context is needed. First the CSS applied to these elements, followed by the full PHP file for the gallery view.
div.videoPreviewBox {
float: left;
display: inline-block;
width: 21%;
margin: 2%;
margin-top: 0%;
}
#media only screen and (max-width: 594px) {
div.videoPreviewBox {
width: 46%;
}
}
#media only screen and (max-width: 300px) {
div.videoPreviewBox {
width: 96%;
}
}
div.videoPreview {
position: relative;
}
img.videoPreviewImg {
width: 100%;
z-index: 1;
}
img.playButtonHover, img.playButton {
position: absolute;
z-index: 5;
top: 50%;
left: 50%;
margin-top: -24px;
margin-left: -24px;
-webkit-transition: opacity .7s ease-in-out;
-moz-transition: opacity .7s ease-in-out;
-o-transition: opacity .7s ease-in-out;
transition: opacity .7s ease-in-out;
}
div.videoPreview:hover img.playButton {
opacity: 0;
}
div.videoPreviewBox p {
margin-top: 2%;
color: white;
line-height: 18px;
}
.player_wrapper{float: left;}
.player{float: left;clear: both;}
.scroller{margin-bottom: 35px; float: left;clear: both; width: 100%;}
.rssIcon{float: left;clear: both;}
.scroll_item{clear: left; float: left;}
.thumb_img{margin-right: 12px; margin-bottom: 25px; float: left; border-color: white; border-width: 2px; border-style: solid; background-color: #dddddd; height: 135px;}
.preview{ width: 600px; }.play_button{padding: 8px; background-color: #e9e9e9; border-color: #c9c9c9; border-width: 1px; border-style: solid; float: right;}
.rssIcon{width: 100%;}
.gallery_thumb{margin-bottom: 12px; margin-right: 12px; }
.gallery_thumb:hover{cursor: pointer;}
.mejs-container{margin-top: 20px!important}
And the full PHP gallery view file:
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$btID = $b->getBlockTypeID();
$bt = BlockType::getByID($btID);
$uh = Loader::helper('concrete/urls');
$rssUrl = $controller->getRssUrl($b);
$textHelper = Loader::helper("text");
$fl = Loader::model('file_version');
$uh = Loader::helper('concrete/urls');
$mh = Loader::helper('media_type','promedia');
$bt = BlockType::getByHandle('promedia_list');
$height = '400';
$width = '600';
$spread = 4;
$playerID = rand(0,777777);
if (count($cArray) > 0) { ?>
<div class="scroller">
<div id="media_galery">
<?php
$t = 0;
for ($i = 0; $i < count($cArray); $i++ ) {
$t++;
$file = $cArray[$i];
$remote = null;
$name = $file->getFileName();
$file_path = BASE_URL.DIR_REL.$file->getDownloadURL();
$ak_d = FileAttributeKey::getByHandle('media_date');
$date = $file->getAttribute($ak_d);
$ak_a = FileAttributeKey::getByHandle('media_artwork');
$image = $file->getAttribute($ak_a);
if($image==null){
$image_path=$uh->getBlockTypeAssetsURL($bt).'/tools/mp3.png';
}else{
$image = File::getByID($image->fID);
$image_path=$image->getURL();
}
$file_src = BASE_URL.DIR_REL.$file->getRelativePath();
$ak_s = FileAttributeKey::getByHandle('media_audio');
$audio = $file->getAttribute($ak_s);
if($audio){
$audio_array = $mh->getMediaTypeOutput($audio);
$audio_path = $audio_array['path'];
$file_src = $audio_path;
}
$video = null;
$ak_s = FileAttributeKey::getByHandle('media_video');
$video = $file->getAttribute($ak_s);
if($video){
$video_array = $mh->getMediaTypeOutput($video);
$video_path = $video_array['path'];
//var_dump($video_array['id']);
$image_path = $mh->getMediaThumbnail($video_array['id'],$video_array['type']);
//$video_src = $video_path.'?width='.$width.'&height='.$height;
//$file_src = $video_src;
if($video_array['type'] == 'vimeo'){
$file_src = 'http://player.vimeo.com/video/'.$video_array['id'];
}else{
$file_src = $video_path;
}
}
?>
<div class="media-thumb videoPreviewBox">
<div class="videoPreview">
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
<img class="playButtonHover" src="<?php echo $this->getThemePath()?>/images/playButtonHover.png">
<img class="playButton" src="<?php echo $this->getThemePath()?>/images/playButton.png">
</div>
</div>
<?php
if($t == $spread){
$t=0;
echo '</tr>';
}
}
for($d=$t;$d<$spread;$d++){
echo '<td></td>';
if($t == $spread){
echo '</tr>';
}
}
?>
</div>
</div>
<div style="display: none;">
<div id="popup_prep">
</div>
</div>
<script type="text/javascript">
/*<![CDATA[*/
$(document).ready(function() {
$('.gallery_thumb').fancybox({
width: 'auto',
height: 'auto',
onClosed: function(){
$('video, audio').each(function() {
$(this)[0].player.pause();
});
}
});
});
/*]]>*/
</script>
<script type="text/javascript">
/*<![CDATA[*/
$('.gallery_thumb').click(function(){
$('#popup_prep').html('');
var html = '';
var path = $(this).attr('alt');
//console.log(path);
var extension = path.substr( (path.lastIndexOf('.') +1) );
if(extension){
switch(extension) {
case 'm4v':
case 'mpeg':
case 'mpg':
case 'wmv':
case 'avi':
case 'mov':
case 'flv':
case 'f4v':
case 'mp4':
html = '<video class="player_box" id="play" controls="controls" src="'+path+'" width="<?php echo $width?>" height="<?php echo $height?>"></video><br style="clear: both;"/>';
break;
case 'mp3':
case 'm4a':
html = '<audio class="audioplayer" id="play" src="'+path+'" controls="controls" type="audio/m4a"></audio><br style="clear: both;"/>';
break;
default:
var iFrame = true;
html = '<iframe title="video player" width="<?php echo $width?>px" height="<?php echo $height?>px" src="'+path+'" frameborder="0" allowfullscreen></iframe>';
}
}
$('#popup_prep').append(innerShiv(html));
if(!iFrame){
var player = new MediaElementPlayer('video,audio');
}
});
/*]]>*/
</script>
<?php
}
if(!$previewMode && $controller->rss) {
?>
<div class="rssIcon">
<img src="<?php echo $uh->getBlockTypeAssetsURL($bt, 'rss.png')?>" width="14" height="14" />get this feed
</div>
<link href="<?php echo $rssUrl?>" rel="alternate" type="application/rss+xml" title="<?php echo $controller->rssTitle?>" />
<br/>
<?php
}
if ($paginate && $num > 0 && is_object($_fl)) {
$_fl->displayPaging();
}
?>
For full context, here is the page.
I'm not exactly a php expert, but I notice some problems in this line of code:
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
First of all, I'm not sure why there is a "/a" there. That is normally the end tag of a hyperlink. (if I put the <> there it disappears from the answer and gets read like html so lets pretend the "" is the same as <>).
Secondly, it looks like you got the stuff inside the quotes for href and alt mixed up. href is the link, alt is the description. Unless you want the description to be the same as the image link, but I would recoment something more fitting such as alt="Play Video".
Thirdly, check the number of spaces in the class. For example, if $playerID="123" and $i=1. The class would be the following: 123player_get1 gallery_thumb (notice the space between $i, or in this example the number 1, and gallery_thumb). Again I might be wrong but I don't think that space should be there and it can be fixed by eliminating the space beside the closing php tag.
Fourthly, you have two href="" in this img tag. Like I said I'm not an expert, but I'm assuming the first one need not be there and probably is causing trouble, as href is the link it will take you to when the image is clicked.
Fifthly, I'm not sure if it really matters or not, but normally an img tag doesn't have a /> at the end, it only requires a >.
Try substituting it for this line of code:
img src="<?php echo $image_path ?>" href="<?php echo $file_src ?>" alt="YOUR DESCRIPTION HERE" class="<?php echo $playerID ?>player_get<?php echo $i ?>gallery_thumb">
*edit: If it works make sure you understand why!

Categories