Thursday, September 27, 2012

Get MIME type


If you looking for a solution to find the File Content-Type or MIME Type.
Detecting MIME Type of each file extension is necessary to build dynamic file processing in PHP

Example:

// In the following code image/png is MIME type which is assigned dynamically
header("Content-Type:image/png"); 


We can get MIME Type using the following simple php code


Here is the best solution to find the Exact MIME Type of the file using the following simple upload form.

<?php
session_start();

if(isset($_POST['upload']))
{
 $mime_type =  $_FILES['upload_file']['type'];
 $_SESSION['mime_type'][] = $mime_type;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload and Resize Image</title>
<style type="text/css">
body,td,th {
 font-size: 14px;
 font-family: Verdana, Geneva, sans-serif;
}
body {
 margin-left: 10px;
 margin-right: 10px;
}
</style>
</head>
<body>

<form action="" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend>Upload File in PHP</legend>
    
        <input name="upload_file" type="file" id="upload_file" />
        
        <input name="upload" type="submit" id="upload" value="Upload" />
        
    </fieldset>
</form>

<div style="padding:10px;">
Uploaded File MIME Type is : <br />
<pre>
<?php 
if(is_array(@$_SESSION['mime_type']))
foreach($_SESSION['mime_type'] as $type)
echo @$type."\n"; ?>
</pre>
</div>

</body>
</html>


Download This Script     Live Demo  

Resize Image when upload PHP


Upload and Resize Image dynamically. when upload a image file in PHP is very use full to manage images on web development.
here is my solution how to upload image after resize  uploaded image in PHP


Upload Rezied Image on Server

Upload and Resize Image PHP

Upload-Resize.php (full Source)

<?php

if(isset($_POST['upload_image']))
{
 
 // Check Valid Image 
 if(preg_match("/(jpg)|(png)|(gif)/i",$_FILES['image_file']['name']))
 {
   
 // Load uploaded image from $_FILES
 // encode type in form need to set | encode="multipart/form-data" |
 $filename = $_FILES['image_file']['tmp_name'];
 
 // Prepare target image name with path on server appending with timestamp
 $target_file_name = "Image".time();
 $target_file_name = "images/$target_file_name";  
 
 $image_info = getimagesize($filename);
 $image_type = $image_info[2];


  
  if( $image_type == IMAGETYPE_JPEG ) {
   $image = imagecreatefromjpeg($filename);
   $target_file_name.=".jpg"; // append file extention based on image type;
  } elseif( $image_type == IMAGETYPE_GIF ) {
   $image = imagecreatefromgif($filename);
   $target_file_name.=".gif";  
  } elseif( $image_type == IMAGETYPE_PNG ) {
   $target_file_name.=".png";  
   $image = imagecreatefrompng($filename);
  }
  
  // get width height of upload image file
  $height = imagesy($image);
  $width = imagesx($image);
 
  // SET THE SIZE OF TARGET IMAGE i use 250 x 250
  $target_width = 250;
  $target_height = 250;
  // Create a Empty Image Canvas
  $new_image = imagecreatetruecolor($target_width,$target_height);
  imagealphablending($new_image, true);
  imagesavealpha($new_image, true);
  
  // Set Canvas Background Color / Transparent as per your requirement
  // I prefer to go with Transparent
  
  // Make canvas Filled Background Color
  $white = imagecolorallocate($new_image, 255, 255, 255);
  $grey = imagecolorallocate($new_image, 128, 128, 128);
  $black = imagecolorallocate($new_image, 0, 0, 0);
  imagefilledrectangle($new_image, 0, 0, $target_width,$target_height, $grey); 
  
  // Make Canvas Transparent
  $trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
  imagefill($new_image, 0, 0, $trans_colour);
 
  // Copy Source Image to Canvas
  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $target_width,$target_height,$width, $height);
  $image = $new_image;
  

  
  // Save image based on file type;
  if( $image_type == IMAGETYPE_JPEG ) {
   imagejpeg($image,$filename,100);
  } elseif( $image_type == IMAGETYPE_GIF ) {
   imagegif($image,$target_file_name);         
  } elseif( $image_type == IMAGETYPE_PNG ) {
   imagepng($image,$target_file_name);
  }   
   chmod($target_file_name,777);
  
  $response = '<span style="color:#060">Image Successfully Uploaded!</span>';
 }else
 {
  $response = '<span style="color:#FF0000;">Select a Valid (JPG,GIF,PNG) Images</span>';
 }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload and Resize Image</title>
<style type="text/css">
body,td,th {
 font-size: 14px;
 font-family: Verdana, Geneva, sans-serif;
}
body {
 margin-left: 10px;
 margin-right: 10px;
}
</style>
</head>
<body>

<div style="padding:10px;">
<?php echo @$response; ?>
</div>
<form action="" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend>Upload Image</legend>
    
        <input name="image_file" type="file" id="image_file" />
        
        <input name="upload_image" type="submit" id="upload_image" value="Upload" />
        
    </fieldset>
</form>
</body>
</html>