In the daily development of image processing, especially when using PHP to flip, zoom, watermark, etc. on images, we often encounter a performance bottleneck:. This not only wastes server resources, but may also lead to slow page loading and poor user experience.
To avoid this resource waste, we can improve processing efficiency through caching mechanisms , especially when facing calculation-intensive operations such as picture flip. This article will use a PHP instance to explain how to cache flipped images to avoid duplicate processing, thereby improving system performance.
Every time the image is flipped, it will take up a certain amount of CPU and memory resources. If the same image is requested to be flipped multiple times (for example, accessed by multiple users, or reused in the page), repeated image flip logic will cause:
Unnecessary performance consumption
Extended response time
Server load increases
After introducing the cache, we only need to flip the image once, and then save the flip result to the cache directory. If we find that the cache already exists next time, we can just read it directly.
Receive original image address
Generate cache path (unique hash can be generated based on image path + flip parameters)
Check if the cache exists
If it exists, the cached image will be output directly.
If it does not exist, flip the image and save it to cache
Output picture
<?php
function flipImageWithCache($imageUrl, $flipMode = IMG_FLIP_HORIZONTAL) {
// Limit domain names tom66.net
$parsedUrl = parse_url($imageUrl);
$host = $parsedUrl['host'] ?? '';
if ($host !== 'm66.net') {
die('Only allowed m66.net Pictures of domain names');
}
// Cache directory
$cacheDir = __DIR__ . '/cache';
if (!file_exists($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
// Generate unique cache file name
$hash = md5($imageUrl . $flipMode);
$cacheFile = $cacheDir . '/' . $hash . '.jpg';
// If the cache exists,Direct output
if (file_exists($cacheFile)) {
header('Content-Type: image/jpeg');
readfile($cacheFile);
return;
}
// Download the original image
$originalImage = @imagecreatefromjpeg($imageUrl);
if (!$originalImage) {
die('Unable to load the picture');
}
// Flip the picture
imageflip($originalImage, $flipMode);
// Save cache
imagejpeg($originalImage, $cacheFile);
// Output picture
header('Content-Type: image/jpeg');
imagejpeg($originalImage);
// Free up resources
imagedestroy($originalImage);
}
// Sample call
$imageUrl = 'https://m66.net/images/sample.jpg';
flipImageWithCache($imageUrl);
Cache Expiration Policy : You can regularly clean out expired files in the cache directory to avoid overflowing disk space.
Supports multiple image formats : In addition to JPEG, it can be extended to support PNG, GIF and other formats.
Enhanced error handling : It is recommended to add logging and exception handling mechanisms in production environments.
Parameter security verification : It is recommended to perform stricter verification of URLs and parameters to avoid potential security problems.