Question

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.

Was it helpful?

Solution

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>

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top