Home > Backend Development > PHP Tutorial > PHP automatically generates thumbnails based on URLs and handles high concurrency issues_PHP tutorial

PHP automatically generates thumbnails based on URLs and handles high concurrency issues_PHP tutorial

WBOY
Release: 2016-07-13 10:40:13
Original
739 people have browsed it

There are generally two types of timing for the server to generate thumbnails: generated when uploading files and generated when accessing. Here is an introduction to how PHP automatically generates thumbnails based on URLs and handles high concurrency issues

The timing for the server to generate thumbnails is generally divided into two types: ​ 1. Generated when uploading files ​ Advantages: The required thumbnails are already generated when uploading, and there is no need to judge when reading, reducing CPU operations.​ ​ Disadvantage: When the thumbnail size changes or a new size is added, all thumbnails need to be regenerated.​ ​ 2. Generated during access ​ Advantages: 1. It only needs to be generated when a user accesses it. If there is no access, it does not need to be generated, which saves 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 problems when accessing, other advantages are better than the first method, so as long as the high concurrency problems are solved.​ ​ Regarding 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. The file name is named md5 (the file name to be generated), and the temporary file is deleted 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, which means that the file is being processed, the thumbnail generation method will not be called, but will wait until the temporary mark file is deleted and the successful output is generated.​ ​ The modified files are as follows, others are the same as before.​ ​ createthumb.php The code is as follows: $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)); } ​ ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/727563.htmlTechArticleThe timing when the server generates thumbnails is generally divided into two types: generated when uploading files and generated when accessing. The following is for everyone Introducing the service that PHP automatically generates thumbnails based on URLs and handles high concurrency issues...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template