469,315 Members | 2,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 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 #1
53 3756
code green
1,726 Expert 1GB
imagecopyresized() returns a boolean, not an image resource
which imagejpeg() expects
May 12 '09 #2
angelicdevil
116 100+
so wat shud it be? plz tell
May 12 '09 #3
angelicdevil
116 100+
i even tried tht line with imagecopyresampled() still same thing
May 12 '09 #4
code green
1,726 Expert 1GB
Never used this so I would have to study manual to get it right.
Have a look here http://us2.php.net/imagejpeg
May 12 '09 #5
angelicdevil
116 100+
i did ...the examples show similar thing ...cant understand y the images are not getting displayed
May 13 '09 #6
code green
1,726 Expert 1GB
The examples show the correct method, but you have not followed them.
My first post explained what you had done wrong.
I am not re-writing the code because I have never used this.
But below I am pointing out where you have gone wrong
Expand|Select|Wrap|Line Numbers
  1. //This returns a resource
  2. $thumb = imagecreatetruecolor($newwidth, $newheight); 
  3. //This also returns a resource
  4. $source = imagecreatefromjpeg($filename); 
  5. ###NOTE returns a boolean#####    
  6. $thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
  7. #######also returns a boolean and expects an image resource. 
  8. $thumbnail =  imagejpeg($thumb_image); 
  9. //You are supplying a boolean " $thumb_image "
  10. ####THEN trying to echo a boolean $thumbnail #######
  11. echo $thumbnail; 
May 13 '09 #7
angelicdevil
116 100+
ok so what should it be? plz tell as all examples i saw seem to use this way
May 13 '09 #8
code green
1,726 Expert 1GB
I get the impression you don't understand the difference
between a boolean and a resource!
I have tried to gently point you in the right direction
but you you are not interested in learning. Only in copying.
So copy this.
Expand|Select|Wrap|Line Numbers
  1. //This returns a resource 
  2. $thumb = imagecreatetruecolor($newwidth, $newheight);  
  3. //This also returns a resource ,Which one are you using????????
  4. $source = imagecreatefromjpeg($filename);     
  5. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);   
  6. imagejpeg($thumb);  
  7. echo $thumb;  
I think you need to check how to correctly display the image, because a resource is a pointer and I doubt echo is the right thing to do here,
May 13 '09 #9
angelicdevil
116 100+
maybe u r not able to understand wat i m sayying... let me repeat.... i have tried all echo $thumb , echo $ source eveything.... it just returns back the link of the php file in this case i.e

http://localhost/test1.php
May 13 '09 #10
Markus
6,050 Expert 4TB
You shouldn't need to echo anything, just make a call the the imagejpeg(), imagepng(), etc., functions.
May 13 '09 #11
angelicdevil
116 100+
tried that still not working still its showing the http://localhost/test1.php
May 13 '09 #12
hoopy
88
Make sure you are sending the header to display the jpg i.e:

Expand|Select|Wrap|Line Numbers
  1. header('Content-type: image/jpeg');
  2. imagejpeg($thumb);
May 13 '09 #13
angelicdevil
116 100+
already did that still not working. displaying the the location to php file
May 14 '09 #14
angelicdevil
116 100+
is the rest of the code correct?
May 14 '09 #15
code green
1,726 Expert 1GB
is the rest of the code correct?
Have you made the changes I have suggested twice?
Can we see the code as it stands now.

maybe u r not able to understand wat i m sayying... let me repeat.... i have tried all echo $thumb , echo $ source eveything
Which is why I said
I doubt echo is the right thing to do here
In fact you are echoing booleans and pointers.

Now have you tried what Markus said?
Can we see the latest code?
May 14 '09 #16
angelicdevil
116 100+
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // generating a array with image paths
  4.  
  5. $query_images = "SELECT image_path 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.           $filename = $record_images['image_path'] ;
  11.  
  12.  
  13. // Content type
  14. header('Content-type: image/jpeg');
  15.  
  16. // generating a thumbnail
  17. $thumb_height = 100;
  18.  
  19. list($width, $height) = getimagesize($filename);
  20. $ratio = ($height/$width);
  21. $newheight = $thumb_height;
  22. $newwidth = ($thumb_height/$ratio);
  23.  
  24. $thumb = imagecreatetruecolor($newwidth, $newheight);
  25. $source = imagecreatefromjpeg($filename);
  26.  
  27. $thumbnail= imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  28.  
  29. // displaying thumbnail
  30.  
  31. header('Content-type: image/jpeg');
  32. imagejpeg($thumb);
  33. imagedestroy($thumb); 
  34.  
  35.  
  36. }
  37.  
  38. ?>
  39.  
May 14 '09 #17
Markus
6,050 Expert 4TB
You are trying to output multiple images in one page. You cannot do that. The image script must output a single image. Determine which image you need to output based on data passed via the url (in the image's src attribute) and output that single image.

Clarification: using a while loop to output multiple images to one page will not work.
May 14 '09 #18
angelicdevil
116 100+
only 1 image is uploaded as of now
May 14 '09 #19
hoopy
88
The following code works for me if I have a database record with the image_path value set as "image.jpg" and that image within the same directory the code is executed in. Therefore it may be a problem with your actual path name. Can you show us what that field contains in your database.

Expand|Select|Wrap|Line Numbers
  1. // generating a array with image paths
  2. $query_images = "SELECT image_path FROM userfolders" ;
  3. $result_images = mysql_query($query_images);
  4. while ($record_images = mysql_fetch_assoc($result_images)) 
  5. {
  6.   $filename = $record_images['image_path'] ;
  7.  
  8.   // generating a thumbnail
  9.   $thumb_height = 100;
  10.  
  11.   list($width, $height) = getimagesize($filename);
  12.   $ratio = ($height/$width);
  13.   $newheight = $thumb_height;
  14.   $newwidth = ($thumb_height/$ratio);
  15.  
  16.   $thumb = imagecreatetruecolor($newwidth, $newheight);
  17.   $source = imagecreatefromjpeg($filename);
  18.  
  19.   $thumbnail= imagecopyresized($thumb, $source, 0, 0, 0, 0, 
  20.     $newwidth, $newheight, $width, $height);
  21.  
  22.   // displaying thumbnail
  23.   header('Content-type: image/jpeg');
  24.   imagejpeg($thumb);
  25.   imagedestroy($thumb); 
  26. }
May 14 '09 #20
angelicdevil
116 100+
the path in image_path is

d:/wamp/www/images/apple.jpg
May 15 '09 #21
angelicdevil
116 100+
it seems the getimagesize is not locating the directory. since i tried echoing the $width & $height and it shows that No such file or directory in D:\wamp\www\images\test3.php

the field image_path has the value as apples.jpg now where m i going wrong

how do i fix it?
May 16 '09 #22
code green
1,726 Expert 1GB
Pass the full path to the function
May 18 '09 #23
angelicdevil
116 100+
i did tht already still same error
May 18 '09 #24
angelicdevil
116 100+
how come the same code is working for hoopy n not for me
May 19 '09 #25
code green
1,726 Expert 1GB
No such file or directory in
means the path is wrong or the file does not exist
May 20 '09 #26
angelicdevil
116 100+
the file and directory both exist but still getting tht msg
May 20 '09 #27
hoopy
88
Just a guess but maybe windows path names? Looks like you are using Linux style forward slashes. I dont really use windows that much but should your path not be:

d:\\wamp\\www\\images\\apple.jpg

Cheers.
May 20 '09 #28
angelicdevil
116 100+
but didnt u use the image path as apple.jpg ?
May 20 '09 #29
hoopy
88
I dont know where you are getting apple.jpg from, I used image.jpg which was in the same directory as the code I was executing. Just for testing purposes put it in the same directory and just have the path as the filename.
May 20 '09 #30
Atli
5,058 Expert 4TB
Hi.
@angelicdevil
@angelicdevil
Which is it?

These functions require a valid path to work properly.
Simply feeding "apple.jpg" into such a function will not work, unless that image happens to be in the same directory as the script.

Make sure these paths are correct, and that the files they point to exist and are readable. (The file_exists function may help there.)


I also though I'd point out why you are having such problems diagnosing this problem...
Which is: you ignoring the return values of pretty much every function you use.

Functions, like imagecopyresized, return boolean values for a reason; to give you the ability to check if the function actually completed successfully.

You should always make sure this is the case, or at least be aware of the consequences when you choose to ignore these return values. (Pages failing to execute without any warning or error messages, for example.)

Consider this; a rewrite of your code, only adding basic error checking:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $sql = "SELECT image_path FROM whatever LIMIT 1";
  3. $result = mysql_query($sql) or die("Query failed.");
  4.  
  5. // Make sure there was actually a row retrieved.
  6. if(mysql_num_rows($result) == 0) {
  7.   die("No data available in the database");
  8. }
  9. $row = mysql_fetch_assoc($result);
  10. $file_path = $row['file_path'];
  11.  
  12. // Make sure the file does exist.
  13. if(!file_exists($file_path)) {
  14.   die("The file '{$file_path}' does not exist.");
  15. }
  16.  
  17. // Get and calculate the thumb dimensions
  18. list($width, $height) = getimagesize($file_path);
  19. $ratio = $height / $width;
  20. $thumbHeight = 100;
  21. $thumbWidth = $thumbHeight / $ratio;
  22.  
  23. // Create the source image
  24. $source = imagecreatefromjpg($file_path);
  25. if(!$source) {
  26.   die("Failed to get the source image at '{$file_path}'");
  27. }
  28.  
  29. // Create the thumbnail
  30. $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  31. if(!$thumb) {
  32.   die("Failed to create the thumbnail");
  33. }
  34.  
  35. // Copy the source to the thumbnail
  36. $copySuccess = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  37. if(!$copySuccess) {
  38.   die("Failed to copy the source to the thumbnail.");
  39. }
  40.  
  41. // Display the thumbnail
  42. header("Content-Type: text/jpeg");
  43. imagejpeg($thumb);
  44.  
  45. // Free resources
  46. imagedestroy($source);
  47. imagedestroy($thumb);
  48. ?>
  49.  
Written like this, it will tell you exactly why it failed, which will make it much easier to fix.
May 21 '09 #31
code green
1,726 Expert 1GB
And please angelicdevil do not just copy and paste Atli's code
and post back
Tried, does not work
or something similar.
You are obviously a very novice programmer but to continually
ignore peoples advice and simply demand the answer is not the way to learn.

Re-write your code yourself and put some debugging in there..
Do this by echoing out the values in variables and returns from functions at every step.
Atli's 'die' calls is not my favourite but it will show exactly where the script is failing and why.
May 21 '09 #32
angelicdevil
116 100+
code green please read my posting very carefully before making any statements.

i have mentioned very clearly that the code which i wrote though is working for hoopy is not working for me in spite of the file existing and the path being correct.

i agree i m a novice but making allegations as i m not rewriting the code and simply copy pasting is purely ur assumptions. each time i have posted a query in this forum i have troubleshooted it myself and taken into consideration the various suggestions ....sometimes figured out that errors myself sometimes with other's help....but never have i simply done any copy paste. if that is what i intended to do i would have probably visited some warez site and got the codes. whenever someone has corrected my code i tried to understand wat they did n y they did it before incorporating it in my code.

another thing , in post back reply i have not posted back the whole code and then my reply .....plz read the posts carefully....each time the code was different.

kindly refrain from making such allegations....as i m a novice programmer alright but someone who is seriously trying to understand and figure the issue on my own.
May 21 '09 #33
angelicdevil
116 100+
to atli

i tested the code with both the path name being "apples.jpg" and "d:/wamp/www/images/apple.jpg" in both the cases its giving me the same error...though the file is very much present in the folder ...i get the error file does not exist in the directory when i had debugged it before you posted your code....with your code i get same issue. i cant understand y its not reading the file.
May 21 '09 #34
Markus
6,050 Expert 4TB
Have you tried turning on PHP debugging messages?

If PHP is saying the file doesn't exist, then there must be a file path error.
May 21 '09 #35
angelicdevil
116 100+
yes markus i have that enabled...but wat i cant understand is y m i getting file path error when the path is correct meaning it does exists. i think i m missing something very small but crucial here.
May 21 '09 #36
code green
1,726 Expert 1GB
Sorry for upsetting you angelicdevil by making assumptions, but as I could not see any error trapping, debugging or checking of any kind in your code I was getting frustrated at the rather unhelpful replies.

We have offered advice on how to program as well as solve your problem
to improve your skills, not to insult.

Just a thought, are both the script you are running and folder you are trying to read on the same server, or same directory for that matter?

The error you are getting can also mean that php has no knowledge of the folder because it is not part of its known mapping.
May 21 '09 #37
angelicdevil
116 100+
its ok code green...no issues...i hope i figure out the problem with this code soon...its highly frustrating now, i have been at this script for past 15 days.

i m testing the script on local server apache installed on my pc. the folder too is on my pc and both the php file and the images are in same folder on my pc.
May 21 '09 #38
Atli
5,058 Expert 4TB
Ok, seeing as this seems to be a issue with the path, try creating a new PHP file and just check if PHP can actually read the path.

Like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $path = "D:/path/to/file.ext";
  3.  
  4. if(file_exists($path)) {
  5.   if(is_readable($path)) {
  6.     echo "File exists and is readable.";
  7.   }
  8.   else {
  9.     echo "File exists but is not readable.";
  10.   }
  11. }
  12. else {
  13.   echo "Path appears not to exist.";
  14. }
  15. ?>
If this gives you "appears not to exist", there must be an underlying problem with your OS, as PHP doesn't even appear to have access to list the files in that directory.
Might be a permission problem. (Windows file permissions never made much sense to me.)

If it gives you "exists but is not readable" then it is definitely a permission issue. Make sure that whatever user is running Apache has proper permissions set on the file.
Or, the less secure way; give Everybody permission to read the file.

If it gives you "exists and is readable" then PHP can read the file and the problem is in the way your code is using it.
May 22 '09 #39
angelicdevil
116 100+
ok atli i used ur code and as wll as incorporated it into my code to see if its readin the path or not....below is the code followed by the error.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $path = "images/apple.jpg";
  3.  
  4. if(file_exists($path)) {
  5.  if(is_readable($path)) {
  6.    echo "File exists and is readable.";
  7.  }
  8.  else {
  9.    echo "File exists but is not readable.";
  10.  }
  11. }
  12. else {
  13.  echo "Path appears not to exist.";
  14. }
  15. ?>
  16.  
file exists and is readable
now the i changed the path the read the path from the database
the value of $file = apple.jpg
the value of $dirname = images

below is my code

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. // generating a array with image paths
  5.  
  6. $query_images = "SELECT image_path,image_name 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.           $file = $record_images['image_name'] ;
  12.           $dirname = $record_images['image_path'] ;
  13.  
  14.  
  15. // generating a thumbnail
  16. $thumb_height = 100;
  17. $filename = $dirname."/".$file;
  18. }
  19.  
  20. ?>
  21.  
  22. <?php
  23. $path = $filename;
  24.  
  25. if(file_exists($path)) {
  26.  if(is_readable($path)) {
  27.    echo "File exists and is readable.";
  28.  }
  29.  else {
  30.    echo "File exists but is not readable.";
  31.  }
  32. }
  33. else {
  34.  echo "Path appears not to exist.";
  35. }
  36. ?>
  37.  
