php automatically generates thumbnails from url and handles high concurrency issues
- 2020-12-16 05:53:43
- OfStack
There are generally two types of opportunities for the server to generate thumbnails:
1. Generated when uploading files
Advantages: The required thumbnails are generated when uploading, no judgment is needed when reading, and the cpu operation is reduced.
Cons: When the thumbnail size changes or is added, you need to regenerate all the thumbnails.
2. Generated during access
Advantages: 1. It needs to be generated only when there is user access. It needs not to be generated if there is no access, saving space.
2. When modifying the size of thumbnails, only the Settings need to be changed, and there is no need to regenerate all the thumbnails.
Cons: High concurrent access can consume server resources when thumbnails do not exist and need to be generated.
While there are high concurrency issues with access time generation, the other advantages are better than the first approach, so you just have to solve the high concurrency problem.
For the principle and implementation of how to automatically generate thumbnails based on url, please refer to my previous article "php Automatically generates thumbnails based on url".
Principle of high concurrency processing:
1. When the image needs to be generated, create a temporary tag file in tmp/ directory, name the file name with md5 (the file name to be generated), and delete the temporary file after processing.
2. When it is determined that the file to be generated has a temporary tag file in tmp/ directory, indicating that the file is being processed, the method of generating thumbnails is not called, but wait until the temporary tag file is deleted and the output is generated successfully.
The modified files are as follows, others are the same as before.
createthumb.php
Source download address: click to view
1. Generated when uploading files
Advantages: The required thumbnails are generated when uploading, no judgment is needed when reading, and the cpu operation is reduced.
Cons: When the thumbnail size changes or is added, you need to regenerate all the thumbnails.
2. Generated during access
Advantages: 1. It needs to be generated only when there is user access. It needs not to be generated if there is no access, saving space.
2. When modifying the size of thumbnails, only the Settings need to be changed, and there is no need to regenerate all the thumbnails.
Cons: High concurrent access can consume server resources when thumbnails do not exist and need to be generated.
While there are high concurrency issues with access time generation, the other advantages are better than the first approach, so you just have to solve the high concurrency problem.
For the principle and implementation of how to automatically generate thumbnails based on url, please refer to my previous article "php Automatically generates thumbnails based on url".
Principle of high concurrency processing:
1. When the image needs to be generated, create a temporary tag file in tmp/ directory, name the file name with md5 (the file name to be generated), and delete the temporary file after processing.
2. When it is determined that the file to be generated has a temporary tag file in tmp/ directory, indicating that the file is being processed, the method of generating thumbnails is not called, but wait until the temporary tag file is deleted and the output is generated successfully.
The modified files are as follows, others are the same as before.
createthumb.php
<?php
define('WWW_PATH', dirname(dirname(__FILE__))); // site www directory
require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php
require(WWW_PATH.'/ThumbConfig.php'); // include ThumbConfig.php
$logfile = WWW_PATH.'/createthumb.log'; // The log file
$source_path = WWW_PATH.'/upload/'; // The original path
$dest_path = WWW_PATH.'/supload/'; // The target path
$path = isset($_GET['path'])? $_GET['path'] : ''; // Images accessed URL
// check path
if(!$path){
exit();
}
// Get photo URI
$relative_url = str_replace($dest_path, '', WWW_PATH.$path);
// To obtain type
$type = substr($relative_url, 0, strpos($relative_url, '/'));
// To obtain config
$config = isset($thumb_config[$type])? $thumb_config[$type] : '';
// check config
if(!$config || !isset($config['fromdir'])){
exit();
}
// The original file
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);
// The target file
$dest = $dest_path.$relative_url;
if(!file_exists($source)){ // The original does not exist
exit();
}
// High concurrency processing
$processing_flag = '/tmp/thumb_'.md5($dest); // Used to determine if a file is in process
$is_wait = 0; // Do I need to wait
$wait_timeout = 5; // Wait timeout
if(!file_exists($processing_flag)){
file_put_contents($processing_flag, 1, true);
}else{
$is_wait = 1;
}
if($is_wait){ // We need to wait for the generation
while(file_exists($processing_flag)){
if(time()-$starttime>$wait_timeout){ // timeout
exit();
}
usleep(300000); // sleep 300 ms
}
if(file_exists($dest)){ // Successful image generation
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest));
}else{
exit(); // Generate failed exit
}
}
// Create thumbnails
$obj = new PicThumb($logfile);
$obj->set_config($config);
$create_flag = $obj->create_thumb($source, $dest);
unlink($processing_flag); // Delete the tag file in the process
if($create_flag){ // Determine if the build was successful
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest));
}
?>
Source download address: click to view