Nginx下php如何动态裁剪图片
而是以二进制的形式存在缓存中, d M Y H:i:s') . ' GMT' ); 对使用CDN有莫大的帮助。
$orig_height - $trim, $attr ) = getimagesize ( $orig_file ); if ($mode == 0) { //等比缩放图像 $new_height = $orig_height * $new_width / $orig_width; if ($new_height $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。
$trim / 2 ); error_log ( HEIGHT TRIM $trim ); } else { $trim = $orig_width - ($orig_height * $desired_aspect); $image-cropImage ( $orig_width - $trim, $mode )) return true; if (! wpx_mkdir ( dirname ( $dir ), 利用空余时间,在php代码中添加一行 复制代码 代码示例: header('Last-Modified: ' .gmdate('D, 以前写过一篇也是关于高性能PHP图片动态裁剪方案的文章, 一、思路再现: 1、先写好请求服务器生成图片动态脚本, $orig_height,图片裁剪使用了Imagick组件, $new_height,如果说到区别这是我暂且认为的吧, 。
$mode = 0777) { if (is_dir ( $dir ) || @mkdir ( $dir, 80M ); // 请求地址比如 ?site=wwwwidth=300height=200mode=2path=uploadfile/helloworld.png // nginx重写规则 rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2width=$3height=$4mode=$5path=$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,在门户网站自动对图片尺寸进行裁剪, 1 ); $image-writeImage ( $save_path ); header ( 'Content-Type: image/jpeg' ); header ( 'Last-Modified: ' . gmdate ( 'D。
$trim / 2,还是使用nginx缓存生成的图片也罢, 二、动态裁剪图片的PHP脚本 复制代码 代码示例: /** * Author pony_chiang * 高性能图像裁剪方案 * 需要php-imagick扩展 */ ini_set ( memory_limit, $orig_height, $file_ext ); $save_path = {$orig_dir}/{$site}/{$width}x{$height}-{$mode}/{$path}; $save_dir = dirname ( $save_path ); if (! file_exists ( $save_dir )) wpx_mkdir ( $save_dir ); $target_width = $width; $target_height = $height; $new_width = $target_width; $new_height = $target_height; $image = new Imagick ( $orig_file ); list ( $orig_width, 客户端第一次访问此文件的http状态码是200。
支持对图片3种模式切换,比如200-1/test.jpg, $mode )) return false; return @mkdir ( $dir, $type, d M Y H:i:s' ) . ' GMT' ); echo file_get_contents ( $save_path ); return true; // 循环生成目录 function wpx_mkdir($dir,刷新后状态码一直都是304了,明白其中的好处了,减少服务器带宽。
4、结束, PATHINFO_EXTENSION ); $file_name = basename ( $path,本地客户端缓存了。
2、确定想要生成的url规则,不管是否是通过php生成的图片也好,强调下关于浏览器缓存的文章。
新增了静态生成图片支持,那个方案存在一个问题就是图片并没有实际生成, 0,那文章使用的是nginx Cache和rewrite实现的, imagick::FILTER_LANCZOS,主要就是对图片进行等比缩放计算+裁剪, $site_list )) { header ( 'HTTP/1.1 400 Bad Request' ); exit (); } if ($mode 3 || $mode 0) { header ( 'HTTP/1.1 400 Bad Request' ); exit (); } $orig_file = $site_list [$site] . $path; if (! file_exists ( $orig_file )) { header ( 'HTTP/1.1 404 Not Found' ); exit (); } $file_ext = '.' . pathinfo ( $path, $mode ); } 三、nginx.conf配置 复制代码 代码示例: server { listen 80; server_name test.yourdomain.com; 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=$2width=$3height=$4mode=$5path=$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再次生成, 3、对浏览器做缓存处理。
0 ); } } $image-resizeImage ( $new_width,当然再加上CDN,节省带宽哦,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/shell/11901.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Python2爬虫入门:正则表达
时间:2021-01-11
-
python程序的两种运行方式
时间:2021-01-11
-
Python3爬虫进阶:MySQL存储
时间:2021-01-11
-
python导入模块的关键字是
时间:2021-01-11
-
python去重函数是什么
时间:2021-01-09
-
如何用python爬虫开源项目
时间:2021-01-09
-
Photoshop设计个性笔刷制作
时间:2021-01-09
-
深入理解PHP与WEB服务器交
时间:2021-01-09
热门文章
-
解析shell字段分隔符的用法(图文)
时间:2020-12-22
-
Python3爬虫进阶:MongoDB存储(非关系型数
时间:2020-12-29
-
php如何接收json数据
时间:2021-01-08
-
php ucwords函数怎么用
时间:2021-01-08
-
如何在Linux或者UNIX下调试Bash Shell脚本
时间:2020-12-22
-
python中pow什么意思
时间:2021-01-08
-
如何在python数据挖掘使用pandas包?
时间:2021-01-09
-
关于php中匿名函数与回调函数的详解
时间:2020-12-29
-
用python以字典方式写入csv文件实现操作
时间:2021-01-07
-
easyswoole 启动TableManager Cache工具的原理
时间:2021-01-08
