QAds
Apple iPod shuffle 1 GB Yesil
$35.00
Apple iPad 16gb Wi-Fi
$448.97
Apple iPod nano 8 GB Siyah
$88.21
Nokia N8 Touchscreen Phone Siyah - gps navigation, Voice Navigation, 12 MP Camera
$449.99
:: HOME PHP Image Resizing & Uploading
 
Image Resizing & Uploading

To be able to resize image with PHP, You can use below function :

PHP Resize Image function :

     function image_resize($type, $l, $w, $h)
    {
        if($type=="png") header("Content-type: image/png");
        elseif($type=="jpg") header("Content-type: image/jpeg");
        elseif($type=="gif") header("Content-type: image/gif");

        if($type=="png")     $imgs=imagecreatefrompng($l);
        elseif($type=="jpg")     $imgs=imagecreatefromjpeg($l
);
        elseif($type=="gif")     $imgs=imagecreatefromgif($l
);

        $width=imagesx($imgs);
        $height=imagesy($imgs);
    
        $sheight = $h;
        $swidth = $w;
        $imgss = ImageCreateTrueColor($swidth,$sheight);

        imagecopyresampled($imgss,$imgs,0,0,0,0,$swidth,$sheight,$width,$height);

        if($type=="png")     imagepng($imgss,$l);
        elseif($type=="jpg")     imagejpeg($imgss,$l);
        elseif($type=="gif")     imagegif($imgss,$l);
        

        imagedestroy($imgs);
        imagedestroy($imgss);
    }

 As you see, we get 4 variables to resize our image. At first we get the type of the image($type), location of image ($l), requested width of image ($w) and requested height of image($h).

PHP file that upload image and run image_resize function :

         upload($_POST['width'], $_POST['height'], $_FILES['img']);

        function upload($h, $w, $photo)

        {

        if($photo['size']>2000000) return false; // put your maximum file size limit
       
        $i = strlen($photo['name']);
        $type = substr($photo['name'],($i-3),3); // getting image type

        if($type<>"jpg" AND $type<>"gif" AND $type<>"png") return false; // check image type
       
        $randnum="uploaded"."_".time(); // determine image name
           
        $loc="images/".$randnum.".".$type.""; // image location on server

        copy($photo['tmp_name'],$loc); // copy image to server

        image_resize($image,$type,$loc,$w,$h); // resizing

        $image = $randnum.".".$type;
           
        if(file_exists($loc))
            // do whatever you want

        }

HTML file with upload form to upload image :

<form action="upload.PHP" method="post" enctype="multipart/form-data">
<table border="1" cellspacing="0" cellpadding="0" width="100%">
    <tr>
        <td colspan="2">New Image</td>
    </tr>
    <tr>
        <td>Title : </td>
        <td><input type="file" name="img"></td>
    </tr>
    <tr>
        <td>Width : </td>
        <td><input type="text" name="width"></td>
    </tr>
    <tr>
        <td>Height : </td>
        <td><input type="text" name="height"></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" name="submit" value="UPLOAD">
        </td>
    </tr>
</table>
</form>