Great Way to Upload, Resize and Crop an Image with PHP

Hi guys! Today I want to share with you a great way (well, at least for me) to upload, resize and crop an Image with PHP. We’re gonna use a class here. I found this class really amazing and works well. This class is really easy and simple to implement.

Great Way to Upload, Resize and Crop an Image with PHP
Great Way to Upload, Resize and Crop an Image with PHP

This is how I used it:

        <title>Upload, Resize and Crop an Image with PHP -</title>
if( isset($_FILES['image'] ) ) { //if the image was uploaded
    //include our class
    include 'libs/img_upload_resize_crop.php';
    //instantiate our class
    $your_image = new _image;
    //To Upload
    //set the location
    $your_image->uploadTo = 'uploads/';
    $upload = $your_image->upload($_FILES['image']);
    echo "<div>" . $upload . "</div>";
    //To Resize
    $your_image->newPath = 'thumbs/';
    //set new image dimensions
    $your_image->newWidth = 400;
    $your_image->newHeight = 300;
    $resized = $your_image->resize();
    echo "<div>" . $resized . "</div>";
    //To Crop
    //dimensions of cropped image
    $width = "150"; 
    $height = "100";
    //we will supply coordinates so that the class would
    //know what part of the image we want to start cropping
    $fromX = "0"; //x coordinate
    $fromY = "0"; //y coordinate
    $your_image->newPath = 'cropped/';
    $cropped = $your_image->crop($width,$height,$fromX,$fromY);
    echo "<div>" . $cropped . "</div>";
    echo "Image was successfully uploaded.";
<!-- Create your form -->
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <div><input type="file" name="image" id="image" /></div>
    <div><input type="submit" name="button" id="button" value="Submit" /></div>

If you want more configuration of this class for example you want to decrease the image quality, etc., you can check the class’ code which is found in libs/img_upload_resize_crop.php and set your values there.

I run the script and after browsing the image and hitting the submit button, it look like this:


So I got what I want to do with my image after uploading it. Easy and simple right? Thanks to MJDigital.

Hi! I'm Mike Dalisay, the co-founder of, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.