Upload, resize, and crop center of image with PHP

16,819

Solution 1

The GD Library is a good place to start.

http://www.php.net/manual/en/book.image.php

Solution 2

There a simple to use, open source library called PHP Image Magician. It uses GD but simplifies it's usage to 3 lines.

Example of basis usage:

$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');

Solution 3

If you want an example to work from my upload, resize and crop class does all of this plus some other cool stuff - you can use it all if needed or just take the bits out that you like:

http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/

I don't think it is too bloated! - you can just do something this (not tested):

if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it
    include('INCLUDE_CLASS_FILE_HERE.php');
    $myImage = new _image;
    // upload image
    $myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE
    $myImage->returnType = 'array'; // RETURN ARRAY OF IMAGE DETAILS
    $img = $myImage->upload($_FILES['file']);
    if($img) {
        $myImage->newWidth = 116;
        $myImage->newHeight = 116;
        $i = $myImage->resize(); // resizes to 116px keeping aspect ratio
        // get new image height
        $imgWidth = $i['width'];
        // get new image width
        $imgHeight = $i['height'];
        if($i) {
            // work out where to crop it
            $cropX = ($imgWidth>116) ? (($imgWidth-116)/2) : 0;
            $cropY = ($imgHeight>116) ? (($imgHeight-116)/2) : 0;
            $cropped = $myImage->crop(116,116,$cropX,$cropY);
            if($cropped) { echo 'It Worked (I think!)'; print_r($cropped);
            } else { echo 'Crop failed'; }
        } else { echo 'Resize failed'; }
    } else { echo 'Upload failed'; }
Share:
16,819
Adam
Author by

Adam

Student, web developer, photographer.

Updated on June 04, 2022

Comments

  • Adam
    Adam almost 2 years

    I'm wanting to create a very very basic upload, resize, and crop PHP script. The functionality to this will be identical (last i checked anyway) to the method Twitter uses to upload avatar pictures.

    I want the script to take any size image, resize the shortest side to 116px, then crop off the top and bottom (or left and right side if it's landscape) as to get a square 116px by 116px.

    I don't want a bloated PHP script with client side resizing or anything, just a simple PHP resize and crop. How is this done?

  • mj7
    mj7 almost 13 years
    Thanks - although I realised in my original post I left out the last two parameters of the crop method - D'oh! I have fixed it above now so hopefully it should be OK - just in case you needed the crop method should be called using: $myImage->crop($cropToWidth,$cropToHeight,$cropFromX,$cropFr‌​omY);
  • MazarD
    MazarD almost 10 years
    The best by far. Lots of very easy to use features. Thank you!!
  • Wilf
    Wilf over 4 years
    This is working for me! May I ask, why those resize functions all return empty?