There are generally two types of timing for the server to generate thumbnails:
1. Generated when uploading files
Advantages: The required thumbnails are already generated when uploading, and no further judgment is required when reading. Reduce cpu operations.
Disadvantage: When the thumbnail size changes or a new size is added, all thumbnails need to be regenerated.
2. Generate when accessed
Advantages: 1. It only needs to be generated when a user accesses it, and does not need to be generated when there is no access, saving space.
2. When modifying the thumbnail size, you only need to modify the settings without regenerating all thumbnails.
Disadvantages: When thumbnails do not exist and need to be generated, high concurrent access will consume a lot of server resources.
Although there will be high concurrency issues when accessing, other advantages are better than the first method, so you only need to solve the high concurrency issues.
For the principle and implementation of how to automatically generate thumbnails based on URLs, you can refer to the "php automatically generates thumbnails based on URLs" I wrote before.
High concurrency processing principle:
1. When it is determined that a picture needs to be generated, create a temporary mark file in the tmp/ directory, and name the file with md5 (the file name to be generated). Delete the temporary files after the processing is completed.
2. When it is judged that the file to be generated has a temporary mark file in the tmp/ directory, indicating that the file is being processed, the generate thumbnail method will not be called, but will wait until the temporary mark file is deleted and the successful output will be generated. .
The modified files are as follows, others are the same as before.
createthumb.php
Copy code The code is as follows:
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'; // Log file
$source_path = WWW_PATH.'/upload /'; // Original path
$dest_path = WWW_PATH.'/supload/'; // Destination path
$path = isset($_GET['path'])? $_GET['path '] : ''; // Accessed image URL
// Check path
if(!$path){
exit();
}
/ / Get image URI
$relative_url = str_replace($dest_path, '', WWW_PATH.$path);
// Get type
$type = substr($relative_url, 0, strpos($ relative_url, '/'));
// Get config
$config = isset($thumb_config[$type])? $thumb_config[$type] : '';
// Check config
if(!$config || !isset($config['fromdir'])){
exit();
}
// Original image file
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);
// target File
$dest = $dest_path.$relative_url;
if(!file_exists($source)){ // The original image does not exist
exit();
}
// High concurrency processing
$processing_flag = '/tmp/thumb_'.md5($dest); // Used to determine whether the file is being processed
$is_wait = 0; // Whether 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){ // Need to wait for generation
while(file_exists($processing_flag)){
if(time()-$starttime>$ wait_timeout){ // timeout
exit();
}
usleep(300000); // sleep 300 ms
}
if(file_exists($dest)){ / / Image generated successfully
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest));
}else{
exit(); // Exit if generation fails
}
}
// Create thumbnail
$obj = new PicThumb($logfile);
$obj-> set_config($config);
$create_flag = $obj->create_thumb($source, $dest);
unlink($processing_flag); // Delete the processing flag file
if($create_flag){ // Determine whether the generation is successful
ob_clean();
header('content-type:'.mime_content_type($dest));
exit(file_get_contents($dest)) ;
}
?>
Source code download address: Click to view
http://www.bkjia.com/PHPjc/726022.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/726022.htmlTechArticleThe timing when the server generates thumbnails is generally divided into two types: 1. Generating when uploading files Advantages: Already when uploading Generate the required thumbnails, no need to judge when reading, and reduce CPU operations. ...