views:

48

answers:

3

Hiya,

I've created a function to overlay text onto images, but I'm scratching my head to find a way of outputting the image in a way in which it can be utilised within another script or outputted to the screen.

I can call this function from a php script, passing it the image and the text to use, but when the function returns the data - it takes over the page due to the header - all I get as output is the image.

I suspect this is an easy one and just shows a hole in my PHP knowledge - can someone set me straight here?

Thanks!

function makeimage($file, $text) { ob_start(); $x = getimagesize($file); $width = $x[0]; $height = $x[1]; $type = $x[2];

 //header('Content-type: $type');

 if ($type == 1) {
  $im = imagecreatefromgif($file);
 } elseif ($type==2) {
  $im = imagecreatefromjpeg($file);
 } elseif ($type==3) {
  $im = imagecreatefrompng($file);
 }

 // Create some colors
 $white = imagecolorallocate($im, 255, 255, 255);
 $grey = imagecolorallocate($im, 128, 128, 128);
 $black = imagecolorallocate($im, 0, 0, 0);

 // Replace path by your own font path
 $font = 'arial.ttf';

 // Add some shadow to the text
 imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

 // Add the text
 imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

ob_clean(); //to be sure there are no other strings in the output buffer
imagepng($im);
$string = ob_get_contents();
ob_end_clean();
return $string;

}

I want to create this image, and then have it outputted in such a way as I can display it on screen in amongst all the other output.

+2  A: 

If you don't want to output the image directly, just don't send the header in your function. Every browser will treat the response as image file.

Also, imagedestroy($im) after return will never get executed!

If you want to just create the image and save it to a file, check the imagepng() documentation. The second parameter accepts a filename:

The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

According to your edit:

You should use imagepng with the filename parameter to create the image, and then link to it from your script.

Small example:

<?php
// ...
imagepng($im, 'foo.png');
?>
<img src="foo.png" />

The other way around would be Davide Gualanos solution by using a wrapper script to passtrough image/png headers and direct output.

Karsten
+1  A: 

Hi,

you can use a temporary output buffer to catch the output of the function and then having a string filled with it

Example:

ob_start();

... your image function code ...

ob_clean(); //to be sure there are no other strings in the output buffer

imagepng($im);

$string = ob_get_contents();

ob_end_clean();

return $string;

$string got all the output from imagepng();

Ass3mbler
Hiya - I've used this, but it still returns base64 (or something) - a heap of code rather than an image, in other words. At least it's in the right place though!
hfidgen
have you tried to $string = base64_decode($string) to see if you've got the original image?
Ass3mbler
+1  A: 

You have to put the script that output the image in a page, and call that page in a html tag to display it.

Example:
image.php

<php
$image = $_GET['image'];
$text = $_GET['text'];
makeimage($image, $text);
?>

page.html:

<html>
<body>
<img src="image.php?image=foo.jpg&text=hello" />
</body>
</html>
Davide Gualano
Ahh perfect - that makes sense!
hfidgen