imagecopyresampled() Function in PHP GD

The imagecopyresampled() function is used to resize and copy an image smoothly while maintaining high quality. It is an improved version of imagecopyresized(), providing better anti-aliasing and smoother image scaling.

Syntax

imagecopyresampled(
    $destination, 
    $source, 
    $dst_x, $dst_y, 
    $src_x, $src_y, 
    $dst_width, $dst_height, 
    $src_width, $src_height
) : bool

Parameters

ParameterDescription
$destinationThe destination image resource.
$sourceThe source image resource.
$dst_xX-coordinate where the image will be placed in the destination.
$dst_yY-coordinate where the image will be placed in the destination.
$src_xX-coordinate of the source image where cropping starts.
$src_yY-coordinate of the source image where cropping starts.
$dst_widthWidth of the resized image.
$dst_heightHeight of the resized image.
$src_widthWidth of the original source image.
$src_heightHeight of the original source image.

Returns

  • true on success
  • false on failure

Example: Resize an Image with imagecopyresampled()

The following example resizes an image while maintaining high quality.

<?php
// Load the source image
$source = imagecreatefromjpeg('original.jpg');

// Get original dimensions
$src_width = imagesx($source);
$src_height = imagesy($source);

// Define new width and height
$new_width = 300;
$new_height = ($src_height / $src_width) * $new_width;

// Create a new blank image
$destination = imagecreatetruecolor($new_width, $new_height);

// Resize and copy the image
imagecopyresampled(
    $destination, $source, 
    0, 0, 0, 0, 
    $new_width, $new_height, 
    $src_width, $src_height
);

// Output the resized image
header("Content-Type: image/jpeg");
imagejpeg($destination);

// Free memory
imagedestroy($source);
imagedestroy($destination);
?>

Key Differences Between imagecopyresized() and imagecopyresampled()

FunctionQualityPerformance
imagecopyresized()Low-quality scaling (pixelated)Faster
imagecopyresampled()High-quality scaling (smooth)Slightly slower

Use imagecopyresampled() for better image quality when resizing!

When to Use imagecopyresampled()?

  • Creating thumbnails for image galleries.
  • Resizing images before saving to reduce file size.
  • Generating dynamic images for web applications.
  • Applying watermarks to images.

This method ensures that **images maintain their quality** while being resized, making it **ideal for web applications** where optimized images improve **loading speed and performance**.

Optimizing Images for Web Use with PHP GD

Why Optimize Images for the Web?

Optimizing images helps improve website performance by reducing file sizes without losing visual quality. Smaller images load faster, consume less bandwidth, and boost SEO rankings. In this tutorial, we will use PHP GD to dynamically optimize images before displaying them on the web.

Key Strategies for Image Optimization

  • Resizing Images: Reduce dimensions to match display requirements.
  • Compression: Adjust quality settings while keeping clarity.
  • Choosing the Right Format: Use JPEG for photos, PNG for transparency, and WebP for the best compression.
  • Removing Unnecessary Data: Crop images to focus on important content.

PHP GD Code: Resize and Optimize an Image

The following script resizes a JPEG image and compresses it before saving it.

<?php
function optimize_image($source_file, $destination_file, $new_width) {
    // Get original image dimensions
    $image_info = getimagesize($source_file);
    $width = $image_info[0];
    $height = $image_info[1];

    // Calculate new height to maintain aspect ratio
    $new_height = ($height / $width) * $new_width;

    // Create a new true color image
    $new_image = imagecreatetruecolor($new_width, $new_height);

    // Load the source image
    $source_image = imagecreatefromjpeg($source_file);

    // Resize the image
    imagecopyresampled($new_image, $source_image, 0, 0, 0, 0, 
                        $new_width, $new_height, 
                        $width, $height);

    // Save optimized image with compression
    imagejpeg($new_image, $destination_file, 75);

    // Free memory
    imagedestroy($new_image);
    imagedestroy($source_image);
}

// Example Usage
$source = 'original.jpg';
$destination = 'optimized.jpg';
$width = 800;

optimize_image($source, $destination, $width);
?>

Code Explanation

  • Get Image Dimensions: Retrieves the width and height of the source image.
  • Calculate New Height: Maintains aspect ratio while resizing.
  • Create a New Image: Uses imagecreatetruecolor() for better quality.
  • Resize and Copy Image: Uses imagecopyresampled() for smooth resizing.
  • Save with Compression: Stores the image with 75% quality (adjustable).
  • Free Memory: Calls imagedestroy() to release memory.

Benefits of This Approach

  • Speeds Up Website Load Time: Smaller images load faster.
  • Reduces Bandwidth Usage: Saves server resources.
  • Boosts SEO Rankings: Google favors fast-loading websites.
  • Enhances User Experience: Faster sites lead to better engagement.

Final Thoughts

With PHP GD, you can automatically resize and compress images for optimal performance and efficiency. This technique ensures that images are properly scaled, compressed, and delivered in the best format for the web.

Back to GD Functions
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer