Current Location: Home> Latest Articles> Combine imageflip() with imagecopyresampled() to create thumbnails

Combine imageflip() with imagecopyresampled() to create thumbnails

M66 2025-05-31

When working with image thumbnails, we often need not only to reduce the image size, but also to flip the image (such as horizontal or vertical) to meet the needs of certain visual effects. PHP provides two very practical functions: imagecopyresampled() and imageflip() , which are used for image scaling and image flipping respectively. If you want to make a flipped and high-quality thumbnail, these two functions work perfectly.

1. Understand the two core functions

imagecopyresampled()

This function is used to copy and scale a part of the image to another image, supporting high-quality image scaling processing. Compared with imagecopyresized() , its effect is more delicate and suitable for generating thumbnails.

imageflip()

The imageflip() function is used to flip an image, supporting horizontal, vertical and horizontal + vertical flip at the same time. This function has been introduced since PHP 5.5.

2. Practical example: Create flipped high-quality thumbnails

Here is a complete example code that scales an original image to generate a thumbnail, and then flips the thumbnail horizontally:

 <?php
// Original image path
$sourcePath = 'https://m66.net/uploads/sample.jpg';

// Get image information
list($width, $height, $type) = getimagesize($sourcePath);

// Create image resources
switch ($type) {
    case IMAGETYPE_JPEG:
        $sourceImage = imagecreatefromjpeg($sourcePath);
        break;
    case IMAGETYPE_PNG:
        $sourceImage = imagecreatefrompng($sourcePath);
        break;
    case IMAGETYPE_GIF:
        $sourceImage = imagecreatefromgif($sourcePath);
        break;
    default:
        die('Unsupported image types');
}

// Thumbnail size
$newWidth = 150;
$newHeight = intval($height * $newWidth / $width);

// Create a target image resource
$thumbImage = imagecreatetruecolor($newWidth, $newHeight);

// High-quality scaling
imagecopyresampled($thumbImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

// Flip thumbnails(Horizontal flip)
imageflip($thumbImage, IMG_FLIP_HORIZONTAL);

// Output to browser or save
header('Content-Type: image/jpeg');
imagejpeg($thumbImage);

// Destroy resources
imagedestroy($sourceImage);
imagedestroy($thumbImage);
?>

3. Some things to note

  • Before using imageflip() , make sure your PHP version is not lower than 5.5.

  • In order to maintain the image proportion, it is recommended to calculate the thumbnail size according to the aspect ratio of the original image.

  • If you are processing PNG images, remember to deal with transparent backgrounds (such as using imagealphableending() and imagesavealpha() ).

4. Practical application scenarios

This processing method is very common in e-commerce platforms, galleries, content management systems and other scenarios, especially when you need to generate image previews and add visual effects (such as mirrors), the combination of imageflip() and imagecopyresampled() can greatly improve development efficiency and image quality.

Through this article's explanation, you can easily grasp it