后端开发|php教程
nginx,php,步骤
后端开发-php教程以前写过一篇也是关于高性能PHP图片动态裁剪方案的文章,那文章使用的是nginx Cache和rewrite实现的,当然再加上CDN,那个方案存在一个问题就是图片并没有实际生成,而是以二进制的形式存在缓存中。如果缓存失效了那么还需要请求php再次生成。如果说到区别这是我暂且认为的吧。
听书源码,vscode 自动代码段,ubuntu pac翻墙,tomcat 配置热加载,pythongui爬虫,php call调用,上海seo排名哪家好,企业网站官网源码下载,html5 传单模板lzw
利用空余时间,新增了静态生成图片支持,支持对图片3种模式切换,在门户网站自动对图片尺寸进行裁剪,减少服务器带宽,图片裁剪使用了Imagick组件。
微信金融多级分销平台源码,vscode怎么下载中文,ubuntu查看img,tomcat各种日志,c 快速读取sqlite,ESP32股票爬虫,php商品属性,seo网络竞价运营,户外运动网站模板,微商转账作图网页版,wifi密码提示模板lzw
A、思路再现:
1、先写好请求服务器生成图片动态脚本,主要就是对图片进行等比缩放计算+裁剪。
2、确定想要生成的url规则,比如/www/300×200-1/test.jpg。
3、对浏览器做缓存处理。
4、结束。
数据查询app源码,ubuntu如何打开QQ,pycharm爬虫小程序,php转成double,seo咨询优化lzw
B、动态裁剪图片的PHP脚本
/** * Author pony_chiang * 高性能图像裁剪方案 * 需要php-imagick扩展 */ ini_set ( "memory_limit", "80M" );// 请求地址比如 /resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.jpg// nginx重写规则 rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2&width=$3&height=$4&mode=$5&path=$6 last;$path = trim ( $_GET [path] );$mode = intval ( $_GET [mode] );$site = trim ( $_GET [site] );$width = intval ( $_GET [width] );$height = intval ( $_GET [height] );$site_list = array (www => /mnt/webroot/test/ );$orig_dir = dirname ( FILE );if (! array_key_exists ( $site, $site_list )) { header ( HTTP/1.1 400 Bad Request ); exit ();}if ($mode > 3 || $mode $target_height) {$new_width = $orig_width * $target_height / $orig_height;$new_height = $target_height; }} else if ($mode == "2") { // 放大并裁剪图像 $desired_aspect = $target_width / $target_height; $orig_aspect = $orig_width / $orig_height; if ($desired_aspect > $orig_aspect) {$trim = $orig_height - ($orig_width / $desired_aspect);$image->cropImage ( $orig_width, $orig_height - $trim, 0, $trim / 2 );error_log ( "HEIGHT TRIM $trim" ); } else {$trim = $orig_width - ($orig_height * $desired_aspect);$image->cropImage ( $orig_width - $trim, $orig_height, $trim / 2, 0 ); } }$image->resizeImage ( $new_width, $new_height, imagick::FILTER_LANCZOS, 1 );$image->writeImage ( $save_path );header ( Content-Type: image/jpeg );header ( Last-Modified: . gmdate ( D, d M Y H:i:s ) . GMT );echo file_get_contents ( $save_path );return true;// 循环生成目录function wpx_mkdir($dir, $mode = 0777) { if (is_dir ( $dir ) || @mkdir ( $dir, $mode ))return true; if (! wpx_mkdir ( dirname ( $dir ), $mode ))return false; return @mkdir ( $dir, $mode );}
C、nginx.conf配置
server {listen 80;server_name ;root /mnt/webroot/test;index index.php;expires 30d;location /s { #只有当没有生成这张图片时才调用动态裁剪 if (!-e $request_filename) {rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2&width=$3&height=$4&mode=$5&path=$6 last;break; }}error_page 404 403 402 500 502 503 504 /404.html;location = /404.html {}location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; includefastcgi_params;}}
说明,强调下关于浏览器缓存的文章,不管是否是通过php生成的图片也好,还是使用nginx缓存生成的图片也罢,在php代码中添加一行
header(Last-Modified: .gmdate(D, d M Y H:i:s) . GMT );
对使用CDN有莫大的帮助。
客户端第一次访问此文件的http状态码是200,刷新后状态码一直都是304了,明白其中的好处了,本地客户端缓存了,节省带宽哦。