0

I have a problem with generating and exporting image with PHP.

So I have this:

header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename=WHATEVER.jpg');
$dragon = self::static_dragon($numbers,$avatars,$new_name,$filepath);

Inside of static_dragon function I load data from database, work with them, etc and then:

imagejpeg($canvas,'',60);
imagedestroy($canvas); 
return true;

What I want to achieve is that if I write <img src="http://www.example.com/dragon.php&width=100&height=100" /> it will give me the image. Everything works fine, database connection, even saving the final jpg file on server, only problem is that browser doesn't show me the image. I think maybe some problem in headers? Incidentally, I cannot execute the headers after the function as script tells me that headers were already sent (even though I don't see any command which would do so).

3
  • do you echo anything out before you set the headers? even if it's only whitespace before your <?php or anything? Commented Feb 27, 2012 at 5:43
  • No, nothing, the "header already sent" error i telling me that headers were sent on line: $handler = imagecreatefromjpeg($picture); which makes no sense to me Commented Feb 27, 2012 at 6:05
  • Have you checked that there are no error or warning messages being generated? Commented Feb 27, 2012 at 7:11

3 Answers 3

2

Are u doing the same:

header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename=WHATEVER.jpg');

$im = imagecreatefromjpeg('download.jpg') or  or die('Cannot Initialize new GD image stream'); // this will let u know whether the creation of image resource was a failure

imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00); //just a add on 

imagejpeg($im);
imagedestroy($canvas);
return true;

The above code is working fine in a browser. Any errors?

@Tom as you have mentioned u get error: "header already sent" for imagecreatefromjpeg. Solve the error first as imagecreatefromjpeg() provides the resource for the image. If the resource is failing then image wont be visible.

Give a try by using, @imagecreatefromjpeg()

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

3 Comments

Ah now im thinking about it, yes it did give me error with image resource and that might be the "already sent error". What is difference from imagecreatefromjpeg() and @imagecreatefromjpeg() ? I will try without the error, what is the function imagestring() ? Thank you!
Ok so now i can have the headers where I want, but image is still not displaying. In Chrome its just broken image, in Firefox I see message: The image "domain.com/?request=request&width=300&height=300" cannot be displayed because it contains errors. Any more advice? Thank you
Having the @ operaton in front of a function will prevent printing error messages to the user’s browser.I hv updated the code with handler.
1

Haven't you forgotten to add

 echo $dragon

object in the content of the php file? :) add this line to the end of your php file:

echo $dragon

see

1 Comment

I thought the same, but that didnt help as well
1

So instead of echo-ing $canvas use this:

$this->getResponse()->setContent($canvas)

in the head part of your php file.

(I assumed that $canvas is the object loaded from DB)

Please make sure that you have not printed any other thing in the php file.

Comments

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.