Setting Headers For Dynamic Images With Php

Setting headers for dynamic images with PHP
by Janeth Kent Date: 30-04-2013 php


PHP provides an amazing library called Graphics Draw ( GD ) to generate images. You can create and output the images directly to the browser or you can store it as a image file. For example Google Charts API generates QR Code images and outputs it directly to the browser. You can do the same with PHP GD functions that can generate jpg, gif or png and send them as output, but the default content type of PHP is text or html. So when you output an image to the browser you need to tell the browser, hey look! now I am sending an image, you should not treat it as text or html.

For example when the browser requests a PHP or HTML page, browsers sends some information to the server, the server then looks for the file requested, if it is available it sends back the content along with some information of how to treat the content. That “some information” is called header.

Header information is sent using header() in PHP by passing the header string as parameter. There are lots of header strings for various operations and writing a complete list will be beyond the scope of this article. But let us look into setting up headers for image output. You can clearly see the headers received by the browser when a page is loaded. You can see this in real-time by installing Firebug.

For example take a look at the following 2 screenshots.

 

Headers for HTML Content received by the browser

Headers for images received by the browser

 

You could see Content-Type changes for html and images sent to the browser, but these headers must be sent to the browser before you send any other output.

HEADERS ALREADY SENT – WHAT? I DINT SEND ANY..

This is the usual panic attack faced by PHP programmers, but let me tell you don’t panic. The problem might have occurred for the following reasons.

Intentional mistakes

  • You might have used echo, print or other functions like var_dump() that produces output before sending the header.
  • <html> tags before <?php – placing an <html> tag before sending header will send the output to the browser.

Yeah, I can hear you saying – “I am not that stupid”. Well, you are not, but the error might have occurred for the following reasons.

Unintentional mistakes

  • Blank white-spaces before <?php – a single white space is enough to give you a mild heart attack.
  • UTF-8 Byte Order Mark( BOM ) – I would like to call it BOM(B) ready to explode as soon as you execute your PHP script. The byte order mark (BOM) is a Unicode character used to signal the byte order of a text file or stream.
  • error messages, warnings or notices that occurred somewhere before sending the header.
<?php
 
    $im = imagecreatetruecolor( 200, 200 );
    $color = imagecolorallocate( $im, 255, 0, 0 );
    imageellipse( $im, 100, 100, 150, 150, $color );
 
    header( "Content-type: image/png" );
    imagepng( $im );

The above code generates an image, and in the final line imagepng() function sends the image directly to the browser, so we need to tell the browser that we are sending an image and so theheader() function specifying the content type is set. In the above code we don’t have any  output functions before the header is sent, no blank space before PHP tag, but still you will get the error “Headers already sent”. Most of the time this error occurs because of the BOM character that is embedded within the document while saving them. You could remove them easily. Follow these simple steps.

  • Install Notepad++
  • From the – Encoding menu at the top, select “Convert to UTF-8 without BOM”, then save it.

 

You will get rid of that BOM character and your PHP will run fine. Now the sample code will show an image in the browser, there arises another problem – You try to save that image, and you will see that while saving images generated by PHP your image filename will be the same as the PHP script that sent the image to the browser.

 

You can see .php is the extension instead of png, jpg or gif. Now if you want to give a proper name and type for the image output that is sent to the browser you have to send another header like the one below.

header(&quot;Content-Disposition: inline; filename=myimage.png&quot;);

The above line says the content sent to the browser is to be opened inline and to be called asmyimage.png. But there is also another type of Content-Disposition, that is attachment.The difference between inline and attachment is

  • inline – tells the browser to display it inline, the browser will attempt to display the content, for example png, jpg, gif, pdf files etc. But if the browser could not to display the content it will show the download dialog to save it in some other location.
  • attachment – will force the browser to open the download dialog, so the browser will not attempt to display the content even if it can, but shows the download dialog.

 

source: http://techlister.com

 
by Janeth Kent Date: 30-04-2013 php hits : 2072  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.

 
 
 

Related Posts

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The most popular Array Sorting Algorithms In PHP

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations,…

MySQL 8.0 is now fully supported in PHP 7.4

MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules…

A roadmap to becoming a web developer in 2019

There are plenty of tutorials online, which won't cost you a cent. If you are sufficiently self-driven and interested, you have no difficulty training yourself. The point to learn coding…

10 PHP code snippets to work with dates

Here we have some set of Useful PHP Snippets, which are useful for PHP Developers. In this tutorial we'll show you the 10 PHP date snippets you can use on…

8 Free PHP Books to Read in Summer 2018

In this article, we've listed 8 free PHP books that can help you to learn new approaches to solving problems and keep your skill up to date.   Practical PHP Testing This book…

Best Websites to Learn Coding Online

You know and we know that it’s totally possible to learn to code for free... If you can teach yourself how to write code, you gain a competitive edge over your…

PHP and XMP format: Create a Panorama 360/VR viewer in PHP and A-Frame

The Extensible Metadata Platform (XMP) is an ISO standard, originally created by Adobe Systems Inc., for the creation, processing and interchange of standardized and custom metadata for digital documents and…

PHP: Basic Introduction to Namespaces

History of PHP Namespaces In PHP prior to 5.3 (2009), any class you define lived at the same global level as other classes. Class User, class Contact, class Db they're all together…

PHP 7.2 will be the first Programming Language to add Modern Cryptography to its Standard Library

Last week, the voting phase closed on an RFC to add libsodium to PHP 7.2. The result was unanimous (37 in favor, 0 against). When version 7.2 releases at the end…

Php: How to extend the highlight_string function

PHP has a cool function that automatically highlights PHP code called highlight_string(); Theoretically this could be used to roll your own code highlighting on a site, rather than rely on…

Install Apache, MariaDB and PHP7 on Ubuntu 16.04

Ubuntu 16.04 LTS Xenial Xerus comes with PHP7 by default so you don’t have to rely on third-party PPA to get PHP7 installed. In this tutorial, we are going to…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration