469,280 Members | 2,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

image gallery- image not displaying

116 100+
ok i have a folder made on the server and uploaded image files to it...the link to the image file or rather the path to the image is uploaded in the database in 'image_path' , now i want that the system read the path and resize it to generate a thumbnail and display it on the browser.

but the code is showing blank

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?
  3.  
  4. // generating a array with image paths 
  5.  
  6. $query_images = "SELECT image_path FROM userfolders" ;
  7. $result_images = mysql_query($query_images);
  8. confirm_query($result_images);
  9.  
  10. while ($record_images = mysql_fetch_assoc($result_images)) {
  11.           $image_list[] = $record_images['image_path'] ;
  12.  
  13. ?>
  14.  
  15. <?
  16. // generating a thumbnail 
  17.  
  18. $thumb_height = 100;
  19.  
  20. for ($i=0,$i<count($image_list),$i+)
  21. {
  22.  
  23. $filename = $image_list[$i];
  24. list($width, $height) = getimagesize($filename);
  25. $ratio = ($height/$width);
  26. $newheight = $thumb_height;
  27. $newwidth = ($thumb_height/$ratio);
  28.  
  29. $thumb = imagecreatetruecolor($newwidth, $newheight);
  30. $source = imagecreatefromjpeg($filename);
  31.  
  32. $thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  33.  
  34.   // displaying thumbnail
  35.  
  36. $thumbnail =  imagejpeg($thumb_image);
  37.  
  38. echo $thumbnail;
  39.  
  40.  
  41.  
  42.  
  43. ?>
  44.  
  45.  
Plz tell me wat i m doing wrong what lines i need to add or remove.

the image path is like d:/wamp/www/images/apple.jpg
May 12 '09
53 3752
angelicdevil
116 100+
i took the static values as follwing code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // generating a array with image paths
  4.  
  5.  
  6.  
  7. // generating a thumbnail
  8. $thumb_height = 100;
  9. $filename = "http://bytes.com/images/apple.jpg";
  10. list($width, $height) = getimagesize($filename);
  11.         $ratio = ($height/$width);
  12.         $newheight = $thumb_height;
  13.         $newwidth = ($thumb_height/$ratio);
  14.         echo  $newwidth;
  15.         $thumb = imagecreatetruecolor($newwidth, $newheight);
  16.         $source = imagecreatefromjpeg($filename);
  17.  
  18.         $thumbnail= imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  19.  
  20. // displaying thumbnail
  21.  
  22. header('Content-type: image/jpeg');
  23. imagejpeg($thumb);
  24. imagedestroy($thumb); 
  25.  
  26.  
  27.  
  28.  
  29.  
  30. ?>
  31.  
  32.  
i get the output as
http://localhost/test.php
cant seem to figure out y as phpinfo shows gd is enabled
May 28 '09 #51
Atli
5,058 Expert 4TB
Two problems there...
  1. To use URLs in that manner, the allow_url_fopen directive must be enabled.
  2. The URL 'http://bytes.com/images/apple.jpg' isn't a image.
Try an absolute file system path instead.

Anyways, you need to debug the code... find out exactly where it is failing.
Check the return values of each of the function calls. Make sure they are actually valid. (See my first post.)
May 28 '09 #52
angelicdevil
116 100+
code 1 :-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // generating a array with image paths
  4.  
  5. $query_images = "SELECT image_path,image_name FROM userfolders" ;
  6. $result_images = mysql_query($query_images);
  7. confirm_query($result_images);
  8.  
  9. while ($record_images = mysql_fetch_assoc($result_images)) {
  10.           $file = $record_images['image_name'] ;
  11.           $dirname = $record_images['image_path'] ;
  12.  
  13.  
  14. // generating a thumbnail
  15. $thumb_height = 100;
  16. $filename = $dirname."/".$file;
  17. list($width, $height) = getimagesize($filename);
  18.         $ratio = ($height/$width);
  19.         $newheight = $thumb_height;
  20.         $newwidth = ($thumb_height/$ratio);
  21.         echo  $newwidth;
  22.  $thumb = imagecreatetruecolor($newwidth, $newheight);
  23.  echo $thumb ;
  24.  $source = imagecreatefromjpeg($filename);
  25.  echo $source;
  26.  $thumbnail= imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  27.  echo $thumbnail;
  28.  
  29.  header('Content-type: image/jpeg');
  30. imagejpeg($thumbnail);
  31.  
  32. }
  33. ?>
  34.  
the o/p is as follows
109, resource id#10, resource id #12, 1
it seems the code is failing at imagejpeg command as soon as i put

code 2 :-
Expand|Select|Wrap|Line Numbers
  1. header('Content-type: image/jpeg');
  2. imagejpeg($thumb);
  3. imagedestroy($thumb); 
  4.  
the o/p displays only http://localhost/test.php
even the values of new width thumb source and thumbnail do not get displayed when the code 2 is inserted into code 1
Jun 1 '09 #53
Atli
5,058 Expert 4TB
Your code is failing because of all the echos in your code.

If you plan to output an image, you must only output the image data, or the image will be corrupted and the browser will not be able to display it.

In cases like these, you should only print debug messages if there is a problem.Use if statements to check the variables and stop the execution with an error message if it doesn't check out.
(See the way I use die() in the example in my first post)

But don't print debug messages when outputting binary data unless there is a problem.

P.S.
You are passing the wrong variable to the imagejpg function in your first code.
It should be getting $thumb rather than $humbnail.
Jun 1 '09 #54

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by nick | last post: by
2 posts views Thread by Daniel Kelly \(AKA Jack\) | last post: by
1 post views Thread by Simone Winkler | last post: by
10 posts views Thread by Captain Ranger McCoy | last post: by
5 posts views Thread by Fred | last post: by
1 post views Thread by desjardins.daniel | last post: by
11 posts views Thread by ste | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.