PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
imagecopyresampled()
and imagecopyresized()
are PHP functions used to copy and resize part of an image.
Here is a brief comparison:
imagecopyresized()
: This function copies a rectangular part of one image to another image, resizing it in the process. It does not maintain the aspect ratio and does not provide the best quality because it uses a simple algorithm.imagecopyresampled()
: This function is similar to imagecopyresized()
but uses a more sophisticated algorithm. It maintains the aspect ratio and provides a higher quality output, but it's slower.Here's an example of how you can use both of these functions:
<?php // Load the original image $originalImage = imagecreatefromjpeg('original.jpg'); // Get the original image dimensions $originalWidth = imagesx($originalImage); $originalHeight = imagesy($originalImage); // Define the new image dimensions $newWidth = 200; $newHeight = 200; // Create new image with new dimensions $newImageResized = imagecreatetruecolor($newWidth, $newHeight); $newImageResampled = imagecreatetruecolor($newWidth, $newHeight); // Resizing and resampling the original image imagecopyresized($newImageResized, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); imagecopyresampled($newImageResampled, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save the resized and resampled images imagejpeg($newImageResized, 'resized.jpg'); imagejpeg($newImageResampled, 'resampled.jpg'); // Free up memory imagedestroy($originalImage); imagedestroy($newImageResized); imagedestroy($newImageResampled); ?>
In the above script, we first load the original image using imagecreatefromjpeg()
, which creates an image resource from a JPEG file. We then get the dimensions of the original image with imagesx()
and imagesy()
.
Next, we define the new dimensions and create new blank images with those dimensions using imagecreatetruecolor()
.
Finally, we resize and resample the original image into the new images using imagecopyresized()
and imagecopyresampled()
, respectively, and save the new images with imagejpeg()
.
We then free up the memory by destroying the image resources with imagedestroy()
. The result is two new images: one resized and one resampled.
Image compression in PHP with imagecopyresampled()
:
imagecopyresampled()
function to compress and resize images.<?php $sourceImage = imagecreatefromjpeg('input.jpg'); $targetWidth = 300; $targetHeight = 200; $targetImage = imagecreatetruecolor($targetWidth, $targetHeight); imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, imagesx($sourceImage), imagesy($sourceImage)); header('Content-Type: image/jpeg'); imagejpeg($targetImage, 'compressed_output.jpg', 80); // Save to file with quality setting imagedestroy($sourceImage); imagedestroy($targetImage);
Resizing and resampling images in PHP:
imagecopyresampled()
for resizing.<?php // Same code as the previous example
Using imagecopyresampled()
for high-quality image scaling:
imagecopyresampled()
provides high-quality scaling by resampling.<?php // Same code as the first example
PHP imagecopyresized()
vs. imagecopyresampled()
:
imagecopyresized()
and imagecopyresampled()
for image scaling.<?php $sourceImage = imagecreatefromjpeg('input.jpg'); $targetWidth = 300; $targetHeight = 200; $targetImageResized = imagecreatetruecolor($targetWidth, $targetHeight); $targetImageResampled = imagecreatetruecolor($targetWidth, $targetHeight); // Using imagecopyresized() imagecopyresized($targetImageResized, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, imagesx($sourceImage), imagesy($sourceImage)); // Using imagecopyresampled() imagecopyresampled($targetImageResampled, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, imagesx($sourceImage), imagesy($sourceImage)); // Save or output the images header('Content-Type: image/jpeg'); imagejpeg($targetImageResized, 'resized_output.jpg', 80); imagejpeg($targetImageResampled, 'resampled_output.jpg', 80); imagedestroy($sourceImage); imagedestroy($targetImageResized); imagedestroy($targetImageResampled);
Maintaining aspect ratio during image compression in PHP:
<?php $sourceImage = imagecreatefromjpeg('input.jpg'); $targetWidth = 300; // Set the desired width $aspectRatio = imagesx($sourceImage) / imagesy($sourceImage); $targetHeight = $targetWidth / $aspectRatio; $targetImage = imagecreatetruecolor($targetWidth, $targetHeight); imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, imagesx($sourceImage), imagesy($sourceImage)); header('Content-Type: image/jpeg'); imagejpeg($targetImage, 'compressed_output_aspect_ratio.jpg', 80); imagedestroy($sourceImage); imagedestroy($targetImage);
Quality settings for imagecopyresampled()
in PHP:
<?php // Same code as the first example
Handling transparency and alpha channels in PHP image compression:
<?php $sourceImage = imagecreatefrompng('input.png'); $targetWidth = 300; $targetHeight = 200; $targetImage = imagecreatetruecolor($targetWidth, $targetHeight); // Preserve alpha channel imagealphablending($targetImage, false); imagesavealpha($targetImage, true); imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, imagesx($sourceImage), imagesy($sourceImage)); header('Content-Type: image/png'); imagepng($targetImage, 'compressed_output_alpha.png'); imagedestroy($sourceImage); imagedestroy($targetImage);
Examples of image compression using PHP GD library:
<?php // Various examples from above