Home » Php » image gallery- image not displaying – PHP

image gallery- image not displaying – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

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


<?
// generating a array with image paths
$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);
while ($record_images = mysql_fetch_assoc($result_images)) {
$image_list[] = $record_images['image_path'] ;
?>
<?
// generating a thumbnail
$thumb_height = 100;
for ($i=0,$i<count($image_list),$i+)
{
$filename = $image_list[$i];
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
$thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// displaying thumbnail
$thumbnail = imagejpeg($thumb_image);
echo $thumbnail;
?>

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

A(Answer):

imagecopyresized() returns a boolean, not an image resource
which imagejpeg() expects

A(Answer):

so wat shud it be? plz tell

A(Answer):

i even tried tht line with imagecopyresampled() still same thing

A(Answer):

Never used this so I would have to study manual to get it right.
Have a look here http://us2.php.net/imagejpeg

A(Answer):

i did …the examples show similar thing …cant understand y the images are not getting displayed

A(Answer):

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

//This returns a resource
$thumb = imagecreatetruecolor($newwidth, $newheight);
//This also returns a resource
$source = imagecreatefromjpeg($filename);

A(Answer):

NOTE returns a boolean

A(Answer):

##
$thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

A(Answer):

A(Answer):

#also returns a boolean and expects an image resource.
$thumbnail = imagejpeg($thumb_image);
//You are supplying a boolean " $thumb_image "

A(Answer):

#THEN trying to echo a boolean $thumbnail

A(Answer):

A(Answer):

#
echo $thumbnail;

A(Answer):

ok so what should it be? plz tell as all examples i saw seem to use this way

A(Answer):

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.

//This returns a resource
$thumb = imagecreatetruecolor($newwidth, $newheight);
//This also returns a resource ,Which one are you using????????
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
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,

A(Answer):

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

A(Answer):

You shouldn’t need to echo anything, just make a call the the imagejpeg(), imagepng(), etc., functions.

A(Answer):

tried that still not working still its showing the http://localhost/test1.php

A(Answer):

Make sure you are sending the header to display the jpg i.e:

header('Content-type: image/jpeg');
imagejpeg($thumb);

A(Answer):

already did that still not working. displaying the the location to php file

A(Answer):

is the rest of the code correct?

A(Answer):

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?

A(Answer):


<?php
// generating a array with image paths
$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);
while ($record_images = mysql_fetch_assoc($result_images)) {
$filename = $record_images['image_path'] ;
// Content type
header('Content-type: image/jpeg');
// generating a thumbnail
$thumb_height = 100;
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
$thumbnail= imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// displaying thumbnail
header('Content-type: image/jpeg');
imagejpeg($thumb);
imagedestroy($thumb);
}
?>

A(Answer):

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.

A(Answer):

only 1 image is uploaded as of now

A(Answer):

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.

// generating a array with image paths
$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
while ($record_images = mysql_fetch_assoc($result_images))
{
$filename = $record_images['image_path'] ;
// generating a thumbnail
$thumb_height = 100;
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
$thumbnail= imagecopyresized($thumb, $source, 0, 0, 0, 0,
$newwidth, $newheight, $width, $height);
// displaying thumbnail
header('Content-type: image/jpeg');
imagejpeg($thumb);
imagedestroy($thumb);
}

A(Answer):

the path in image_path is

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

A(Answer):

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?

A(Answer):

Pass the full path to the function

A(Answer):

i did tht already still same error

A(Answer):

how come the same code is working for hoopy n not for me

A(Answer):

No such file or directory in

means the path is wrong or the file does not exist

A(Answer):

the file and directory both exist but still getting tht msg

A(Answer):

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.

A(Answer):

but didnt u use the image path as apple.jpg ?

A(Answer):

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.

A(Answer):

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:


<?php
$sql = "SELECT image_path FROM whatever LIMIT 1";
$result = mysql_query($sql) or die("Query failed.");
// Make sure there was actually a row retrieved.
if(mysql_num_rows($result) == 0) {
die("No data available in the database");
}
$row = mysql_fetch_assoc($result);
$file_path = $row['file_path'];
// Make sure the file does exist.
if(!file_exists($file_path)) {
die("The file '{$file_path}' does not exist.");
}
// Get and calculate the thumb dimensions
list($width, $height) = getimagesize($file_path);
$ratio = $height / $width;
$thumbHeight = 100;
$thumbWidth = $thumbHeight / $ratio;
// Create the source image
$source = imagecreatefromjpg($file_path);
if(!$source) {
die("Failed to get the source image at '{$file_path}'");
}
// Create the thumbnail
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
if(!$thumb) {
die("Failed to create the thumbnail");
}
// Copy the source to the thumbnail
$copySuccess = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if(!$copySuccess) {
die("Failed to copy the source to the thumbnail.");
}
// Display the thumbnail
header("Content-Type: text/jpeg");
imagejpeg($thumb);
// Free resources
imagedestroy($source);
imagedestroy($thumb);
?>

Written like this, it will tell you exactly why it failed, which will make it much easier to fix.

A(Answer):

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.

A(Answer):

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.

A(Answer):

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.

A(Answer):

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.

A(Answer):

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.

A(Answer):

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.

A(Answer):

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.

A(Answer):

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:

<?php
$path = "D:/path/to/file.ext";
if(file_exists($path)) {
if(is_readable($path)) {
echo "File exists and is readable.";
}
else {
echo "File exists but is not readable.";
}
}
else {
echo "Path appears not to exist.";
}
?>

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.

A(Answer):

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.


<?php
$path = "images/apple.jpg";
if(file_exists($path)) {
if(is_readable($path)) {
echo "File exists and is readable.";
}
else {
echo "File exists but is not readable.";
}
}
else {
echo "Path appears not to exist.";
}
?>

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


<?php
// generating a array with image paths
$query_images = "SELECT image_path,image_name FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);
while ($record_images = mysql_fetch_assoc($result_images)) {
$file = $record_images['image_name'] ;
$dirname = $record_images['image_path'] ;
// generating a thumbnail
$thumb_height = 100;
$filename = $dirname."/".$file;
}
?>
<?php
$path = $filename;
if(file_exists($path)) {
if(is_readable($path)) {
echo "File exists and is readable.";
}
else {
echo "File exists but is not readable.";
}
}
else {
echo "Path appears not to exist.";
}
?>

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.

A(Answer):

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:

echo "<pre>"; var_dump($path); echo "</pre>";

A(Answer):

i put in that line at

and output is as follows :-

string(16) "images/apple.jpg"

Path appears not to exist.

A(Answer):

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?

A(Answer):

yes botht he scripts are in same folder

A(Answer):

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 )


