"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Create responsive thumbnails to maintain the quality of the original image

Create responsive thumbnails to maintain the quality of the original image

Posted on 2025-04-12
Browse:611

How to Create Responsive Thumbnails from Uploaded Images While Maintaining Original Quality?

Creating Responsive Thumbnails from Uploaded Images

When working with user-uploaded images, creating responsive thumbnails is crucial to enhance the user experience and maintain site performance. This guide addresses the challenge of generating thumbnails while preserving the original image quality.

Generating Thumbnails with PHP

PHP provides a range of image manipulation functions, including imagecopyresized(). To create a thumbnail from an uploaded image, follow these steps:

  1. Retrieve the original image: Use getimagesize() to obtain the original image's dimensions and MIME type.
  2. Calculate thumbnail dimensions: Determine the desired thumbnail size (e.g., 100x100 pixels).
  3. Create a new image resource: Establish a new image resource using imagecreatetruecolor().
  4. Resize the image: Utilize imagecopyresized() to resize the original image to fit the thumbnail dimensions.
  5. Save the thumbnail: Employ imagejpeg() or imagepng() to save the thumbnail to a desired location.

Preserving Original Image Quality

To maintain the original image's quality, use a higher $quality parameter in imagejpeg() or imagepng(). This parameter ranges from 0 to 100, with a higher value indicating better quality.

Utilizing ImageMagick

ImageMagick is a more robust image manipulation library. If installed on your server, you can leverage its Imagick class to generate thumbnails:

  1. Install ImageMagick: Ensure ImageMagick is installed on your server.
  2. Create Imagick object: Instantiate an Imagick object using the original image's path.
  3. Set image properties: Configure image format, compression, and quality using class methods.
  4. Resize the image: Employ thumbnailImage() to resize the image.
  5. Save the thumbnail: Use file_put_contents() to write the resized image to a new file.

Sample Code with Thumbnail Creation

Here's a sample imageupload.php file modified to include thumbnail generation:

...
if(isset($_FILES['image_data'])){
       if(is_uploaded_file($_FILES['image_data']['tmp_name'])) {

            // Original image processing
            $imgData =addslashes (file_get_contents($_FILES['image_data']['tmp_name']));

            // Thumbnail generation
            if (generateThumbnail($_FILES['image_data']['tmp_name'], 100, 100, 90)) {
                $thumbData = addslashes (file_get_contents($_FILES['image_data']['tmp_name'] . '_thumb.jpg'));

                // Insert original and thumbnail images into the database
                $sql = "UPDATE users SET user_pic='".$imgData."', user_pic_small='".$thumbData."' WHERE>

This code uses generateThumbnail() to create a thumbnail with dimensions 100x100 and a quality of 90%. The thumbnail is then saved with a "_thumb.jpg" suffix.

By implementing these techniques, you can achieve both responsive thumbnail creation and preservation of original image quality.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3