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
DOWNLOAD SOURCE CODE

This is how I used it:

<html>
    <head>
        <title>Upload, Resize and Crop an Image with PHP - https://www.codeofaninja.com/</title>
     
    </head>
<body>
<?php
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>
</form>
</body>
</html>

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:

after-submit

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

By Mike Dalisay

I'm Mike Dalisay, a pro web developer since 2010. I love web development. Improving our tutorials and source codes makes me happy. Do you want to suggest an edit to our tutorial? Got something to ask about our source codes? You may use our comments section below or email our team at [email protected]

2 comments

  1. Hello, I am using this code and I want it to crop before resize, crop, because that is needed to get the coordinates to be used before resize images to insert into the web.

    Source -> crop – resize:

    800 x 600 ==> 300×200 ==> 500×400

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top