Make Image Thumbnail on Upload Time.


 Today, we will talk about how to Make a Thumbnail  Image of Image uploaded via a File Upload using PHP.

For this I have created a script, which we have to upload anywhere in the website, where you want to upload the files.

Now use this code and save as createimage.php


***********************Code***********************

<?php
if(!isset($_GET['fileurl']))
{
exit();


$fileurl = $_GET['fileurl'];

// actual script begins here
$type=false;
function open_image ($file) {
    //detect type and process accordinally
    global $type;
    $size=getimagesize($file);
    switch($size["mime"]){
        case "image/jpeg":
            $im = imagecreatefromjpeg($file); //jpeg file
        break;
        case "image/gif":
            $im = imagecreatefromgif($file); //gif file
      break;
      case "image/png":
          $im = imagecreatefrompng($file); //png file
      break;
    default: 
        $im=false;
    break;
    }
    return $im;
}

$url = $_GET['url'];
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($url))) {
  // send the last mod time of the file back
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($url)).' GMT', true, 304); //is it cached?
} else {

$image = open_image($fileurl);

if ($image === false) { //die ('Unable to open image'); 
}

$w = imagesx($image);
$h = imagesy($image);

//calculate new image dimensions (preserve aspect)

$_GET['w'] = 1600;
$_GET['h'] = 1600;

if(isset($_GET['w']) && !isset($_GET['h'])){
    $new_w=$_GET['w'];
    $new_h=$new_w * ($h/$w);
} elseif (isset($_GET['h']) && !isset($_GET['w'])) {
    $new_h=$_GET['h'];
    $new_w=$new_h * ($w/$h);
} else {
    $new_w=isset($_GET['w'])?$_GET['w']:560;
    $new_h=isset($_GET['h'])?$_GET['h']:560;
    if(($w/$h) > ($new_w/$new_h)){
        $new_h=$new_w*($h/$w);
    } else {
        $new_w=$new_h*($w/$h);    
    }


$im2 = ImageCreateTrueColor($new_w, $new_h);
imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
//effects
if(isset($_GET['blur'])){
    $lv=$_GET['blur'];
    for($i=0; $i<$lv;$i++){
        $matrix=array(array(1,1,1),array(1,1,1),array(1,1,1));
        $divisor = 9;
        $offset = 0;
        imageconvolution($im2, $matrix, $divisor, $offset); 
    } 
}
if(isset($_GET['sharpen'])){
    $lv=$_GET['sharpen'];
    for($i=0; $i<$lv;$i++){
        $matrix = array(array(-1,-1,-1),array(-1,16,-1),array(-1,-1,-1));
        $divisor = 8;
        $offset = 0;
        imageconvolution($im2, $matrix, $divisor, $offset);
    } 
}

header('Content-type: image/jpeg');
$name=explode(".", basename($_GET['url']));
header("Content-Disposition: inline; filename=".$name[0]."_t.jpg");
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($url)) . ' GMT');
header("Cache-Control: public");
header("Pragma: public");
imagejpeg($im2);
}   

?>

***********************End of Code***********************

Now You just have to change on your uploader.php , just the following code, which contain "move_uploaded_file" Function whith the Following code.


***********************Code***********************


<?php

if(move_uploaded_file($_FILES["file"]["tmp_name"],"folderpathrespecttodomain/token".$a.$_FILES["file"]["name"]))

{
$newfilename = $a.$_FILES["file"]["name"];
$fullurl = "domainname/folderpathrespecttodomain/token".$newfilename;
$createimagefile = "domainname/images/createimage.php?fileurl=".$fullurl;
if(copy($createimagefile,'../images/advertismentupload/'.$newfilename))
{
unlink("pyysicalpathothefolder
/
folderpathrespecttodomain
/
token
".$newfilename);
}


}
?>

***********************End of Code***********************




IF You have to Know about, How to Upload file using PHP, Click Here






Comments

Popular posts from this blog

Page Unload Show Notification to user

Function Oriented File Upload PHP

Upload File Using PHP - Example For Image