bootstrap image row out of place - php

[SOLVED]
I'm have written a horizontal row of 4 images 500x300 using placeholders, this works perfectly fine, but when I try to replace those images with images from a folder (using php) of the same dimensions, the first 2 image containes are knocked out of place while the last 2 are where they belong.. I've attached a screenshot and the code below is what I'm using to get the row of images.
<div class="row" style="margin-top:1px">
<?php
$directory = 'images';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
foreach (scandir($directory) as $file) {
?>
<div class="col-sm-3 col-xs-6" >
<a href="#" >
<img class="img-responsive portfolio-item" src="<?php echo "images/$file"; ?>" alt="">
</a>
</div>
<?php
}
?>
</div>
this is what i get in the browser

Using glob instead of an array fixed the problem. PHP is including the . and double dot of the directory structure and therefore creating 2 empty images.

Related

Change part of URL string for images with PHP

In my MySQL database, the image links are saved in the format like http://www.old.com/image1.jpg. But I had to change the domains of the images and the new links appear like images.new.com/image1.jpg. I have been changing the images links with jQuery with the following function:
$(document).ready(function() {
$('img').each(function() {
var src = $(this).attr('src');;
$(this).attr('src', src.replace('www.old', 'images.new'));
});
});
But I am wondering is there any way to change part of the URL strings with PHP. I am getting the image URLs with the following PHP function.
<?php
$imageQuery = $mysqli->query("SELECT imageURL FROM images WHERE album = 'UK' ORDER BY date ASC");
$images = $imageQuery->fetch_all(MYSQLI_ASSOC);
$images = array_chunk($images, 2);
?>
<div class="row">
<div class="col-4" id="box1">
<?php foreach (array_column($images, 0) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
<div class="col-4" id="box2">
<?php foreach (array_column($images, 1) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
</div>
With PHP how can I echo the new links for the images directly here in the img src? <img class="img-fluid" src="<?= $image['imageURL']; \\modified link here ?>">
PHP has it's own method for replacing strings, str_replace. The equivalent to your jQuery in PHP is:
str_replace('www.old', 'images.new', $image['imageURL'])
A better idea would be to update the values in your database.
An even better idea would be to not store the duplicate root URLs anywhere, and stitch them together in the application if you need absolute URLs, for some reason.
You could alternatively convert the text in the sql select statement:
SELECT REPLACE(imageURL, 'www.old', 'images.new') AS imageURL FROM ...
By using AS imageURL none of the rest of the code would need to change.

Getting Images With PHP And Display In Script

I have a blog and I want to be able to show images that are stored in a folder using a simple PHP script. Here is the PHP:
<?php
$folder = "$segment_url/files/photos/$post_year/$post_id/";
$filetype = ".jpg";
$files = glob($folder.$filetype, GLOB_BRACE);
foreach ($files as $file)
{
echo "
<img class=\"galleryPhoto\" src=\"$file\" /> // PROBLEM 1
";
}
?>
<?php echo $folder; ?>
<?php
$display_content = "
<div class=\"pageSection text\">
$content_intro
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
// PROBLEM 2
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
$content_conclusion
</div>
";
?>
In reference to the above code:
Problem 1: Even though the path to the images folder is correct, no images are being displayed.
Problem 2: How do I get the echoed content to be displayed here rather than at the top of the page?
For Problem 1 : You are missing to use full image path, otherwise search will search in current directory. Try like this.
<img class=\"galleryPhoto\" src=\"$folder/$file\" /> // PROBLEM 1

remove file extensions from a file directory in php

I am doing a gallery and using php to get images to show up without hard-coding for each image. I had used the following method for obtaining the images:
<?php
$dir="image/";
//open dir
if ($opendir=opendir($dir))
{
//readdir
while($file=readdir($opendir))
{
if ($file!="."&&$file!="..")
echo "
<div id='item' class='grid-item'>
<a href='$dir/$file'>
<div id='masks' class='mask'></div>
<div id='title' class='text'>$file</div>
<img src='$dir/$file'>
</a>
</div>
";
}
}
?>
The gallery uses jquery for the masonry gallery plugin.
However the file extension is showing up along with the file name. I don't want the extension. I have tried many methods, but they're not working for this case. How can I hide it and what changes can be done to the code in order for it to happen?
You can use pathinfo:
<?php
$dir="image/";
//open dir
if ($opendir=opendir($dir))
{
//readdir
while($file=readdir($opendir))
{
if ($file!="."&&$file!="..")
{
$filename = pathinfo($file, PATHINFO_FILENAME);
echo "
<div id='item' class='grid-item'>
<a href='$dir/$file'>
<div id='masks' class='mask'></div>
<div id='title' class='text'>$filename</div>
<img src='$dir/$file'>
</a>
</div>
";
}
}
}
To get the filename via this method, you must use PHP > 5.2, which you probably should anyway.

Create new line for each seperated element in field (database)

Here's my code :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2'].'" >
</div>
In that "img2" row in the database, it's image path. But I need to detect if there's more than one image path seperated by a comma in there.
So if there's 2 images in the field (ex.: img/image1.jpg, img/image2.jpg), well the HTML would look like :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2(first value)'].'" >
<img src="'.$row['img2(second value)'].'" >
</div>
But if there's only 1 value, it stays like my first code part.
Anyone? Thanks a lot
use the explode function and then a foreach loop
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
?>
<img src="<?php echo $image; ?>" >
<?php } ?>
</div>

