Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP header ('Content-Type: image/png')

Tags:

php

I have a problem with header in php.

I havet this simple code.

<?php
header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
      or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>

I think it should works fine, but it didn't. There is no error, and in top-left corner of my browser a little crashed picture icon shows up.

I think header is the problem.

other useful infos: PHP Version 5.3.10-1ubuntu3.9 GD Support enabled GD Version 2.0 FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.4.8

UPDATE In IE even if i have header, i got bunch of data...

like image 549
user3260816 Avatar asked Feb 21 '14 11:02

user3260816


People also ask

What is header content-type PNG?

The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the file indicating the format of the file. For example, for image file its media type will be like image/png or image/jpg, etc. In response, it tells about the type of returned content, to the client.

How to add png image in PHP?

Example 1 − Showing the loaded PNG image in the browser php // Load an image from local drive/file $img = imagecreatefrompng('C:\xampp\htdocs\Images\img29. png'); // It will show the loaded PNG image in the browser header('Content-type: image/png'); imagejpeg($img); imagedestroy($img); ?>

Which code will set content-type header to image PNG?

php header ('Content-Type: image/png'); $im = @imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream'); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); imagepng($im); imagedestroy($im); ?>

What is header content-type in PHP?

The header in PHP is a PHP built-in function for sending a raw HTTP header. The HTTP functions are those that manipulate information sent by the webserver to the client or browser before it sends any further output. The header() function in PHP sends a raw HTTP header to a client or browser.


1 Answers

For draw image in browser its have to send two headers before the main image data. First it is type of the data

header ('Content-Type: image/png'); 
//or
header ('Content-Type: image/jpeg');

and second it is data length. Without that header browser will be not know where the data ended.

header("Content-length: $size");   //$size image file size in bytes

need check php file have not any spaces and new lines before <?php and no php output before headers and image data output.

like image 101
dharmabook.ru Avatar answered Sep 20 '22 01:09

dharmabook.ru