Why won't php work with this image?

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
Tygur
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2004 5:55 pm

Why won't php work with this image?

Post by Tygur »

When I try to use imagecreatefromjpeg and imagecreatefromstring to work with this one image, it claims that it isn't a valid jpeg image. Why is this, and do I have any options?

Thanks in advance for any help. I'm just trying to automatically generate a thumbnail. The image is a picture taken with a cell phone camera. If it doesn't work with this one, it seems safe to assume it won't work with others, which would be a problem.

It can be found here:
http://www.snsware.net/Image003.jpg
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post the code you are trying to use with this image.
Breckenridge
Forum Commoner
Posts: 62
Joined: Thu Sep 09, 2004 11:10 pm
Location: Breckenridge, Colorado

Post by Breckenridge »

I had this problem with few images and this is what I did. I loaded the image into an image editing program, then did a save as jpeg, ran my code again and it worked. Try it it should work.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

If you think that taking something like a .gif or a .png and changing the extension to .jpg will work, think again. (this seems like what's happenning). Think of it as taking a book written in French and writing "ENGLISH" on the cover. A person who can read both French and English won't care (same with a program that can read both gifs and jpegs), but a person who doesn't know the first thing about French will be dazed and confused (much like imagecreatefromjpeg() doesn't know anything about anything other than jpegs). I suggest doing what Breckenridge said.

Good luck!
Tygur
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2004 5:55 pm

Post by Tygur »

Ok, I made a quick php script that expects the image file to be in the same directory with the script itself:

Code: Select all

<?php
echo "<h1>First let's try imagecreatefromjpeg</h1>";
$image = imagecreatefromjpeg('Image003.jpg');
if ($image===false) {
  echo "<p>Something went wrong!";
} else {
  echo "<p>Ok, now everything's fine.";
  imagedestroy($image);
}
echo "<h1>Now let's try imagecreatefromstring</h1>";
$image_contents = file_get_contents('Image003.jpg');
if ($image_contents===false) {
  echo "<p>The image couldn't even be read!";
} else {
  echo "<p>The image was read in.  Now let's see if it works.";
  $image = imagecreatefromstring($image_contents);
  if ($image===false) {
    echo "<p>Something went wrong!";
  } else {
    echo "<p>Ok, now everything's fine.";
    imagedestroy($image);
  }
}
?>

Here is the output I got:
First let's try imagecreatefromjpeg

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'Image003.jpg' is not a valid JPEG file in f:\www\root\image.php on line 3

Something went wrong!
Now let's try imagecreatefromstring

The image was read in. Now let's see if it works.
Warning: imagecreatefromstring() [function.imagecreatefromstring]: Passed data is not in 'JPEG' format in f:\www\root\image.php on line 16

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Couldn't create GD Image Stream out of Data in f:\www\root\image.php on line 16

Something went wrong!
Tygur
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2004 5:55 pm

Post by Tygur »

evilmonkey wrote:If you think that taking something like a .gif or a .png and changing the extension to .jpg will work, think again. (this seems like what's happenning).
No, I did not take an image of some other format and change the extension. Check out the script I just posted. imagecreatefromstring recognizes that it looks at first like a jpeg, but it still rejects it for some reason.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's possible your version of GD doesn't recognize the type/version of JPEG it is... after all, JPEG is an umbrella format.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

My version of GD (bundled (2.0.12 compatible)) is happy to open it. The version on my hosting company's server (2.0.33) isn't though. The only odd thing I can see about it is that theres no resolution defined in the jpeg, and its jpg/jfif compressed rather than jpg/jfif,progressive.. bit of a puzzle really.
Post Reply