views:

180

answers:

4

I have a code that dinamically resizes an image and sends it to the browser.

But it doesn't work properly... only if I ask it to output the image to a file instead of the browser.

I don't think the problem is my code, because this problem only occurs on the real server; in my computer its working perfectly.

Code:

$img = $_GET['img'];

ini_set('allow_url_fopen', 'on');

$info = getimagesize($img);
header('Content-type: '.image_type_to_mime_type($info[2]));
Fotos::redimensiona($img, null, Fotos::MINIGAL_WIDTH, Fotos::MINIGAL_HEIGHT, false);

Fotos::redimensiona():

//[...] a whole bunch of code calculating dimensions, they just works
// $funcImage is like 'imagejpeg'/'imagepng'/'imagegif', depends on file
if ($arquivo) {
 $funcImage($thumb, "$final.$ext");
 return "$final.$ext";
}
else {
 $funcImage($thumb);
}

Remember: it works on local development, but doesn't on remote web server.

[EDIT]
if I comment the header line, the binary code is printed; and this code changes when i change the original image, as expected.
But with the header Firefox shows me the URL of the page (like http://www.sabianoar.com.br/novosabia/inc/phpImg.php?img=awful_escaped_long_path.jpeg), and if I do CTRL+I it tells me it is an JPEG of 0x0 size, and like 10kb.
Opera shows me an empty image, as it would do if I had placed an <img> with the wrong src, i.e.

[EDIT2] EyeOfGnome says "Not a JPEG file: starts with 0xef 0xbb" when I try to save and then open the output (it saves to a .jpeg file normally).

A: 

Check the permissions of the image/directory where the image is located.

Randell
The folder is 777, and the JPEG, 666. Both are readable. And I forgot to say [i'll edit the question], but if I comment the `header` line, the binary code is printed; and this code changes when i change the original image.
Igoru
Have you tried making the JPEG 777 as well? I don't know if it will work. I've experience the quirk some time ago.
Randell
Also, check the HTML source from the browser.
Randell
Makin the image 777 does the same thing. I think that there's no point on this because different images generate differente binary code. If the image wasn't being read, it would say 'hey, I don't have permissions to read this'. ty anyway =]<br />And I can't read the source because it has an image content-type.But I tried to return the image to a file, and I saw that the HTML is simply empty. So, when it outputs the image code, there's only the code.
Igoru
A: 

This probably isn't the cause, but why are you echoing the result of Fotos::redimensional()? The functions like imagejpeg() etc. all return a bool (not the image data) after outputting the image, so you will end up printing a '1' to the end of the image. I just tested this though and it doesn't seem to break jpeg images.

Tom Haigh
yes... this doesn't break 'cos it works on my pc. anyway, i stripped out the echo. =P but doesn't solve my problem =(
Igoru
+1  A: 

Your code outputs BOM at the beginning. Open your code from remote web server and check (using hex editor, for example xxd) if it contains BOM before <?php. If BOM isn't in a file you're looking at, it might be in includes.

Some editors add BOM and apache outputs it. That's probably what breaks your images.

niteria
thank you SO MUCH \o/
Igoru
A: 

Have you tried comparing your local machine to the server? Compare the output of phpinfo(); Are they different platforms?

therealsix