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

Source: Internet
Author: User
Tags exit config header log md5 php define relative require

The timing of server generation thumbnails is generally divided into two categories:

1. Generate when uploading files

Advantages: The upload has generated the required thumbnail, read without any need to judge, reduce CPU operations.

Disadvantage: All thumbnails need to be regenerated when the thumbnail size changes or when new dimensions are added.

2. Generate when accessing

Advantages: 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.

While there is a high concurrency problem when you access the build, other benefits are better than the first, so you can only solve the high concurrency problem.

on how to automatically generate thumbnails based on the principle of the URL and implementation, 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 after the processing is complete.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

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.

The modified files are as follows, others are the same 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 file $source _path = Www_path. '      /upload/'; The original path $dest _path = Www_path. '       /supload/'; Target path $path = isset ($_get[' path ')?     $_get[' path ']: ';  
Access to the picture URL//check path if (! $path) {exit ();  
      
//Get picture uri $relative _url = str_replace ($dest _path, ", Www_path. $path);  
      
Gets the 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 file $source = Str_replace ('/'. $type. /', '/'. $config [' Fromdir ']. '  
      
/', $source _path. $relative _url); Target file $dest = $dest _path. $rElative_url;  
if (!file_exists ($source)) {//original artwork 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 _tim  
        Eout) {//timeout exit (); } usleep (300000);  
        Sleep Ms} if (File_exists ($dest)) {//Picture generated successfully ob_clean ();  
        Header (' Content-type: ' Mime_content_type ($dest));  
    Exit (file_get_contents ($dest));  
}else{exit ();//Build failed Exit}//create thumbnail $obj = new Picthumb ($logfile);  
$obj->set_config ($config);  
      
$create _flag = $obj->create_thumb ($source, $dest); Unlink ($proCessing_flag);  
    Delete the tag file if ($create _flag) {//Judge whether to generate success Ob_clean ();  
    Header (' Content-type: ' Mime_content_type ($dest));  
Exit (file_get_contents ($dest)); }?>

SOURCE Download Address: http://download.csdn.net/detail/fdipzone/6809757

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.