I have a PHP based image gallery using the Lightbox plug-in. When I run on MAMP, it works fine on a local server.
However, it fails when I upload the files to an SFTP client for my website.
I have the following code:
<?php
$directory = 'SpringPhotos17';
$thumbsdirectory = 'SpringPhotos17/_thumb';
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = #opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
$nomargin='';
if(in_array($ext,$allowed_types))
{
if(($i+1)%4==0) $nomargin='nomargin';
echo '
<div class="pic '.$nomargin.'" style="background:url('.$thumbsdirectory.'/'.$file.') no-repeat 50% 50%;">
'.$title.'
</div>';
$i++;
}
}
closedir($dir_handle);
?>
I inserted this PHP code within the HTML code for an image gallery. On the website, it comes up as:
'.$title.'
How can I resolve this issue? Thank you for your time and help in advance!
Related
I've been displaying using the below code for a few years to display a random image in WordPress without any issues. The code broke today when I upgraded to PHP 7.4. I'm stuck on how to make the code work again or replace it with new code so that images from a directory are randomly displayed.
*Update: Broken code means images are you no longer being displayed. There are no error messages our outputs, just a blank section where the random image would usually display.
<?php
$folder = '';
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
header('Location: '.$folder.$files[$rand]); // Voila!
?>
The file is called rotate.php and it's located within the images directory. I call the rotate.php script from within the website's css style sheet using the following code:
background: #ffffff url(images/rotate.php) no-repeat center top;
Thanks for your help.
I'm trying to paginate an arbitrary number of photos that I'm getting from a image directory onto my web page.
Here is my PHP script I'm running thus far:
<?php
$dir = "images/";
$counter = 1;
if($opendir = opendir($dir)){
//read directory
while(($file = readdir($opendir)) !== FALSE){
if($file != "." && $file != ".."){
echo "<img src='$dir/$file' alt='Picture broken' style='border-style: solid;border-width: 2px;border-color: #4d94ff; margin: 5px'>";
$counter++;
}
}
}
echo "<p>There are $counter images</p>";
?>
How do I get this to paginate automatically with 10 images on each page?
save all your images name of specific directory in a global array and paginate it with you own will :)
I finally got my code to populate a gallery from a directory while using fancybox. It's a basic thumbnail gallery that shows the larger image upon clicking. The only problem is that it is missing a lot of files from the thumbnail directory.
The code retrieves all of the links for the larger images but it won't retrieve ALL of the thumbnails, only a few of them and they're not even in order.
What is wrong in my code?
<?php
$directory = 'thumb'; //where the gallery thumbnail images are located
$allowed_types=array('jpg','jpeg','gif','png');//allowed image types
$file_parts=array(); $ext=''; $title=''; $i=0;//try to open the directory
$dir_handle = #opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle)) //traverse through the files
{ if($file=='.' || $file == '..') continue; //skip links to the current and parent directories
$file_parts = explode('.',$file); //split the file name and put each part in an array
$ext = strtolower(array_pop($file_parts)); //the last element is the extension
$title = implode('.',$file_parts); //once the extension has been popped out, all that is left is the filename
$title = htmlspecialchars($title); //make the filename html-safe to prevent potential security issues
natsort($file_parts); //sort by filename--NOT WORKING
$nomargin='';
if(in_array($ext,$allowed_types)) //check if the extension is an allowable type
{
if(($i+1)%4==0) $nomargin='nomargin'; //the last image on the row is assigned the CSS class "nomargin"
//Begin thumbs containers with fancybox class
echo '<div class="thumbs fancybox '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;"> <a rel="group"
href="images/'.$file.'" title="'.$title.'">'.$title.'</a>
</div>';
$i=0; //increment the image counter
} } closedir($dir_handle); //close the directory
?>
An alternative approach you may want to use to secure your list of thumbnails:
$directory = 'thumb'; //where the gallery thumbnail images are located
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename
Then to render it out simply do this:
<?php
for($x = 0; $x < count($files); $x++):
$thumb = $files[$x];
$file = basename($thumb);
$nomargin = $x%4 == 0?" nomargin":"";
$title = htmlspecialchars($file);
?>
<div class="thumbs fancybox<?= $nomargin ?>"
style="background:url('<?= $thumb ?>') no-repeat 50% 50%;">
<a rel="group" href="images/'.<?= $file ?>" title="<?= $title ?>"><?= $title ?></a>
</div>
<?php
endfor;
I've been trying for days to display a thumbnail of my weather cam. The server path is as follows
public_html
..cam
...20121012
...20121013
...20121014
The date folder are auto created everyday by my webcam settings. So everyday it will create a new folder and name it date('Ymd').
I'm using the following script to have the image displayed on my wordpress sidebar, but I can't get it to work. Furthermore, I would like to have it displayed as a thumbnail and when a visitor clicks on it, to have it open slightly larger (real size) in a popup screen.
<?php
chdir('/home/deb57301n2/domains/meteowestkust.be/public_html/cam/');
$subdirname = date('Ymd').'/';
echo getcwd();
$newest_mtime = 0;
$show_file = 'webcam_offline.png';
if ($handle = opendir($subdirname)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..')) {
$mtime = filemtime($subdirname.$file);
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = $subdirname.$file;
}
}
}
}
print '<img src="' .$show_file. '" alt="Weather Cam - West Coast - Belgium">';
?>
any help is highly appreciated !
It worked by adding
$show_file = '/cam/'.$subdirname.$file;
thanks for the help anyway guys !
I'm trying to improve the administrator panel of my website. I need to preview the images in the thumbnails folder so that when i'm using thumbnails for news I dont have to upload the image for the second time. I found a great script, but I get failed to read the directory error. Here is the script:
<?php
// filetypes to display
$imagetypes = array("image/jpeg", "image/gif", "image/png");
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function getImages($dir)
{
global $imagetypes;
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
// full server path to directory
$fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";
$d = #dir($fulldir) or die("getImages: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
// check for image files
$f = escapeshellarg("$fulldir$entry");
$mimetype = trim(`file -bi $f`);
foreach($imagetypes as $valid_type) {
if(preg_match("#^{$valid_type}#", $mimetype)) {
$retval[] = array(
'file' => "/$dir$entry",
'size' => getimagesize("$fulldir$entry")
);
break;
}
}
}
$d->close();
return $retval;
}
// fetch image details
$images = getImages("../images/thumbnails");
// display on page
foreach($images as $img) {
echo "<div class=\"photo\">";
echo "<img src=\"{$img['file']}\" {$img['size'][3]} alt=\"\"><br>\n";
// display image file name as link
echo "",basename($img['file']),"<br>\n";
// display image dimenstions
echo "({$img['size'][0]} x {$img['size'][1]} pixels)<br>\n";
// display mime_type
echo $img['size']['mime'];
echo "</div>\n";
}
?>
I really appreciate if someone could help..
EDIT:
<div style=" height: 200px; width: 600px; overflow: auto;">
<?PHP
foreach(glob("../thumbnail/".'*') as $filename){
echo "<div style=\"display:inline-table; font-size:10px; font-family:'Tahoma'; margin:5px;\">";
echo "<img width=\"100px\" height=\"100px\" src=\"../thumbnail/$filename\"/>";
echo "<br>".basename($filename) . "<br>";
echo "</div>";
}
?>
</div>
This method works perfect. No need to use complicated scripts.
Anyway, can somebody please tell me how to check images less than 100px x 100px displayed?
<?PHP
foreach(glob("../thumbnail/".'*') as $filename){
list($width, $height, $type, $attr) = getimagesize("../thumbnail/".$filename);
if($width>=100 || $height >=100) continue;
$rest = substr($filename, 3);
?>
That should do it..