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
53 4227
imagecopyresized() returns a boolean, not an image resource
which imagejpeg() expects
so wat shud it be? plz tell
i even tried tht line with imagecopyresampled() still same thing
i did ...the examples show similar thing ...cant understand y the images are not getting displayed
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);
-
###NOTE returns a boolean#####
-
$thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
-
#######also returns a boolean and expects an image resource.
-
$thumbnail = imagejpeg($thumb_image);
-
//You are supplying a boolean " $thumb_image "
-
####THEN trying to echo a boolean $thumbnail #######
-
echo $thumbnail;
ok so what should it be? plz tell as all examples i saw seem to use this way
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,
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
You shouldn't need to echo anything, just make a call the the imagejpeg(), imagepng(), etc., functions.
Make sure you are sending the header to display the jpg i.e: - header('Content-type: image/jpeg');
-
imagejpeg($thumb);
already did that still not working. displaying the the location to php file
is the rest of the code correct?
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?
-
<?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);
-
-
-
}
-
-
?>
-
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.
only 1 image is uploaded as of now
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);
-
}
the path in image_path is
d:/wamp/www/images/apple.jpg
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?
Pass the full path to the function
i did tht already still same error
how come the same code is working for hoopy n not for me
No such file or directory in
means the path is wrong or the file does not exist
the file and directory both exist but still getting tht msg
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.
but didnt u use the image path as apple.jpg ?
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.
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: -
<?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.
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.
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.
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.
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.
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.
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.
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.
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: - <?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.
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.
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: - echo "<pre>"; var_dump($path); echo "</pre>";
i put in that line at
and output is as follows :-
string(16) "images/apple.jpg"
Path appears not to exist. 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?
yes botht he scripts are in same folder
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.
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: - if(!function_exists("imagejpeg")) {
-
die("GD extension is not installed on this server.");
-
}
how do i check if GD is installed or not...if not then how do i install it?
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.
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";}
-
?>
-
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: nick |
last post by:
After spending many hours trying to find a simple looking,
fast,dynamic php photo-gallery for my digital pictures, I decided to
code a my own.
Once installed, all you have to do is drop a new...
|
by: Daniel Kelly \(AKA Jack\) |
last post by:
Hi!
I'm searching for a Photo Gallery software package (like Coppermine and
Gallery) that works, from the ground up, like a database-driven app. In
other words, I want a gallery which entirely...
|
by: Simone Winkler |
last post by:
Hello,
I don't know if my question is out of topic...so I apologize if I am.
I use the coppermine picture gallery for my web (actually I want to use it
but the server doesn't seem to have GD...
|
by: Captain Ranger McCoy |
last post by:
Hello!
Suppose I have ten servers at ten ips:
x.x.x.1
x.x.x.2
x.x.x.3
x.x.x.4 and so on
Each server hosts 100+ photo galleries, all under a single domain name,
|
by: Fred |
last post by:
I've written a number of "image gallery" pages before, but I'm trying
to do something a little different.
All the images are rectangular (these are just pictures from my
camera), and the...
|
by: desjardins.daniel |
last post by:
Hi !
Excuse my english, i'm a french canadien... So here my message :
I have put on my site a photo gallery and at the right a nav menu. This
menu has a red dot visible want someone is passing...
|
by: Throw |
last post by:
G'day everyone
I'm looking for a simple photo gallery script in PHP (or Perl), but not
too simple. I have tried several photo gallery scripts in either
language and I have found that they are...
|
by: ste |
last post by:
Hi there,
Further to my recent posts where I've received excellent help from Rik and
Jerry, I've ended up with an image gallery on my website that displays
images in a table, 3 images per row. ...
|
by: omerbutt |
last post by:
hi all
i am using a slide picture gallery provided by
http://smoothgallery.jondesign.net/showcase/gallery/
the gallery is working fine but only if i try to make 2 slide shows on the same page it...
|
by: nomad |
last post by:
Hello Everyone.
I founded an Flash and xml photo gallery. It works but I took it to another step
What I want is to have six different galleries in one Flash file. I figure out how to do that but ...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |