Jump to content

imagecreatefromjpeg failure


Recommended Posts

After working with the sample imagecreatefromjpeg provided in the PHP manual, I successfully got a result (after clearing my cache) from

Quote

imagecreatefromjpeg($im, $file);

I've gotten a good education after navigating this function over the past week, and loaded it with ECHO messages to give me insight.

Everything was going fine.

And then, this ONE test image came along.

Apparently, the image (which is as good aj peg as I can find) FAILS the if(!im) test.

When I used echo $im;  

i discovered that when images pass through the function, they receive a "Resource" name.

Images that FAIL are NOT named.

This image gets a Resource name, yet FAILS. Is there a problem with my logic? A problem with the image? What would cause this? How can I verify?

Link to post
Share on other sites

You can't just echo $im to view the image, you need to send a type header then output it with imagejpeg()

$im = imagecreatefromjpeg('my_image.jpg');

// output the image
header("Content-type: image/jpeg");
imagejpeg($im);

 

Link to post
Share on other sites
Posted (edited)
Quote


    $im = @imagecreatefromjpeg($file);

if($im == "")  {

echo "1";} else { echo "00"; }
   }

    //See if it failed
    if(!$im)  {
     echo "running";

 //Unaltered manipulation code from https://www.php.net/manual/en/function.imagecreatefromjpeg.php

    }

    ////return $im;

//displays RESOURCE ID
echo "<br> im is ". $im;
echo "<br>";

header('Content-Type: image/jpeg');

//imagejpeg($img);

imagejpeg($im, $file);


echo "<img src='".$file."'> ;

 

My messages that seem in conflict with the result are

Quote

1runningi

im is Resource id #19

Is there a way to validate WHY this result was given by the function?

Edited by phppup
Link to post
Share on other sites

1. Don't use @.
2. $im will never be a string. It does not make sense to compare it to a string. Don't do that either.

Read the documentation to find out exactly what imagecreatefromjpeg does.

Link to post
Share on other sites

For 1 - okay. How can I force an error message just to see how it appears?

For 2 - ok, but why does 

echo $im; 

Give me the string beginning with Resource? (I honestly wasn't expecting that result).

How can I go deeper to determine WHY a (perfectly good) image failed?

Link to post
Share on other sites
49 minutes ago, phppup said:

For 1 - okay. How can I force an error message just to see how it appears?

Make $file be a non-JPEG image.

 

49 minutes ago, phppup said:

For 2 - ok, but why does 

echo $im; 

Give me the string beginning with Resource? (I honestly wasn't expecting that result).

Because $im is a resource and if you want to echo it out then PHP needs to do something.

 

49 minutes ago, phppup said:

How can I go deeper to determine WHY a (perfectly good) image failed?

Remove the @ and look for error messages.

Link to post
Share on other sites

I've cleaned up a few things, but this error message remains:

Warning:....failed to open stream: No such file or directory in...

I am simultaneously uploading three jpeg files from the same folder during my development / testing and this is the ONLY image that is being rejected.

Reason? Explanation? Solution?

Thanks.

Link to post
Share on other sites
8 hours ago, phppup said:

Reason? Explanation? Solution?

The reason would be that there is no such file or directory. The explanation would be somewhere in your code. The solution is :psychic:

Link to post
Share on other sites

How is that possible if the other two test files originate from the same folder? Clearly it (and the path) exists.

How can I drill down for a deeper explanation?

 

Also, I've realized that some images do not refresh unless browser history / cache is cleared.

Research send to point to using no-cache headers or a flush directive. What is the best/suggested method?

Link to post
Share on other sites
On 3/29/2021 at 7:28 AM, phppup said:

I am simultaneously uploading three jpeg files

Do you mean uploading via PHP?  Have to properly verified that the upload was successful?  Maybe the upload is failing for that one file (too large?) and so you end up with $file referencing something that doesn't exist.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.