How to upload and resize an image through GD PHP ?

Description:- Through this simple example you can easily upload your image file with the extensions of gif, png, jpg and jpeg and after uploading it will automatically resized.

For this example I made three different files :-
1. my_file.php
2. upload_file.php
3. webtechball_1.0.php


Step1:- Creating my_file.php

<!DOCTYPE HTML>
<head>
    <title></title>
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">

<h2>Choose file:</h2> 
<input type="file" name="uploads"/> <br/>
<input type="submit" value="Upload"/>

</form>
</body>
</html>

Step2:- Creating upload_file.php

<?php
$name = $_FILES["uploads"]["name"];
$tmpName = $_FILES["uploads"]["tmp_name"];
$type = $_FILES["uploads"]["type"];
$size = $_FILES["uploads"]["size"];
$errorMsg = $_FILES["uploads"]["error"];
$explode = explode(".",$name);
$extension = end($explode);
//starting PHP image upload error handlings
if(!$tmpName)
{
    echo "ERROR: Please choose file";
    exit();
}
else if($size > 5242880)// if file size is larger than 5MB 
{
    echo "ERROR: Please choose less than 5MB file for uploading";
    unlink($tmpName);
    exit();
}
else if(!preg_match("/\.(gif|jpg|png|jpeg)$/i",$name)) 
{
    echo "ERROR: Please choose the file only with the GIF, PNG or JPG file format";
    unlink($tmpName);
    exit();
}
else if($errorMsg == 1)
{
    echo "ERROR: An unexpected error occured while processing the file. Please try again.";
    exit();
}
// End of PHP image upload error handlings

//Placing folder "uploads" where files will going to uploaded
$moveFile = move_uploaded_file($tmpName,"uploads/$name");

if($moveFile != true)
{
    echo "ERROR: File not uploaded. Please try again";
    unlink($tmpName);
    exit();
}


// ------Resizing your image ----------- 
include_once("webtechball_1.0.php");
$target = "uploads/$name";
$resize = "uploads/new_$name";
$max_width = 200; // maximum width of new file. Change it according to your need
$max_height = 150; // maximum height of new file. Change it according to your need
webtechball_img_resize($target, $resize, $max_width, $max_height, $extension);
//-----------End of resizing your image-----------

echo "<h2>Original image:-</h2> ";
echo "<img src='uploads/$name' /> <br/>";
echo "<h2>Resized image:-</h2> ";
echo "<img src='uploads/new_$name' />"

?>

Step3:- Creating webtechball_1.0.php

<?php
// This library file is made by webtechball
// Starting of function called webtechball_img_resize made by webtechball
function webtechball_img_resize($targett, $newcpy, $w, $h, $extn)
{

    list($origWidth, $origHeight) = getimagesize($targett);

    $ratio = $origWidth / $origHeight;

    if(($w / $h) > $ratio)
    {
        $w = $h * $raio;
    }
    else
    {
        $h = $w * $ratio;
    }
    
    $img="";
    $extn = strtolower($extn);
    if($extn == "gif")
    {
        $img = imagecreatefromgif($targett);
    }
    else if($extn == "png")
    {
        $img = imagecreatefrompng($targett);
    }
    else
    {
        $img = imagecreatefromjpeg($targett);
    }
    $a = imagecreatetruecolor($w, $h);
    
    imagecopyresampled($a,$img,0,0,0,0,$w,$h,$origWidth,$origHeight);
    imagejpeg($a,$newcpy,80);
}
// End of function called webtechball_img_resize made by webtechball
?>

*Note :- Remember one important thing don’t forget to make a new folder and rename it as “uploads”. In this folder your all uploaded files as well as your resized files will going to save. Follow the video for more details….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s