Php automatically generates thumbnails Based on URLs and handles high concurrency issues

Source: Internet
Author: User

The server generally has two types of thumbnails:

1. generated during File Upload

Advantage: the required thumbnails have been generated during the upload, and no judgment is required during reading, reducing the cpu operation.

Disadvantage: When the thumbnail size is changed or the new size is added, all the thumbnails need to be regenerated.

2. generated during access

Advantages: 1. users need to generate access only when they have access, and do not need to generate access that has no access, saving space.

2. When you modify the thumbnail size, you only need to modify the settings and do not need to regenerate all the thumbnails.

Disadvantage: When the thumbnails do not exist and need to be generated, high-concurrency access will consume a lot of server resources.

Although there is a high concurrency problem generated during access, other advantages are better than the first method, so you only need to solve the high concurrency problem.

For more information about how to automatically generate thumbnails Based on URLs, see "automatically generate thumbnails Based on URLs in php".

High concurrency processing principle:

1. When determining that an image needs to be generated, create a temporary Tag file in the tmp/directory. The file name should be named by md5 (the file name to be generated). After processing, delete the temporary file.

2. when you determine that the file to be generated has a temporary Tag file in the tmp/directory, indicating that the file is being processed, wait until the temporary Tag file is deleted without calling the generate thumbnail method, generate successful output.

The modified file is as follows.

Createthumb. php
Copy codeThe Code is as follows:
<? Php
Define ('www _ path', 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']: ''; // URL of the accessed Image

// Check the path
If (! $ Path ){
Exit ();
}

// Obtain the image URI
$ Relative_url = str_replace ($ dest_path, '', WWW_PATH. $ path );

// Obtain 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 ();
}

// Source Image File
$ Source = str_replace ('/'. $ type. '/', '/'. $ config ['fromdir']. '/', $ source_path. $ relative_url );

// Target file
$ Dest = $ dest_path. $ relative_url;

If (! File_exists ($ source) {// The source image does not exist
Exit ();
}

// High concurrency Processing
$ Processing_flag = '/tmp/thumb _'. md5 ($ dest); // used to determine whether the file is being processed
$ Is_wait = 0; // whether to wait
$ Wait_timeout = 5; // wait for timeout

If (! File_exists ($ processing_flag )){
File_put_contents ($ processing_flag, 1, true );
} Else {
$ Is_wait = 1;
}

If ($ is_wait) {// wait for Generation
While (file_exists ($ processing_flag )){
If (time ()-$ starttime> $ wait_timeout) {// timeout
Exit ();
}
Usleep (300000); // sleep 300 MS
}

If (file_exists ($ dest) {// The image is generated successfully.
Ob_clean ();
Header ('content-type: '. mime_content_type ($ dest ));
Exit (file_get_contents ($ dest ));
} Else {
Exit (); // generate failed to exit
}
}

// Create a thumbnail
$ Obj = new PicThumb ($ logfile );
$ Obj-> set_config ($ config );
$ Create_flag = $ obj-> create_thumb ($ source, $ dest );

Unlink ($ processing_flag); // Delete the marked File

If ($ create_flag) {// determine whether the generation is successful
Ob_clean ();
Header ('content-type: '. mime_content_type ($ dest ));
Exit (file_get_contents ($ dest ));
}

?>

Source code: Click to view

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.