Image resizing and cropping with Php

DomingoSL picture DomingoSL · Feb 26, 2013 · Viewed 9.2k times · Source

I'm using a php script to upload and resize an image, pretty simple:

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $image = $_FILES["image_upload"];
    $uploadedfile = $image['tmp_name'];

    if ($image) {
        $filename = stripslashes($_FILES['image_upload']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
            $error_txt = 'Immagine incoretta';
            $errors=1;
        } else {
            $size=filesize($uploadedfile);
            if ($size > MAX_SIZE*1024) {
                $error_txt = "Immagine troppo grande";
                $errors=1;
            }
            if($extension=="jpg" || $extension=="jpeg" ) {
                $uploadedfile = $uploadedfile;
                $src = imagecreatefromjpeg($uploadedfile);
            } else if($extension=="png") {
                $uploadedfile = $uploadedfile;
                $src = imagecreatefrompng($uploadedfile);
            } else {
                $src = imagecreatefromgif($uploadedfile);
            }

            list($width,$height)=getimagesize($uploadedfile);

            $newwidth=500;
            $newheight=375;
            $tmp=imagecreatetruecolor($newwidth,$newheight);



            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);


            $filename = "images/". generateRandomString(5) . $image['name'];

            imagejpeg($tmp,$filename,100);

            imagedestroy($src);
            imagedestroy($tmp);
        }
    }

I want to got a bit further, right now im just resizing the image no matter the proportions, the think is, i want to resize it to a fixed with and height without losing the original proportion, and this of course is achieved through the cropping+resize of the original image.

I have no idea how to do this using my actual imagecreatetruecolor and imagecopyresampled functions, and looking to the php manual seems is not very easy.

There is a very good library im trying to integrate to my code, the use its as simple as mysite.com/lib/timthumb.php?src=castle1.jpg&h=180&w=120 but i dont know how to integrate that with my actual code.

So, what do you suggest?

Answer

chrislondon picture chrislondon · Mar 22, 2013

Please forgive me if there are any typos or anything in the following code. I haven't tested it. What I've done here is calculate whether the height or the width is the proportion that's too long. Then adjust the source dimension to match the final image dimension. Also, adjust the center of the side that we shrunk so the cropped image is centered.

    $newwidth  = 500;
    $newheight = 375;
    $tmp       = imagecreatetruecolor($newwidth, $newheight);

    $widthProportion  = $width / $newwidth;
    $heightProportion = $height / $newheight;

    if ($widthProportion > $heightProportion) {
        // width proportion is greater than height proportion
        // figure out adjustment we need to make to width
        $widthAdjustment = ($width * ($widthProportion - $heightProportion));

        // Shrink width to proper proportion
        $width = $width - $widthAdjustment;

        $x = 0; // No adjusting height position
        $y = $y + ($widthAdjustment / 2); // Center the adjustment
    } else {
        // height proportion is greater than width proportion
        // figure out adjustment we need to make to width
        $heightAdjustment = ($height * ($heightProportion - $widthProportion));

        // Shrink height to proper proportion
        $height = $height - $heightAdjustment;

        $x = $x + ($heightAdjustment / 2); // Center the ajustment
        $y = 0; // No adjusting width position
    }

    imagecopyresampled($tmp, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);

So basically with the $width and $height variables you are specifying how much of the picture you want (cropping). and with the $x, $y we're saying where we want to crop the picture. The rest is just the standard resizing to fit the full new image.

I hope that helps!