php automatically generates thumbnails based on url and handles high concurrency problems

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

  1. <?php  
  2. define('WWW_PATH', dirname(dirname(__FILE__))); //Site www directory  
  3.   
  4. require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php  
  5. require(WWW_PATH.'/ThumbConfig.php');    // include ThumbConfig.php  
  6.   
  7. $logfile = WWW_PATH.'/createthumb.log';  //Log files  
  8. $source_path = WWW_PATH.'/upload/';      //The original path  
  9. $dest_path = WWW_PATH.'/supload/';       //Target path  
  10.   
  11. $path = isset($_GET['path'])? $_GET['path'] : '';     //Accessed picture URL s  
  12.   
  13. //Check path  
  14. if(!$path){  
  15.     exit();  
  16. }  
  17.   
  18. //Get the picture URI  
  19. $relative_url = str_replace($dest_path'', WWW_PATH.$path);  
  20.   
  21. //Get type  
  22. $type = substr($relative_url, 0, strpos($relative_url'/'));  
  23.   
  24. //Get config  
  25. $config = isset($thumb_config[$type])? $thumb_config[$type] : '';  
  26.   
  27. //Check config  
  28. if(!$config || !isset($config['fromdir'])){  
  29.     exit();  
  30. }  
  31.   
  32. //Original drawing document  
  33. $source = str_replace('/'.$type.'/''/'.$config['fromdir'].'/'$source_path.$relative_url);  
  34.   
  35. //Object document  
  36. $dest = $dest_path.$relative_url;  
  37.   
  38. if(!file_exists($source)){ //The original map does not exist  
  39.     exit();  
  40. }  
  41.   
  42. //High concurrency processing  
  43. $processing_flag = '/tmp/thumb_'.md5($dest); //Used to determine whether a document is being processed  
  44. $is_wait = 0;                                //Need to wait  
  45. $wait_timeout = 5;                           //Waiting overtime  
  46.   
  47. if(!file_exists($processing_flag)){  
  48.     file_put_contents($processing_flag, 1, true);  
  49. }else{  
  50.     $is_wait = 1;  
  51. }  
  52.   
  53. if($is_wait){ //Need to wait for generation  
  54.     while(file_exists($processing_flag)){  
  55.         if(time()-$starttime>$wait_timeout){ //Overtime  
  56.             exit();  
  57.         }  
  58.         usleep(300000); // sleep 300 ms  
  59.     }  
  60.   
  61.     if(file_exists($dest)){ //Successful picture generation  
  62.         ob_clean();  
  63.         header('content-type:'.mime_content_type($dest));  
  64.         exit(file_get_contents($dest));  
  65.     }else{  
  66.         exit(); //Generation Failure Exit  
  67.     }  
  68. }  
  69.   
  70. //Create thumbnails  
  71. $obj = new PicThumb($logfile);  
  72. $obj->set_config($config);  
  73. $create_flag = $obj->create_thumb($source$dest);  
  74.   
  75. unlink($processing_flag); //Delete tagged files in processing  
  76.   
  77. if($create_flag){ //Determine whether the generation is successful  
  78.     ob_clean();  
  79.     header('content-type:'.mime_content_type($dest));  
  80.     exit(file_get_contents($dest));  
  81. }  
  82.   
  83. ?>  
<?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



Keywords: PHP

Added by cpetercarter on Fri, 21 Jun 2019 00:08:58 +0300