Showing posts with label PHP Data URI. Show all posts
Showing posts with label PHP Data URI. Show all posts

Saturday, February 25, 2012

Reduce HTTP Request with Data URI in PHP


A part of website optimization technique is Reduce no of HTTP Request when user attempt to open our Web page from our website. HTTP Request count is an important factor for Traffic worthy websites to make your web pages / websites more responsive on time. In this factor of HTTP Request optimization to reduce the HTTP request count of various images in our websites by Rewriting with Data URI Scheme. Data URI is the technology which is used to Reduce and Optimize HTTP request and response.



For example If you have a gallery section in your website. Gallery images are in two Sections one is Thumbnails and another one is actual images. in this case set of 5 Images will request 10 HTTP request every time the Page opens. in this case we can use Data URI with PHP to reduce this HTTP request 0, Data URI will cheat our server and retrieve the image data as plain code along with our standard HTML code.

Monday, October 10, 2011

HTML code for an image

Embedded HTML Code for an Image is an advanced technique for Modern Web. Standard HTML Images are map with <IMG> tag using src attribute src attribute is to be an URL of an Image.

Traditional Image Tag Example:
<img src="pathof-image.jpg" />

HTML code for an image
In Modern Image tag  Image Soruce Placed It self with in the SRC using Image Tag Data URI Scheme

Click Here to View how to create  Image Tag Data URI Scheme

Following PHP Function we can build the HTML Code for an Image.


<?php
// Funtion to prepare Data URI:
// @ parm: Image filepath, Image Size
// reutn : data_uri string
function thumbnail_data_uri($filename,$width=0,$height=0) 
{
 
 // Get Image Header Type
      $image_info = getimagesize($filename);
      $image_type = $image_info[2];

 // Check the Size is Specified
 if($width!=0 and $height!=0)
 {
      if( $image_type == IMAGETYPE_JPEG ) {
         $image = imagecreatefromjpeg($filename);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         $image = imagecreatefromgif($filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         $image = imagecreatefrompng($filename);
   imagealphablending($image, true);

      } 

 $new_image = imagecreatetruecolor($width, $height);
 imagealphablending($new_image, false);
 imagesavealpha($new_image, true);     
      imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image),imagesy($image));
      $image = $new_image; 
   
 ob_start();
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($image);
      } 
 $content=ob_get_clean();   
   
   
 }else
 $content=file_get_contents($filename,true);
 
 
 
 $base64 = base64_encode($content); 
 return "data:$image_type;base64,$base64";
}

?>