| | |
Photo Gallery
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
Hello all, first time working with PHP, had a question. Working on a friend's website, we thought we'd be wicked cool and add a dynamic photo gallery. I stunbles across this neat pile of code:
It works great! Just two things. I am having trouble re-defining where the images are read from. Right now it is the root, but, we want a folder called images to store content. And, we want it to read tif, and gif images. Right now it only reads jpg. I tried copy and pasting the sections that referenced file types and replacing gif as extension, would just throw up errors. I am not very experienced with the language. Any help, very appreciated. Cheers!
php Syntax (Toggle Plain Text)
<?php $columns = 2; $thmb_width = 120; $thmb_height = 80; function resizeImage($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } function generateThumbnails(){ global $thmb_width,$thmb_height; // Open the actual directory if ($handle = opendir(".")) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ $isThumb = true; } else { $isThumb = false; } if (!$isThumb) { // Process the file string $dirName = substr($file,0,strpos($file,basename($file))); if (strlen($dirName) < 1) $dirName = '.'; $fileName = basename($file); $fileMain = substr($fileName,0,strrpos($fileName,'.')); $extName = substr($fileName,strrpos($fileName,'.'), strlen($fileName)-strrpos($fileName,'.')); // Check if the actual file is a jpeg image if (($extName == '.jpg') || ($extName == '.jpeg')){ $thmbFile = $dirName.'/'.$fileMain.'_th.jpg'; // If a thumbnail dosn't exists tahn create a new one if (!file_exists($thmbFile)){ imagejpeg(resizeImage($file,$thmb_width,$thmb_height) ,$thmbFile,80); } } } } } } } function getNormalImage($file){ $base = substr($file,0,strrpos($file,'_th.jpg')); if (file_exists($base.'.jpg')) return $base.'.jpg'; elseif (file_exists($base.'.jpeg')) return $base.'.jpeg'; else return ""; } function displayPhotos(){ global $columns; generateThumbnails(); $act = 0; // Open the actual directory if ($handle = opendir(".")) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ ++$act; if ($act > $columns) { echo '</tr><tr> <td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/></a></td>'; $act = 1; } else { echo '<td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/></a></td>'; } } } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <head> <title>Photo Gallery</title> </head> <body> <center><h3>Photo Gallery</h3></center> <table align="center"><tr> <?php displayPhotos(); ?> </table> </body> </html>
It works great! Just two things. I am having trouble re-defining where the images are read from. Right now it is the root, but, we want a folder called images to store content. And, we want it to read tif, and gif images. Right now it only reads jpg. I tried copy and pasting the sections that referenced file types and replacing gif as extension, would just throw up errors. I am not very experienced with the language. Any help, very appreciated. Cheers!
The lines you want to change are
opendir(".") , this tells it to look in the current directory (where the script is located) May be worth adding a variable for directory at the top with the attributes and replacing the "." with it, there are 2 instances of opendir, so replace "." with $directory or whatever you call the variable. AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
Well, I changed my code thusly:
...and now nothing shows up. I have tried pointing it to different folders, all with the same result. They all contain jpgs, not sure why it doesn't work. And if anyone knows how to add file extensions, or would just recommend to convert them all to jpegs, I would appreciate it. Cheers.
PHP Syntax (Toggle Plain Text)
<?php $columns = 2; $thmb_width = 120; $thmb_height = 80; $directory = "G:/xampp/xampp/htdocs/"; function resizeImage($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } function generateThumbnails(){ global $thmb_width,$thmb_height; // Open the actual directory if ($handle = opendir($directory)) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ $isThumb = true; } else { $isThumb = false; } if (!$isThumb) { // Process the file string $dirName = substr($file,0,strpos($file,basename($file))); if (strlen($dirName) < 1) $dirName = '.'; $fileName = basename($file); $fileMain = substr($fileName,0,strrpos($fileName,'.')); $extName = substr($fileName,strrpos($fileName,'.'), strlen($fileName)-strrpos($fileName,'.')); // Check if the actual file is a jpeg image if (($extName == '.jpg') || ($extName == '.jpeg')){ $thmbFile = $dirName.'/'.$fileMain.'_th.jpg'; // If a thumbnail dosn't exists tahn create a new one if (!file_exists($thmbFile)){ imagejpeg(resizeImage($file,$thmb_width,$thmb_height) ,$thmbFile,80); } } } } } } } function getNormalImage($file){ $base = substr($file,0,strrpos($file,'_th.jpg')); if (file_exists($base.'.jpg')) return $base.'.jpg'; elseif (file_exists($base.'.jpeg')) return $base.'.jpeg'; else return ""; } function displayPhotos(){ global $columns; generateThumbnails(); $act = 0; // Open the actual directory if ($handle = opendir($directory)) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ ++$act; if ($act > $columns) { echo '</tr><tr> <td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/></a></td>'; $act = 1; } else { echo '<td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/></a></td>'; } } } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <head> <title>Photo Gallery</title> </head> <body> <center><h3>Photo Gallery</h3></center> <table align="center"><tr> <?php displayPhotos(); ?> </table> </body> </html>
...and now nothing shows up. I have tried pointing it to different folders, all with the same result. They all contain jpgs, not sure why it doesn't work. And if anyone knows how to add file extensions, or would just recommend to convert them all to jpegs, I would appreciate it. Cheers.
what are the file extensions? jpg, jpeg, JPG or JPEG?
Have you had a look at the source code of the HTML produced to see what it is actually outputting?
Have you had a look at the source code of the HTML produced to see what it is actually outputting?
Last edited by Will Gresham; Feb 19th, 2009 at 9:31 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
Well, the files are all JPEG extensions. When leaving the location as ".", all the JPEG and JPG files show up on the page. When using the $dir, I get a page with just the Photo Gallery title, no pictures, even though it is pointing to the same exact place. View page source, just shows the html at the bottom of the above code, without the php call to the displayPhotos() function. I believe that is what you are asking. I know in Python there is the stdin/stdout to read what the input/output, I don't know if php has a similar function, and that is what you are referring to.
Last edited by TheNational22; Feb 20th, 2009 at 2:39 pm.
Had another look, you should define $directory as a global in the functions, just as the $thmb and $columns variables are.
Also, use a relative directory, change
Also, use a relative directory, change
$directory = "G:/xampp/xampp/htdocs/"; to $directory = "."; and see if you get the correct output. AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
I'm coming in to the middle part of this
but whenever I have something that works with a fixed value, that doesnt work with a variable, I left the quotes out
Debugging is fastest if you use the same value of the variable as the original fixed value
index.php?directory=.
check the exact format of the variable
if the original is
but whenever I have something that works with a fixed value, that doesnt work with a variable, I left the quotes out
Debugging is fastest if you use the same value of the variable as the original fixed value
index.php?directory=.
check the exact format of the variable
if the original is
readdir("."); the replacement variable should be readdir("$directory"); the quotes are still required and dquotes should parse properly Last edited by almostbob; Feb 20th, 2009 at 5:19 pm. Reason: my eyes are getting so bad, cant see syntax errors
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
Thanks both of you, first off. Now I have added the $dir variable as a global, and when designating the path as ".", I get the thumbs to display. When I change it to G:\xampp\xampp\htdocs\images, I get:
Warning: opendir(G: mpp mpp\htdocs\images) failed to open
The issue here looks like an escape character issue, yet only applied to the fist two directories. In python, \ is the escape character (ie: G:\\xampp\\xampp\\htdocs\\images), so I'm not sure the issue here. Thanks again.
Warning: opendir(G: mpp mpp\htdocs\images) failed to open
The issue here looks like an escape character issue, yet only applied to the fist two directories. In python, \ is the escape character (ie: G:\\xampp\\xampp\\htdocs\\images), so I'm not sure the issue here. Thanks again.
•
•
Join Date: Apr 2008
Posts: 20
Reputation:
Solved Threads: 2
•
•
•
•
Thanks both of you, first off. Now I have added the $dir variable as a global, and when designating the path as ".", I get the thumbs to display. When I change it to G:\xampp\xampp\htdocs\images, I get:
Warning: opendir(G: mpp mpp\htdocs\images) failed to open
The issue here looks like an escape character issue, yet only applied to the fist two directories. In python, \ is the escape character (ie: G:\\xampp\\xampp\\htdocs\\images), so I'm not sure the issue here. Thanks again.
"G:/xampp/xampp/htdocs/images/"
I'm fairly sure it's only windows that does it this way - "\".
Secondly, have you tried a relative path instead of an absolute one?
ie. If your script is in "htdocs", instead of giving the address as "G:/xampp/xampp/htdocs/images/" all it would need to be is "images/"
Just some thoughts...
Last edited by jedi_ralf; Feb 20th, 2009 at 5:42 pm.
index.php?directory=G:\xampp\xampp\htdocs\images
I get errors if the directory is not quoted
quotes are still important
the variable gets and works
every function("string") has to enclose the string in quotes,
include("filename")
fopen("filename")
fclose("filename")
I get errors if the directory is not quoted
PHP Syntax (Toggle Plain Text)
opendir(G:\xampp\xampp\htdocs\images)
PHP Syntax (Toggle Plain Text)
opendir(".")// note the quotes in the original opendir("G:\xampp\xampp\htdocs\images")
the variable gets
php Syntax (Toggle Plain Text)
opendir("$directory") // Note the quotes
every function("string") has to enclose the string in quotes,
include("filename")
fopen("filename")
fclose("filename")
Last edited by almostbob; Feb 20th, 2009 at 7:13 pm.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
![]() |
Similar Threads
- Photoshop, Javascript Photo Gallery Help (JavaScript / DHTML / AJAX)
- Photo Gallery (PHP)
- Photoshop web photo gallery to dreamweaver (Site Layout and Usability)
- Photo Gallery. (PHP)
- ecommerce online photo gallery (eCommerce)
- php photo gallery extra code needed (PHP)
Other Threads in the PHP Forum
- Previous Thread: What is wrong with the function?
- Next Thread: php regex for form validation
| Thread Tools | Search this Thread |
# 5.2.10 action address apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo echo$_get[x]changingitintovariable... email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image images include insert ip javascript joomla legislation limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop open paypal pdf persist php popup problem query radio random record recursion remote script search server sessions sms sockets source space sql syntax system table tutorial update upload url validator variable video web youtube