Condition put on images

I want to put a condition on images come from directory that if the images count is 1 then show this div. But the condition is not running properly after applying the condition on it. Please help me to resolve this problem: My code is given below:
<?php
$id = 115;
$path_image = "pageimage/".$id."/";
if(file_exists($path_image)){
$dir = dir($path_image);
//List files in images directory
while (($file = $dir->read()) !== false){
if($file>=1){
?>
<div id="middleimg" style="background:url(<?php echo $path_image.$file; ?>); background-repeat:no-repeat;">
<div id="nav">
<div id="navinner">
<ul>
<?php
foreach($pageMenu as $pmenu){
$url = generateURL($pmenu->custom_url, $pmenu->page_friendlyname, $pmenu->page_title, $pmenu->page_id);?>
<li><?php echo $pmenu->page_name; ?> </li>
<?php } ?>
<!--<li>HOME</li>
<li>ABOUT</li>
<li>MENU</li>
<li>LOCATION</li>
<li>DELIVERY</li>
<li>SPECIAL OFFER</li>
<li>CONTACT</li>-->
</ul>
</div>
</div>
<div id="banner-containerhome">
<div id="promoBanner">
<!--<div id="pizza">-->
<div id="wowslider-container1">
<div class="ws_images">
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
</div>
<div class="ws_bullets"><div>
<img src="data1/tooltips/banner.jpg" alt=""/>1
<img src="data1/tooltips/chrysanthemum.jpg" alt=""/>2
</div></div>
<div class="ws_shadow"></div>
</div>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- </div>-->
</div>
<div class="rightcontents">
<?php
echo $page_content[1]->page_content;
?>
</div>
</div>
</div>
<?php }}} ?>
There are a few things...
1.- read() reads the directory as an iterator, returning the file name, have you tried to echo $file; ?
if($file >= 1) will never be true, as $file is always a String, never an integer, you are comparing there: if("myfilename.ext" >= 1).
To count the total files you should do a while through the full directory and use a counter, increasing it for each file.
Or you can use scandir("myPath") function, that get all the files in the path specified as an array. for example:
$dir = scandir("myDir");
$total = count($dir); // this gives you the lenght of the array, therefore the files.
While counting, remember that the parent, and root directory are counted as "files" as well, and with some methods also the thumbs.db(windows) or DS_store(osx) are hidden files that store information about how you sort the folder, folder icons and stuff, will be counted as well, so remember to add an If statement to skip them.

Categories