PHP automatically generates thumbnails based on URLs and handles high concurrency problems

Source: Internet
Author: User
Tags config md5 relative require

  Server to generate thumbnails of the time is generally divided into two types: upload files when generated, access generation, the following for you to introduce PHP based on the URL automatically generate thumbnails and handle high concurrency problems

The time when the server generates thumbnails is generally divided into two kinds of:    1. Generate     Advantages when uploading files: when uploading, it is necessary to generate thumbnails that need to be read without further judgment and CPU operation.     Disadvantage: All thumbnails need to be regenerated when the thumbnail size changes or when new dimensions are added.     2. Generate     Benefits at access time: 1. When there is a user access only need to generate, no access to generate, save space.     2. When you modify the thumbnail size, you only need to modify the settings without rebuilding all the thumbnails.     Disadvantage: High concurrent access consumes server resources when there is no need to build a thumbnail image.     Although generation can have high concurrency problems while accessing, other benefits are better than the first method, so it is only possible to resolve high concurrency problems.     about how to automatically generate thumbnails based on the principle and implementation of the URL, you can refer to my previous written "PHP based on the URL automatically generated thumbnails."     High concurrent processing principle:    1. When you decide that you want to generate a picture, create a temporary tag file in the tmp/directory, name it with MD5 (the file name you want to generate), and then delete the temporary file when you finish processing.     2. When you determine that the file to be generated has a temporary tag file in the tmp/directory that indicates that the file is being processed, the build thumbnail method is not invoked and waits until the temporary tag file is deleted to generate a successful output.     Modify the file as follows, others as before.     createthumb.php  code is as follows: <?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 ']: '; Visit the pictures url   //Check path  if (! $path) {  exit (); }   //Get picture uri  $relative _url = s Tr_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 document   $source = Str_replace ('/'. $type. /', '/'. $config [' Fromdir ']. ' /', $source _path. $relative _url);   //target file   $dest = $dest _path. $relative _url;    if (!file_ Exists ($source)) {//original image does not exist   exit (); }   //high concurrent processing   $processing _flag = '/tmp/thumb_ '. MD5 ($dest) ; Used to determine whether the file is 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 build   while (file_exists ($processing _flag)) {  if (time)-$ Starttime> $wait _timeout) {//timeout   exit (); }  usleep (300000); Sleep ms }    if (file_exists ($dest)) {//Picture Build Success   Ob_clean ();  header (' Content-type: '). Mime_content_type ($dest));  exit (file_get_contents ($dest)); }else{  exit ();//Build Failure exit  } }    //create thumbnails   $obj = new Picthumb ($logfile);  $obj->set_config ($config);  $create _flag = $obj ->create_thumb ($source, $dest);    unlink ($processing _flag); Delete a tag file     if ($create _flag) {//Determine whether to generate success   Ob_clean ();  header (' Content-type: '. Mime_content_ Type ($dest));  exit (file_get_contents ($dest)); }   ?>   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.