1

With the following code, my browser is returning 'image not created or saved'. I assure you the location of the image exists. So the line within the if statement is returning false for some reason, and yes GD is installed. Any ideas?

if ($img = @imagecreatefromjpeg("/var/www/images/upload/1/1.jpg")) {

            die("image was created");

     } else {

         die ("image was not created or saved");

     }

OK, I did this:

<?php

error_reporting(E_ALL);

if (fopen('/var/www/images/upload/1/1.jpg')) {

    echo 'file was opened';

} else {

    echo 'file was not opened';

}


?>

It returns file was not opened every time, the apache group has all permissions for all of these folders. Is GD or PHP a different username?

After doing an is_readable() from a test script, it returned true. What else could the problem be?

So... when I run the script:

error_reporting(E_ALL);
imagecreatefromjpeg("/var/www/images/upload/1/1.jpg");
print_r(error_get_last());
echo ("hi");

I receive a white screen of death. If I comment out the imagecreatefromjpeg line, the screen displays 'hi'

Just tried this on a 500k jpg image to see if it's a memory issue, but still got the white screen.

When I run the imagecreatefromjpeg within an if statement and run the script through the terminal, the imagecreatefromjpeg is a success! =\ Still can't figure out why it wouldn't work otherwise. EDIT: Running this exact script through my browser also is a success.

2
  • How about your remove the @ operator so you can get any actual error messages from that line? It could be any number of things, from GD not being loaded to running out of memory when trying to create it. Commented May 19, 2010 at 22:48
  • If you remove the error suppression, what is the error message? Commented May 19, 2010 at 22:48

4 Answers 4

5

Clearly the value of $img is not evaluating to true.

Sorry, that was just too much to resist.. my point was, that there is no need for all that other crap in your post.

Image creation can fail if the process can't read the file. try fopen-ing the file for read. also, get rid of the error suppression in front of your function call (@) . Then set error_reporting to E_ALL and display_errors to on.

Update: if the fopen is failing, then the user running php does not have read permissions for the file. Try using fopen and fwrite to write a file to /tmp to see which user your php/apache process is running as. Then make sure that user has read access. Or you can just try chmoding the file to o+r to confirm that read access for everyone fixes our problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Check that the script has privilege to access /var/www/images/upload/1/1.jpg remembering that your web server (assuming it's not a CLI script) may be running as a different user than you expect

1 Comment

My apache user is in the www-data group, which has permissions to my entire /var/www/ directory and it's sub folders
0

You probably do not have the permission to access the file.

If your localhost maps to var/www there's no need to call the file like that. Just use the path relative to that of the PHP file.

3 Comments

I tried calling the absolute path for testing, since the relative path wasn't working either.
@ThinkingInBits: yeah, but probably you run into permission problems. 1) remove the @ and see what is the error message 2) check the permissions of the file 3) what happens if you access the file from the browser? can you see it?
Yes, I can see the image in the browser. Permissions are 755
0

Try using error_get_last() (PHP >= 5.2) or $php_errormsg to see what the img*() functions are returning.

As well, a better way to check a file's readableness is, oddly enough, is_readable(), rather than fopen.

2 Comments

If the file's readable, then possibly the .jpg is corrupted or not really a .jpg, causing imagecreatefromjpeg() to fail. Check the error_get_last() response
This script worked fine on my localhost. There is nothing wrong with the jpg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.