Path appears not to exist.
as you can see when i type the path directly as in code 1 it shows path exists however if i get the php file to read the path from database it shows path doesnt exist.
May 22 '09 #40
Atli
5,058 Expert 4TB
Ok. Then there is obviously something wrong with the values from your database.
If they were exactly what you say they are, the path would be correct.

You need to find out exactly what the path is.
Try using the var_dump function on the $path variable after line #34:
Expand|Select|Wrap|Line Numbers
  1. echo "<pre>"; var_dump($path); echo "</pre>";
May 23 '09 #41
angelicdevil
116 100+
i put in that line at

and output is as follows :-


string(16) "images/apple.jpg"

Path appears not to exist.
May 23 '09 #42
Atli
5,058 Expert 4TB
That's extremely weird...

The path looks correct and the file is readable by PHP (as demonstrated by the other script).
Yet your script doesn't seem to be able to read it...

The two scripts are in the same folder, right?
And it's not being included into a script somewhere else either?
May 24 '09 #43
angelicdevil
116 100+
yes botht he scripts are in same folder
May 24 '09 #44
angelicdevil
116 100+
sorry its my mistake i put the file inside the images folder ...its reading the file now . however the thumbnail is still not getting displayed i debugged the code i m posting the both the codes below:-

code 1 - code only till reading the file and displaying the newwidth part ( this is apart file of code 2 - to check if dimensions are getting calculated for the image or not )
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. // generating a array with image paths
  5.  
  6. $query_images = "SELECT image_path,image_name 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.           $file = $record_images['image_name'] ;
  12.           $dirname = $record_images['image_path'] ;
  13.  
  14.  
  15. // generating a thumbnail
  16. $thumb_height = 100;
  17. $filename = $dirname."/".$file;
  18.  
  19. list($width, $height) = getimagesize($filename);
  20. $ratio = ($height/$width);
  21. $newheight = $thumb_height;
  22. $newwidth = ($thumb_height/$ratio);
  23. echo $newwidth; 
  24.  
  25. }
  26.  
  27. ?>
  28.  
