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

Source: Internet
Author: User
Tags config md5 require

The timing for the server to generate thumbnails is generally divided into two types:

1. Generate when uploading files

Advantages: The required thumbnails have been generated at the time of uploading, and no need to judge when reading, reducing the cpu operation.

Disadvantages: When the thumbnail size changes or when you add a new size, you need to regenerate all the thumbnails.

2. Generated on access

advantage:

1. It needs to be generated when there is a user access, and it does not need to be generated without access, saving space.

2. When modifying the thumbnail size, you only need to modify the settings without having to regenerate all the thumbnails.

Disadvantages: High concurrent access can be very resource intensive when thumbnails do not exist and need to be generated.

Although there are high concurrency issues during access generation, other advantages are better than the first method, so you can solve high concurrency problems.

For the principle and implementation of how to automatically generate thumbnails based on url, you can refer to the "PHP automatically generates thumbnails based on url".

High concurrency processing principle:

1. When judging that a picture needs to be generated, create a temporary markup file in the tmp/ directory. The file name is named with md5 (the name of the file to be generated), and the temporary file is deleted after the process ends.

2. When it is judged that the file to be generated has a temporary mark file in the tmp/ directory, indicating that the file is being processed, the method of generating a thumbnail is not called, and waits until the temporary mark file is deleted, and the generated output is successful.

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/'; // original path
$dest_path = WWW_PATH.'/supload/'; // target path

$path = isset($_GET['path'])? $_GET['path'] : ''; // Visited image URL

// check path
If(!$path){
    Exit();
}

// Get the image 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 image file
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);

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

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

// High concurrent processing
$processing_flag = '/tmp/thumb_'.md5($dest); // Used to determine if the file is being processed
$is_wait = 0; // Do you need 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){ // need to wait for generation
    While(file_exists($processing_flag)){
        If(time()-$starttime>$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(); // Generate failed exit
    }
}

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

Unlink($processing_flag); // delete the processing tag file

If($create_flag){ // Determine if the build was successful
    Ob_clean();
    Header('content-type:'.mime_content_type($dest));
    Exit(file_get_contents($dest));
}

?>


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.