Archive for January, 2012

Upload and Resize Image with PHP

here is a simple script for Upload and Resize an Image using PHP script:

make folder, to save uploaded image with name: pics

use this php script bellow:

<?php
 	 if(isset($_POST['Submit']))
 	 {
 	     $size = 150; // the thumbnail height
 	     $filedir = 'pics/'; // the directory for the original image
 	     $thumbdir = 'pics/'; // the directory for the thumbnail image
 	     $prefix = 'small_'; // the prefix to be added to the original name
 	     $maxfile = '2000000';
 	     $mode = '0666';
 	     $userfile_name = $_FILES['image']['name'];
 	     $userfile_tmp = $_FILES['image']['tmp_name'];
 	     $userfile_size = $_FILES['image']['size'];
 	     $userfile_type = $_FILES['image']['type'];
 	     if (isset($_FILES['image']['name'])) 
 	     {
 	         $prod_img = $filedir.$userfile_name;
 	         $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
 	         move_uploaded_file($userfile_tmp, $prod_img);
 	         chmod ($prod_img, octdec($mode));
 	         $sizes = getimagesize($prod_img);
 	         $aspect_ratio = $sizes[1]/$sizes[0]; 
 	         if ($sizes[1] <= $size)
 	         {
 	             $new_width = $sizes[0];
 	             $new_height = $sizes[1];
 	         }else{
 	             $new_height = $size;
 	             $new_width = abs($new_height/$aspect_ratio);
 	         }
 	         $destimg=ImageCreateTrueColor($new_width,$new_height)
 	             or die('Problem In Creating image');
 	         $srcimg=ImageCreateFromJPEG($prod_img)
 	             or die('Problem In opening Source Image');
 	         if(function_exists('imagecopyresampled'))
 	         {
 	             imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
 	             or die('Problem In resizing');
 	         }else{
 	             Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
 	             or die('Problem In resizing');
 	         }
 	         ImageJPEG($destimg,$prod_img_thumb,90)
 	             or die('Problem In saving');
 	         imagedestroy($destimg);
 	     }
 	     echo '
 	     <a href="'.$prod_img.'">
 	         <img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'">
 	     </a>';
 	 }else{
 	     echo '
 	     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
 	     <input type="file" name="image"><p>
 	     <input type="Submit" name="Submit" value="Submit">
 	     </form>';
 	 }
?>

source: http://xenno.org

,

Leave a comment

PHP validate image upload

here is some step to validate image upload using php, and save file info into database.

Create database: “db_images”

Create table “img_table” on database “db_images”

CREATE TABLE IF NOT EXISTS `img_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(255) NOT NULL,
  `filesize` int(11) NOT NULL,
  `location` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

connect.php

<?php
$host = "localhost";
$user = "root";
$pass = "root";
$dbName = "db_images";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed !! : ".mysql_error());
?>

index.php

<form enctype="multipart/form-data" action="validate_image_upload.php" method="POST">
   	<!-- MAX_FILE_SIZE must be set before the input element -->
	<input type="hidden" name="MAX_FILE_SIZE" value="2048000" />

	<!-- The name from the $_FILES array is determined by the input name -->
	Select an Image: <input name="image_file" type="file" /> <input type="submit" value="Upload" />
</form>

validate_image_upload.php

<?php
include "connect.php";
// Do not show notice errors
error_reporting (E_ALL ^ E_NOTICE);
if(!empty($_FILES)){ // Has the image been uploaded?

	/* 
	1 = Check if the file uploaded is actually an image no matter what extension it has
	2 = The uploaded files must have a specific image extension
	*/
	$validation_type = 1;
	if($validation_type == 1){
		$mime = array('image/gif' => 'gif',
					  'image/jpeg' => 'jpeg',
					  'image/png' => 'png',
					  'application/x-shockwave-flash' => 'swf',
					  'image/psd' => 'psd',
					  'image/bmp' => 'bmp',
					  'image/tiff' => 'tiff',
					  'image/tiff' => 'tiff',
					  'image/jp2' => 'jp2',
					  'image/iff' => 'iff',
					  'image/vnd.wap.wbmp' => 'bmp',
					  'image/xbm' => 'xbm',
					  'image/vnd.microsoft.icon' => 'ico');
	}
	else if($validation_type == 2){ // Second choice? Set the extensions
		$image_extensions_allowed = array('jpg', 'jpeg', 'png', 'gif','bmp');
	}
	$upload_image_to_folder = 'images/';	
		
	$file 		= $_FILES['image_file'];
	$fileSize 	= $_FILES['image_file']['size']; //get the file size
	$file_name 	= $file['name'];
	$error 		= ''; // Empty

	// Get File Extension (if any)
	$ext = strtolower(substr(strrchr($file_name, "."), 1));

	// Check for a correct extension. The image file hasn't an extension? Add one
	if($validation_type == 1){
	$file_info = getimagesize($_FILES['image_file']['tmp_name']);
      if(empty($file_info)){ // No Image?
		$error .= "The uploaded file doesn't seem to be an image.";}
      else{ // An Image?
      $file_mime = $file_info['mime'];
         if($ext == 'jpc' || $ext == 'jpx' || $ext == 'jb2'){
			 $extension = $ext;
			 }
		 else{
			 $extension = ($mime[$file_mime] == 'jpeg') ? 'jpg' : $mime[$file_mime];
			 }
		 if(!$extension){
			 $extension = '';  
			 $file_name = str_replace('.', '', $file_name); 
			 }
		}
   }
   else if($validation_type == 2){
	  if(!in_array($ext, $image_extensions_allowed)){
	  $exts = implode(', ',$image_extensions_allowed);
	  $error .= "You must upload a file with one of the following extensions: ".$exts;
	  }
	  $extension = $ext;
   }

   if($error == ""){ // No errors were found?
	   $new_file_name = strtolower($file_name);
	   $new_file_name = str_replace(' ', '-', $new_file_name);
	   $new_file_name = substr($new_file_name, 0, -strlen($ext));
	   $new_file_name .= $extension;
   
	   // File Name
	   $move_file = move_uploaded_file($file['tmp_name'], $upload_image_to_folder.$new_file_name);

	   if($move_file){
	   		$query = "INSERT into img_table VALUES('','$file_name','$fileSize','images/$file_name')"; //insert image property to database
			$result= mysql_query($query);
		    $done = 'The image has been uploaded.';
		   }
   }
   else{
   @unlink($file['tmp_name']);
   }
   $file_uploaded = true;
}

 if($file_uploaded) {
	 if($done){	echo '<font color="green">'.$done.'</font>'; }
	 else if($error){echo '<font color="red">'.$error.'</font>'; }
	 echo '<br /><br />';
 }
?>

Leave a comment

Centering div on mindle page (horizontal & vertical)

 

css code:

#centerVerticalHorizontal{
width:725px; /* widthvalue */
height:530px; /* height value */
position:absolute;
top:50%;
left:50%;
margin:-265px 0 0 -362px; /*value that height and width devide 2*/
}

, ,

Leave a comment

Design a site like this with WordPress.com
Get started