Tomas
Tomas

Reputation: 2726

PHP image output issue

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).

Upvotes: 0

Views: 520

Answers (3)

Amin Fazli
Amin Fazli

Reputation: 152

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.

Upvotes: 1

Amin Fazli
Amin Fazli

Reputation: 152

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

Upvotes: 1

Angelin Nadar
Angelin Nadar

Reputation: 9300

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()

Upvotes: 2

Related Questions