Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize images in PHP without using third-party libraries?

In one of my applications, I'm using the code snippet below to copy uploaded images to a directory. It works fine but copying large images (> 2MB) takes more time than ideal and I really don't need images this big, so, I'm looking for a way to resize the images. How to achieve this using PHP?

<?php

$uploadDirectory = 'images/0001/';
$randomNumber = rand(0, 99999); 
$filename = basename($_FILES['userfile']['name']);
$filePath = $uploadDirectory.md5($randomNumber.$filename);

// Check if the file was sent through HTTP POST.

if (is_uploaded_file($_FILES['userfile']['tmp_name']) == true) {

    // Validate the file size, accept files under 5 MB (~5e+6 bytes).

    if ($_FILES['userfile']['size'] <= 5000000) {

        // Move the file to the path specified.

        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filePath) == true) {

            // ...

        }

    }

}

?>
like image 989
Mateus Avatar asked Mar 10 '12 22:03

Mateus


People also ask

How do I scale an image in PHP?

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().

How do I automatically resize images in WordPress?

Adjust image sizes in the Settings > Media screen Here you can change width and height for the thumbnail, medium, and large image sizes. WordPress will then automatically create scaled versions of each image using these sizes. You can choose which size you want to insert from the WordPress editor.

How do I resize an image to fit in HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


1 Answers

Finally, I've discovered a way that fit my needs. The following snippet will resize an image to the specified width, automatically calculating the height in order to keep the proportion.

$image = $_FILES["image"]["tmp_name"];
$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";

copy($_FILES, $resizedDestination);

$imageSize = getImageSize($image);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];

$DESIRED_WIDTH = 100;
$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);

$originalImage = imageCreateFromJPEG($image);

$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);

imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
imageJPEG($resizedImage, $resizedDestination);

imageDestroy($originalImage);
imageDestroy($resizedImage);

To anyone else seeking a complete example, create two files:

<!-- send.html -->

<html>

<head>

    <title>Simple File Upload</title>

</head>

<body>

    <center>

        <div style="margin-top:50px; padding:20px; border:1px solid #CECECE;">

            Select an image.

            <br/>
            <br/>

            <form action="receive.php" enctype="multipart/form-data" method="post">
                <input type="file" name="image" size="40">
                <input type="submit" value="Send">
            </form>

        </div>

    </center>

</body>

<?php

// receive.php

$randomNumber = rand(0, 99999);
$uploadDirectory = "images/";
$filename = basename($_FILES['file_contents']['name']);
$destination = $uploadDirectory.md5($randomNumber.$filename).".jpg";

echo "File path:".$filePath."<br/>";

if (is_uploaded_file($_FILES["image"]["tmp_name"]) == true) {

    echo "File successfully received through HTTP POST.<br/>";

    // Validate the file size, accept files under 5 MB (~5e+6 bytes).

    if ($_FILES['image']['size'] <= 5000000) {

        echo "File size: ".$_FILES["image"]["size"]." bytes.<br/>";

        // Resize and save the image.

        $image = $_FILES["image"]["tmp_name"];
        $resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";

        copy($_FILES, $resizedDestination);

        $imageSize = getImageSize($image);
        $imageWidth = $imageSize[0];
        $imageHeight = $imageSize[1];

        $DESIRED_WIDTH = 100;
        $proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);

        $originalImage = imageCreateFromJPEG($image);

        $resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);

        imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
        imageJPEG($resizedImage, $resizedDestination);

        imageDestroy($originalImage);
        imageDestroy($resizedImage);

        // Save the original image.

        if (move_uploaded_file($_FILES['image']['tmp_name'], $destination) == true) {

            echo "Copied the original file to the specified destination.<br/>";

        }

    }

}

?>
like image 124
Mateus Avatar answered Sep 29 '22 12:09

Mateus