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.
imagecopyresampled(
$destination,
$source,
$dst_x, $dst_y,
$src_x, $src_y,
$dst_width, $dst_height,
$src_width, $src_height
) : bool
Parameter | Description |
---|---|
$destination | The destination image resource. |
$source | The source image resource. |
$dst_x | X-coordinate where the image will be placed in the destination. |
$dst_y | Y-coordinate where the image will be placed in the destination. |
$src_x | X-coordinate of the source image where cropping starts. |
$src_y | Y-coordinate of the source image where cropping starts. |
$dst_width | Width of the resized image. |
$dst_height | Height of the resized image. |
$src_width | Width of the original source image. |
$src_height | Height of the original source image. |
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);
?>
Function | Quality | Performance |
---|---|---|
imagecopyresized() | Low-quality scaling (pixelated) | Faster |
imagecopyresampled() | High-quality scaling (smooth) | Slightly slower |
✅ Use imagecopyresampled() for better image quality when resizing!
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 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.
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);
?>
imagecreatetruecolor()
for better quality.imagecopyresampled()
for smooth resizing.imagedestroy()
to release memory.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 FunctionsAuthor
🎥 Join me live on YouTubePassionate 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.