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

PHP imagecopyresampled() And imagecopyresized(): Image Compression

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.

  1. Image compression in PHP with imagecopyresampled():

    • Use the 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);
    
  2. Resizing and resampling images in PHP:

    • Adjust the dimensions using imagecopyresampled() for resizing.
    <?php
    // Same code as the previous example
    
  3. Using imagecopyresampled() for high-quality image scaling:

    • imagecopyresampled() provides high-quality scaling by resampling.
    <?php
    // Same code as the first example
    
  4. PHP imagecopyresized() vs. imagecopyresampled():

    • Compare 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);
    
  5. Maintaining aspect ratio during image compression in PHP:

    • Preserve the aspect ratio when resizing an image.
    <?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);
    
  6. Quality settings for imagecopyresampled() in PHP:

    • Adjust the quality setting when saving the compressed image.
    <?php
    // Same code as the first example
    
  7. Handling transparency and alpha channels in PHP image compression:

    • Consider transparency and alpha channels when working with PNG images.
    <?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);
    
  8. Examples of image compression using PHP GD library:

    • Additional examples of image compression.
    <?php
    // Various examples from above