Home » Support » Help Articles » Resize Uploaded Image with PHP

Resize Uploaded Image with PHP

Back to Help Articles Home


Image resizing certainly comes in handy for PHP applications that allows users upload photos. Image re-sizing helps users to create different versions of the same image. The following steps are required to re-size an uploaded image.

    Step 1: Create a Form to Upload Image

  1. To create the form use the following code.
    <form method=”post” action=”upload.php” enctype=”multipart/form-data”>
    <label>Upload File</label>
    <input type=”file” name=”imageFile”>
    <input type=”submit” value=”Upload & Resize”>
    </form>
  2. Figure One

    Step 2: Create a Function to Resize image

  3. To create a function to resize the images using the following code.
    function resizePhoto($sExtName, $sFileOrigPath, $sFileSizePath, $iWidthSizeImg, $iHeightSizeImg)
    {
    $aImageSize = getimagesize($sFileOrigPath); // Get dimensions of uploaded image in array
    $iWidth = $aImageSize[0]; //Width of Image
    $iHeight = $aImageSize[1]; // Height of Image

    list($sFileSourceWidth, $sFileSourceHeight, $sFileSourceType) = getimagesize($sFileOrigPath);

    $iNewWidth = $iWidthSizeImg; //Width of Resized Image
    $iNewHeight = $iHeightSizeImg; //Height of Resized Image

    if($sExtName == 'jpg' || $sExtName == 'jpeg' || $sExtName == 'JPG' || $sExtName == 'JPEG')
    {
    //var $sExtName is the extension of image
    $img_src = imagecreatefromjpeg( $sFileOrigPath );
    $img_dst = imagecreatetruecolor( $iNewWidth, $iNewHeight );
    imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iWidth, $iHeight );
    imagejpeg( $img_dst, $sFileSizePath, 90 );
    }
    }
  4. Step 3:Create a Function to Upload Image

  5. To create a function to upload the images using the following code.
    function uploadImage()
    {
    //IMAGE FILE
    $sPhotoFileName = $_FILES['imageFile']['name'];
    $sPhotoFileName = str_replace(' ','-',$sPhotoFileName);
    $aFileExt = explode('.', $sPhotoFileName);
    $sFileExt = array_pop($aFileExt);
    if($sPhotoFileName)
    {
    $sPhotoFileName = date("Ymdhis").'.'.$sFileExt;
    $sTmpFileName = $_FILES['pageImage']['tmp_name'];

    $sFileOrig = 'http://example.com/files/original/'.$sPhotoFileName; //Path of Original Image
    $sFileSml = 'http://example.com/files/thumbnail/'.$sPhotoFileName; // Path of Re-sized image

    $iWidthSmImg = '150'; //Width of resized image
    $iHeightSmImg = '236'; //Height if Re-sized Image


    if(move_uploaded_file($sTmpFileName, $sFileOrig))
    {
    //Thumbnail Image Resize
    $bResize = $this->_resizePhoto($sFileExt, $sFileOrig, $sFileSml, $iWidthSmImg, $iHeightSmbImg);

    }
    }
    }


Updated on Nov 11, 2015

The techReview is an online magazine by Batoi and publishes articles on current trends in technologies across different industry verticals and areas of research. The objective of the online magazine to provide an insight into cutting-edge technologies in their evolution from labs to market.

Visit techReview


English - IN (USD)
New Users? Signup.     Existing Users? Login.