value of image width for code 1 is getting displayed correctly.

code 2 - including code for thumbnail generation/ display
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. // generating a array with image paths
  5.  
  6. $query_images = "SELECT image_path,image_name 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.           $file = $record_images['image_name'] ;
  12.           $dirname = $record_images['image_path'] ;
  13.  
  14.  
  15. // generating a thumbnail
  16. $thumb_height = 100;
  17. $filename = $dirname."/".$file;
  18.  
  19. list($width, $height) = getimagesize($filename);
  20. $ratio = ($height/$width);
  21. $newheight = $thumb_height;
  22. $newwidth = ($thumb_height/$ratio);
  23.  
  24. $thumb = imagecreatetruecolor($newwidth, $newheight);
  25. $source = imagecreatefromjpeg($filename);
  26.  
  27. $thumbnail= imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  28.  
  29. // displaying thumbnail
  30.  
  31. header('Content-type: image/jpeg');
  32. imagejpeg($thumb);
  33. imagedestroy($thumb); 
  34.  
  35. }
  36.  
  37. ?>
  38.  
  39.  
  40.  
o/p = http://localhost/images.php
the path to php file gets displayed in the browser...so i guess the problem is with the image generation part of the code.
May 24 '09 #45
Atli
5,058 Expert 4TB
Are you sure you have GD installed?