<?php
// generating a array with image paths
$query_images = "SELECT image_path,image_name FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);
while ($record_images = mysql_fetch_assoc($result_images)) {
$file = $record_images['image_name'] ;
$dirname = $record_images['image_path'] ;
// generating a thumbnail
$thumb_height = 100;
$filename = $dirname."/".$file;
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);
echo $newwidth;
}
?>

value of image width for code 1 is getting displayed correctly.

code 2 – including code for thumbnail generation/ display


<?php
// generating a array with image paths
$query_images = "SELECT image_path,image_name FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);
while ($record_images = mysql_fetch_assoc($result_images)) {
$file = $record_images['image_name'] ;
$dirname = $record_images['image_path'] ;
// generating a thumbnail
$thumb_height = 100;
$filename = $dirname."/".$file;
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
$thumbnail= imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// displaying thumbnail
header('Content-type: image/jpeg');
imagejpeg($thumb);
imagedestroy($thumb);
}
?>

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.

A(Answer):

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:

if(!function_exists("imagejpeg")) {
die("GD extension is not installed on this server.");
}

A(Answer):

how do i check if GD is installed or not…if not then how do i install it?

A(Answer):

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.

A(Answer):

i added the following code to check if its working or not and it gives success meaning gd is installed


<?php if(!function_exists("imagejpeg")) {
echo "GD extension is not installed on this server.";
}
else { echo "success";}
?>

A(Answer):

as for phpinfo function


<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>

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