Photo Gallery

Reply

Join Date: Sep 2008
Posts: 25
Reputation: TheNational22 is an unknown quantity at this point 
Solved Threads: 0
TheNational22 TheNational22 is offline Offline
Light Poster

Photo Gallery

 
0
  #1
Feb 19th, 2009
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:
  1. <?php
  2.  
  3. $columns = 2;
  4. $thmb_width = 120;
  5. $thmb_height = 80;
  6.  
  7. function resizeImage($originalImage,$toWidth,$toHeight){
  8.  
  9. // Get the original geometry and calculate scales
  10. list($width, $height) = getimagesize($originalImage);
  11. $xscale=$width/$toWidth;
  12. $yscale=$height/$toHeight;
  13.  
  14. // Recalculate new size with default ratio
  15. if ($yscale>$xscale){
  16. $new_width = round($width * (1/$yscale));
  17. $new_height = round($height * (1/$yscale));
  18. }
  19. else {
  20. $new_width = round($width * (1/$xscale));
  21. $new_height = round($height * (1/$xscale));
  22. }
  23. // Resize the original image
  24. $imageResized = imagecreatetruecolor($new_width, $new_height);
  25. $imageTmp = imagecreatefromjpeg ($originalImage);
  26. imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0,
  27. $new_width, $new_height, $width, $height);
  28.  
  29. return $imageResized;
  30. }
  31.  
  32. function generateThumbnails(){
  33. global $thmb_width,$thmb_height;
  34.  
  35. // Open the actual directory
  36. if ($handle = opendir(".")) {
  37. // Read all file from the actual directory
  38. while ($file = readdir($handle)) {
  39. // Check whether tha actual item is a valid file
  40. if (is_file($file)){
  41. // Check whether the actual image is a thumbnail
  42. if (strpos($file,'_th.jpg')){
  43. $isThumb = true;
  44. } else {
  45. $isThumb = false;
  46. }
  47.  
  48. if (!$isThumb) {
  49. // Process the file string
  50. $dirName = substr($file,0,strpos($file,basename($file)));
  51. if (strlen($dirName) < 1) $dirName = '.';
  52. $fileName = basename($file);
  53. $fileMain = substr($fileName,0,strrpos($fileName,'.'));
  54. $extName = substr($fileName,strrpos($fileName,'.'),
  55. strlen($fileName)-strrpos($fileName,'.'));
  56.  
  57. // Check if the actual file is a jpeg image
  58. if (($extName == '.jpg') || ($extName == '.jpeg')){
  59. $thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
  60. // If a thumbnail dosn't exists tahn create a new one
  61. if (!file_exists($thmbFile)){
  62. imagejpeg(resizeImage($file,$thmb_width,$thmb_height)
  63. ,$thmbFile,80);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70.  
  71. }
  72.  
  73. function getNormalImage($file){
  74. $base = substr($file,0,strrpos($file,'_th.jpg'));
  75. if (file_exists($base.'.jpg')) return $base.'.jpg';
  76. elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
  77. else return "";
  78. }
  79.  
  80. function displayPhotos(){
  81. global $columns;
  82.  
  83. generateThumbnails();
  84. $act = 0;
  85. // Open the actual directory
  86. if ($handle = opendir(".")) {
  87. // Read all file from the actual directory
  88. while ($file = readdir($handle)) {
  89. // Check whether tha actual item is a valid file
  90. if (is_file($file)){
  91. // Check whether the actual image is a thumbnail
  92. if (strpos($file,'_th.jpg')){
  93. ++$act;
  94. if ($act > $columns) {
  95. echo '</tr><tr>
  96. <td class="photo"><a href="'.getNormalImage($file).'">
  97. <img src="'.$file.'" alt="'.$file.'"/></a></td>';
  98. $act = 1;
  99. } else {
  100. echo '<td class="photo"><a href="'.getNormalImage($file).'">
  101. <img src="'.$file.'" alt="'.$file.'"/></a></td>';
  102. }
  103.  
  104. }
  105. }
  106. }
  107. }
  108. }
  109.  
  110. ?>
  111. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  112. "DTD/xhtml1-transitional.dtd">
  113. <html>
  114. <head>
  115. <title>Photo Gallery</title>
  116. </head>
  117. <body>
  118. <center><h3>Photo Gallery</h3></center>
  119. <table align="center"><tr>
  120. <?php displayPhotos(); ?>
  121. </table>
  122. </body>
  123.  
  124.  
  125. </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!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Photo Gallery

 
0
  #2
Feb 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: TheNational22 is an unknown quantity at this point 
Solved Threads: 0
TheNational22 TheNational22 is offline Offline
Light Poster

Re: Photo Gallery

 
0
  #3
Feb 19th, 2009
Well, I changed my code thusly:

  1. <?php
  2.  
  3. $columns = 2;
  4. $thmb_width = 120;
  5. $thmb_height = 80;
  6. $directory = "G:/xampp/xampp/htdocs/";
  7.  
  8. function resizeImage($originalImage,$toWidth,$toHeight){
  9.  
  10. // Get the original geometry and calculate scales
  11. list($width, $height) = getimagesize($originalImage);
  12. $xscale=$width/$toWidth;
  13. $yscale=$height/$toHeight;
  14.  
  15. // Recalculate new size with default ratio
  16. if ($yscale>$xscale){
  17. $new_width = round($width * (1/$yscale));
  18. $new_height = round($height * (1/$yscale));
  19. }
  20. else {
  21. $new_width = round($width * (1/$xscale));
  22. $new_height = round($height * (1/$xscale));
  23. }
  24. // Resize the original image
  25. $imageResized = imagecreatetruecolor($new_width, $new_height);
  26. $imageTmp = imagecreatefromjpeg ($originalImage);
  27. imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0,
  28. $new_width, $new_height, $width, $height);
  29.  
  30. return $imageResized;
  31. }
  32.  
  33. function generateThumbnails(){
  34. global $thmb_width,$thmb_height;
  35.  
  36. // Open the actual directory
  37. if ($handle = opendir($directory)) {
  38. // Read all file from the actual directory
  39. while ($file = readdir($handle)) {
  40. // Check whether tha actual item is a valid file
  41. if (is_file($file)){
  42. // Check whether the actual image is a thumbnail
  43. if (strpos($file,'_th.jpg')){
  44. $isThumb = true;
  45. } else {
  46. $isThumb = false;
  47. }
  48.  
  49. if (!$isThumb) {
  50. // Process the file string
  51. $dirName = substr($file,0,strpos($file,basename($file)));
  52. if (strlen($dirName) < 1) $dirName = '.';
  53. $fileName = basename($file);
  54. $fileMain = substr($fileName,0,strrpos($fileName,'.'));
  55. $extName = substr($fileName,strrpos($fileName,'.'),
  56. strlen($fileName)-strrpos($fileName,'.'));
  57.  
  58. // Check if the actual file is a jpeg image
  59. if (($extName == '.jpg') || ($extName == '.jpeg')){
  60. $thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
  61. // If a thumbnail dosn't exists tahn create a new one
  62. if (!file_exists($thmbFile)){
  63. imagejpeg(resizeImage($file,$thmb_width,$thmb_height)
  64. ,$thmbFile,80);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71.  
  72. }
  73.  
  74. function getNormalImage($file){
  75. $base = substr($file,0,strrpos($file,'_th.jpg'));
  76. if (file_exists($base.'.jpg')) return $base.'.jpg';
  77. elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
  78. else return "";
  79. }
  80.  
  81. function displayPhotos(){
  82. global $columns;
  83.  
  84. generateThumbnails();
  85. $act = 0;
  86. // Open the actual directory
  87. if ($handle = opendir($directory)) {
  88. // Read all file from the actual directory
  89. while ($file = readdir($handle)) {
  90. // Check whether tha actual item is a valid file
  91. if (is_file($file)){
  92. // Check whether the actual image is a thumbnail
  93. if (strpos($file,'_th.jpg')){
  94. ++$act;
  95. if ($act > $columns) {
  96. echo '</tr><tr>
  97. <td class="photo"><a href="'.getNormalImage($file).'">
  98. <img src="'.$file.'" alt="'.$file.'"/></a></td>';
  99. $act = 1;
  100. } else {
  101. echo '<td class="photo"><a href="'.getNormalImage($file).'">
  102. <img src="'.$file.'" alt="'.$file.'"/></a></td>';
  103. }
  104.  
  105. }
  106. }
  107. }
  108. }
  109. }
  110.  
  111. ?>
  112. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  113. "DTD/xhtml1-transitional.dtd">
  114. <html>
  115. <head>
  116. <title>Photo Gallery</title>
  117. </head>
  118. <body>
  119. <center><h3>Photo Gallery</h3></center>
  120. <table align="center"><tr>
  121. <?php displayPhotos(); ?>
  122. </table>
  123. </body>
  124.  
  125.  
  126. </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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Photo Gallery

 
0
  #4
Feb 19th, 2009
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?
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: TheNational22 is an unknown quantity at this point 
Solved Threads: 0
TheNational22 TheNational22 is offline Offline
Light Poster

Re: Photo Gallery

 
0
  #5
Feb 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Photo Gallery

 
1
  #6
Feb 20th, 2009
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 $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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,325
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: Photo Gallery

 
0
  #7
Feb 20th, 2009
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 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: TheNational22 is an unknown quantity at this point 
Solved Threads: 0
TheNational22 TheNational22 is offline Offline
Light Poster

Re: Photo Gallery

 
0
  #8
Feb 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 20
Reputation: jedi_ralf is an unknown quantity at this point 
Solved Threads: 2
jedi_ralf jedi_ralf is offline Offline
Newbie Poster

Re: Photo Gallery

 
0
  #9
Feb 20th, 2009
Originally Posted by TheNational22 View Post
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.
Firstly, should the slashes not be forward slashes, rather than back slashes?
"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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,325
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: Photo Gallery

 
0
  #10
Feb 20th, 2009
index.php?directory=G:\xampp\xampp\htdocs\images
I get errors if the directory is not quoted

  1. opendir(G:\xampp\xampp\htdocs\images)
  1. opendir(".")// note the quotes in the original
  2. opendir("G:\xampp\xampp\htdocs\images")
quotes are still important
the variable gets
  1. opendir("$directory") // Note the quotes
and works

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC