许久以前写过一篇也是关于高性能PHP图片动态裁剪方案的文章,那文章使用的是nginx Cache和rewrite实现的,当然再加上CDN,那个方案存在一个问题就是图片并没有实际生成,而是以二进制的形式存在缓存中。如果缓存失效了那么还需要请求php再次生成。如果说到区别这是我暂且认为的吧。

利用空余时间,新增了静态生成图片支持,支持对图片3种模式切换,在门户网站自动对图片尺寸进行裁剪,减少服务器带宽,理论上应该也满足了业务的需求吧,图片裁剪使用了Imagick组件。

一、思路再现:

1、先写好请求服务器生成图片动态脚本,主要就是对图片进行等比缩放计算+裁剪。

2、确定你想要生成的url规则,比如http://www.domain.com/www/300×200-1/test.jpg。

3、对浏览器做缓存处理。

4、结束。

二、动态裁剪PHP脚本

/**

* Author pony_chiang

* 高性能图像裁剪方案

* 需要php-imagick扩展

*/

ini_set ( "memory_limit", "80M" );

// 请求地址比如  http://yourdomain.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png

// 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 < 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, PATHINFO_EXTENSION );

$file_name = basename ( $path, $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, $orig_height, $type, $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, $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 );

}

三、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=$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;

include        fastcgi_params;

}

}

PS:在文章的末尾我要特别强调一点是关于浏览器缓存的文章,不管你是否是通过php生成的图片也好,还是使用nginx缓存生成的图片也罢,在php代码中添加一行

header('Last-Modified: ' .gmdate('D, d M Y H:i:s') . ' GMT' );

对你使用CDN有十分莫大的帮助。具体产生的效果就是客户端第一次访问此文件的http状态码是200,刷新后状态码一直都是304了。

nginx php image,PHP加Nginx实现动态裁剪图片方案相关推荐

  1. unity 动态裁剪图片

    在加载图片又是需要去动态获取图片的一部分,或者要识别一张图片的二维码,但是二维码比较小一整张识别容易失败,就可以对图片进行分区识别 public void TailorTexture()     { ...

  2. iOS实现动态区域裁剪图片

    阅读 249 收藏 32 2017-11-29 原文链接:github.com 想自己动手搭建一个 Discuz 论坛?试试腾讯云上实验室吧https://cloud.tencent.com/deve ...

  3. linux中nginx上传文件方法,nginx加载webdav模块实现http协议上传文件

    1 简介 WebDAV (Web-based Distributed Authoring and Versioning) 一种基于 HTTP 1.1协议的通信协议.它扩展了HTTP 1.1,在GET. ...

  4. Nginx配置及配置加载

    Nginx配置(Directives) Nginx默认配置 worker_processes 1;events {worker_connections 1024; }http {include mim ...

  5. nginx 转发慢_学习Nginx的正确姿势,多图详解助你更上一层楼!(干货收藏篇)...

    本文主要帮助大家熟悉 Nginx 有哪些应用场景.Nginx 特点和架构模型以及相关流程.Nginx 定制化开发的几种模块分类. 本文将围绕如下几个部分进行讲解: Nginx 简介及特点 Nginx ...

  6. etcd nginx 容器_Etcd+Confd实现Nginx配置文件自动管理

    一.需求 我们使用Nginx做七层负载均衡,后端是Tomcat.项目采用灰度发布方式,每次项目升级,都要手动先从Nginx下摘掉一组,然后再升级这组,当项目快速迭代时,手动做这些操作显然会增加部署时间 ...

  7. nginx【nginx跨域、nginx开启gizp压缩、nginx服务器部署项目】

    nginx是一个高性能的HTTP和反向代理服务器,它使用配置文件决定如何提供内容.要监听的端口等.因此常用来做静态资源服务器和后端的反向代理服务器. 安装:brew install nginx 启动: ...

  8. nginx 学习 --->>> nginx 实现动静分离

    6.配置示例 -> 动静分离 ​ Nginx 动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离. ​ 严格意义上说应该是动态请求跟静态请求分开,可以理解 ...

  9. nginx作为web服务以及nginx.conf详解

    Nginx系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.nginx简介 nginx是一个优秀的web服务程序.反向代理程序.它采用非 ...

最新文章

  1. xilinx SoC学习笔记之PetaLinux
  2. 反射_Class对象功能概述
  3. 开发Eclipse插件
  4. java selenium_selenium 常见面试题以及答案(Java版)
  5. 破解centos7root口令
  6. (转)ScriptManager.RegisterStartupScript方法和Page.ClientScript.RegisterStartupScript() 方法...
  7. 数据挖掘原理与算法 K-Means算法
  8. 如何监控mysql主从之间的延迟
  9. VS提示error C2011: “timespec”:“struct”类型重定义
  10. 简单解说思科命令大全
  11. 传智播客黑马程序员_Hanselminutes播客48-适用于极客和程序员的入门棋盘游戏
  12. Oracle锁表会影响查询效率么,oracle锁表查询,资源占用,连接会话,低效SQL等性能检查...
  13. Qt+VS2019+OpenCV 使用问题 - Cound not find “QT“
  14. 第六周博客作业西北师范大学|李晓婷
  15. Win10将用户名修改为英文
  16. 行存储 VS 列存储
  17. OpenCvSharp 棋盘格标定助手
  18. 边坡裂缝拉绳位移计的原理及怎样进行长期健康监测
  19. VS2008技巧收集
  20. 千亿资本角逐二手车电商,前瞻还是大泡沫?

热门文章

  1. iOS高仿微信、仪表盘、图片标注图片滤镜、高斯模糊、上拉加载、下拉刷新等源码...
  2. 学校在线二手交易平台-服务器模块(本科生毕业设计)
  3. HTTP中的中文编码与在线编码转换工具
  4. unicode在线编码,java加密解密
  5. vscode 怎么换字体(标准的那种)
  6. Python序列练习题【第十二周】
  7. 读《正见 佛陀的证悟》
  8. python sanic_Sanic + 前端MVVM 一种新一代Python高性能全栈开发实践
  9. 用ANSYS画矩形_【图片】日系楼梯怎么画?教你用一点透视画日系楼梯!【猫爪绘画吧】...
  10. React使用setState后页面没有更新