I tried your code, using static values, on my test server and it worked fine.
So if the GD functions are failing, perhaps they aren't there?

Try adding this to the top of the script:
Expand|Select|Wrap|Line Numbers
  1. if(!function_exists("imagejpeg")) {
  2.     die("GD extension is not installed on this server.");
  3. }
May 24 '09 #46
angelicdevil
116 100+
how do i check if GD is installed or not...if not then how do i install it?
May 24 '09 #47
Atli
5,058 Expert 4TB
Try the adding the check I posted in my last post.
If the GD extension isn't installed, it will tell you so when you try to create your thumb.

You can also check the output of the phpinfo function to see if GD is listed.

If it is not, you need to install it.
Check out GD Installation , and Installation of extensions on Windows in the manual.
May 26 '09 #48
angelicdevil
116 100+
i added the following code to check if its working or not and it gives success meaning gd is installed

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php if(!function_exists("imagejpeg")) {
  3.    echo "GD extension is not installed on this server.";
  4.    }
  5.  
  6.    else { echo "success";}
  7.    ?> 
  8.  
  9.  
May 28 '09 #49
angelicdevil
116 100+
as for phpinfo function

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Show all information, defaults to INFO_ALL
  4. phpinfo();
  5.  
  6. // Show just the module information.
  7. // phpinfo(8) yields identical results.
  8. phpinfo(INFO_MODULES);
  9.  
  10. ?>
  11.  
gd
GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.1.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled
May 28 '09 #50

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
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.