I am trying to generate and return an image to the template using the Silverstripe 3.0 Framework and am getting some issues.
I return this to the browser in a variable like so:
public function make_image(){
$string = 'string';
$im = imagecreate(300,300);
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
}
The browser renders this as:
�PNG IHDR,,C�6PLTE��<��*�"IDATh���1 �Om ?�x-��{�IEND�B`�
The headers that are being returned are text/html and there is only one request that makes me think there is something strange going on there. Can anyone help me out with this.
Possibly a different way of achieving this is Silverstripe using the Image() class?
header("Content-type: image/png");- user1467267<img src="my_image.png" type="image/png" />There's not much info on this, but can't hurt to try to force the image to show correct on output. Are you writing the file to the server first or instantly putting it on the page? If I do this in my Terminal;file -b my_png_file.pngit outputs;PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced. As you are using a CMS, it might be that your headers are already sent. Try enablingerror_reporting('E_ALL');and see if that's the case. Very common issue. You can only use image headers when you are fetching a PHP as an image tho. - user1467267header("Content-type: image/png");you can only give back the image, and not extra content. Something like this would be the result;<img src="my_image.php" />. - user1467267