Home >Backend Development >PHP Tutorial >How to let PHP automatically generate thumbnails based on URLs and handle high concurrency issues

How to let PHP automatically generate thumbnails based on URLs and handle high concurrency issues

jacklove
jackloveOriginal
2018-06-09 14:18:081853browse

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

1. Generate when uploading files

Advantages: The required thumbnails are already generated when uploading, and there is no need to re-judge when reading, reducing CPU operations.

Disadvantages: When the thumbnail size changes or a new size is added, all thumbnails need to be regenerated.

2. Generate when accessing

#Advantages: 1. It only needs to be generated when there is a user accessing, no access No need to generate, saving 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 you only need to solve the high concurrency problems.

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 judged that a picture needs to be generated, Create a temporary mark file in the tmp/ directory, name the file with md5 (the file name that needs to be generated), and delete the temporary file after the processing is completed.

2. When it is determined that the file to be generated has a temporary mark file in the tmp/ directory, which means that the file is being processed, the generate thumbnail method will not be called, but will wait until the temporary mark file is deleted, generating a successful output.

The modified files are as follows, others are the same as before.

createthumb.php

<?php
define(&#39;WWW_PATH&#39;, dirname(dirname(__FILE__))); // 站点www目录
require(WWW_PATH.&#39;/PicThumb.class.php&#39;); // include PicThumb.class.php
require(WWW_PATH.&#39;/ThumbConfig.php&#39;);    // include ThumbConfig.php
$logfile = WWW_PATH.&#39;/createthumb.log&#39;;  // 日志文件
$source_path = WWW_PATH.&#39;/upload/&#39;;      // 原路径
$dest_path = WWW_PATH.&#39;/supload/&#39;;       // 目标路径
$path = isset($_GET[&#39;path&#39;])? $_GET[&#39;path&#39;] : &#39;&#39;;     // 访问的图片URL
// 检查path
if(!$path){
    exit();
}
// 获取图片URI
$relative_url = str_replace($dest_path, &#39;&#39;, WWW_PATH.$path);
// 获取type
$type = substr($relative_url, 0, strpos($relative_url, &#39;/&#39;));
// 获取config
$config = isset($thumb_config[$type])? $thumb_config[$type] : &#39;&#39;;
// 检查config
if(!$config || !isset($config[&#39;fromdir&#39;])){
    exit();
}
// 原图文件
$source = str_replace(&#39;/&#39;.$type.&#39;/&#39;, &#39;/&#39;.$config[&#39;fromdir&#39;].&#39;/&#39;, $source_path.$relative_url);
// 目标文件 
$dest = $dest_path.$relative_url;
if(!file_exists($source)){ // 原图不存在
    exit();
}
// 高并发处理
$processing_flag = &#39;/tmp/thumb_&#39;.md5($dest); // 用于判断文件是否处理中
$is_wait = 0;                                // 是否需要等待
$wait_timeout = 5;                           // 等待超时时间
if(!file_exists($processing_flag)){
    file_put_contents($processing_flag, 1, true);
}else{
    $is_wait = 1;
}
if($is_wait){ // 需要等待生成
    while(file_exists($processing_flag)){
        if(time()-$starttime>$wait_timeout){ // 超时
            exit();
        }
        usleep(300000); // sleep 300 ms
    }
    if(file_exists($dest)){ // 图片生成成功
        ob_clean();
        header(&#39;content-type:&#39;.mime_content_type($dest));
        exit(file_get_contents($dest));
    }else{
        exit(); // 生成失败退出
    }
}
// 创建缩略图
$obj = new PicThumb($logfile);
$obj->set_config($config);
$create_flag = $obj->create_thumb($source, $dest);
unlink($processing_flag); // 删除处理中标记文件
if($create_flag){ // 判断是否生成成功
    ob_clean();
    header(&#39;content-type:&#39;.mime_content_type($dest));
    exit(file_get_contents($dest));
}
?>

This article explains how to let PHP automatically generate thumbnails based on URLs and handle high concurrency Question, please pay attention to php Chinese website for more related questions.

Related recommendations:

About comparison between php string compression methods

About php Timer page running time monitoring Related introduction to classes

#Introduction to the methods of php constructor supporting different numbers of parameters

The above is the detailed content of How to let PHP automatically generate thumbnails based on URLs and handle high concurrency issues. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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