There are two kinds of times when the server generates thumbnails:
1. Generation when uploading files
Advantages: When uploading, the required thumbnails have been generated, and no further judgment is needed when reading, thus reducing the cpu operation.
Disadvantage: When the size of thumbnails changes or new size is added, all thumbnails need to be regenerated.
2. Access-time generation
Advantages: 1. Generation is needed when there is user access. Generation is not needed when there is no access, which saves space.
2. When you modify the size of thumbnails, you only need to modify the settings, and you don't need to regenerate all thumbnails.
Disadvantage: High concurrent access consumes server resources when thumbnails do not exist to be generated.
Although access generation has high concurrency problems, other advantages are better than the first method, so as long as the problem of high concurrency can be solved.
On how to automatically generate thumbnails based on url, you can refer to the principle and implementation I wrote before. "php automatically generates thumbnails based on url".
High concurrency processing principle:
1. When judging the need to generate images, create a temporary tag file in the tmp / directory. The name of the file is named md5 (the name of the file that needs to be generated), and delete the temporary file after processing.
2. When judging that the file to be generated has a temporary tag file in the tmp/directory, indicating that the file is being processed, the method of generating thumbnails is not invoked and waited until the temporary tag file is deleted and the successful output is generated.
The revised document is as follows, 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'; //Log files
- $source_path = WWW_PATH.'/upload/'; //The original path
- $dest_path = WWW_PATH.'/supload/'; //Target path
- $path = isset($_GET['path'])? $_GET['path'] : ''; //Accessed picture URL s
- //Check path
- if(!$path){
- exit();
- }
- //Get the picture 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 drawing document
- $source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);
- //Object document
- $dest = $dest_path.$relative_url;
- if(!file_exists($source)){ //The original map does not exist
- exit();
- }
- //High concurrency processing
- $processing_flag = '/tmp/thumb_'.md5($dest); //Used to determine whether a document is being processed
- $is_wait = 0; //Need to wait
- $wait_timeout = 5; //Waiting overtime
- 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){ //Overtime
- exit();
- }
- usleep(300000); // sleep 300 ms
- }
- if(file_exists($dest)){ //Successful picture generation
- ob_clean();
- header('content-type:'.mime_content_type($dest));
- exit(file_get_contents($dest));
- }else{
- exit(); //Generation Failure Exit
- }
- }
- //Create thumbnails
- $obj = new PicThumb($logfile);
- $obj->set_config($config);
- $create_flag = $obj->create_thumb($source, $dest);
- unlink($processing_flag); //Delete tagged files in processing
- if($create_flag){ //Determine whether the generation is successful
- ob_clean();
- header('content-type:'.mime_content_type($dest));
- exit(file_get_contents($dest));
- }
- ?>
<?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'; // log file $source_path = WWW_PATH.'/upload/'; // Original Path $dest_path = WWW_PATH.'/supload/'; // Target path $path = isset($_GET['path'])? $_GET['path'] : ''; // Accessed picture URL s // Check path if(!$path){ exit(); } // Get the picture 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 Drawing Document $source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url); // Target file $dest = $dest_path.$relative_url; if(!file_exists($source)){ // The original map does not exist exit(); } // High concurrency processing $processing_flag = '/tmp/thumb_'.md5($dest); // Used to determine whether a file is being processed $is_wait = 0; // Do you need to wait? $wait_timeout = 5; // Waiting overtime 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){ // overtime exit(); } usleep(300000); // sleep 300 ms } if(file_exists($dest)){ // Picture Generation Successful ob_clean(); header('content-type:'.mime_content_type($dest)); exit(file_get_contents($dest)); }else{ exit(); // Generation Failure Exit } } // Create Thumbnail $obj = new PicThumb($logfile); $obj->set_config($config); $create_flag = $obj->create_thumb($source, $dest); unlink($processing_flag); // Delete tagged files in processing if($create_flag){ // Determine whether the generation is successful ob_clean(); header('content-type:'.mime_content_type($dest)); exit(file_get_contents($dest)); } ?>
Source download address: Click View