:: 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>


PHP Image Resizing & Uploading PHP Image Resizing & Uploading PHP Image Resizing & Uploading
PHP Image Resizing & Uploading
NEW QUESTION
 
Your E-Mail Address
»
Your e-mail will never be shown!  
   
 
Question
»
Please type your question and please use [CODE] tag for your codes!  
  Add CODE Quote BOLD ITALIC Underline Align Left Align Center Align Right Add Line  
   
 
Human Check
»
Enter human check code below!  
   
 
       
 
* Your Question will be answered as soon as possible, probably within 24 hours!
PHP Image Resizing & Uploading PHP Image Resizing & Uploading PHP Image Resizing & Uploading
PHP Image Resizing & Uploading
 
Quaware Internet Solutions And Consulting - SEO Search Engine Optimization, Website development and design, eCommerce, Online Payment Systems, Shopping Cart, Affiliates Website Designer, Search Engine Optimization Expert, Ajax Developer,Developed And Designed By Mumin Celik With Using PHP, MySQL, CSS, JavaScript, Ajax And Search Engine Optimizer Sinema, Film, Ask, Tekgul, Mutluluk