原来图片服务器采用Windows .net架构,鉴于需求需要生成各种尺寸图片。

流程说明:

用户从Nginx请求对应的图片,判断是否存在_200x300的对应参数,如果没有就直接请求到对应目录的原图,否则继续判断是否在本地已经生成了对应的缓存图片,如果存在返回已经生成过的定制尺寸图片,否则请求PHP动态生成。

Nginx部分配置:

    server {listen       80;server_name  pics.abc.com;location / {root   /var/www/html;index  index.html index.htm index.php;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}location ~ \_(\d+)x(\d+)\.(jpg|png|gif|jpeg|bmp)$ {  //判断是否定制图try_files $uri /temp/$uri /get.php;    //判断是否已生成过定制图否则转交给/get.phpexpires      30d;}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;}location ~ .*\.(gif|jpg|jpeg|png|bmp)${expires      30d;}}}

在/var/www/html我们以只读方式挂载Windows的目录,修改/etc/fstab,添加

\\192.168.2.3\f$\pics.abc.com\pics /var/www/html/pics/ cifs    ro,username=user,password=pass   1  2

然后重启netfs服务,另外执行下面命令,安装依赖的包

yum -y install samba-client cifs-utils
service netfs restart
chkconfig netfs on

生成的缩率图会放到网站目录的temp目录下,如请求的http://pics.abc.com/pics/201604/29/abc_200x300.jpg

则生成的图片放在temp/pics/201604/29/abc_200x300.jpg目录下

PHP脚本:

function thumb($src, $width, $height, $filename, $mode = 'scale', $quality = '100') {
try {$p_w_picpathValue = getp_w_picpathsize($src);$sourceWidth = $p_w_picpathValue[0]; //原图宽$sourceHeight = $p_w_picpathValue[1]; //原图高$thumbWidth = $width; //缩略图宽$thumbHeight = $height; //缩略图高$_x = 0;$_y = 0;$w = $sourceWidth;$h = $sourceHeight;if ($mode == 'scale') {if ($sourceWidth <= $thumbWidth && $sourceHeight <= $thumbHeight) {$_x = floor(($thumbWidth - $sourceWidth) / 2);$_y = floor(($thumbHeight - $sourceHeight) / 2);$thumbWidth = $sourceWidth;$thumbHeight = $sourceHeight;} else {if ($thumbHeight * $sourceWidth > $thumbWidth * $sourceHeight) {$thumbHeight = floor($sourceHeight * $width / $sourceWidth);$_y = floor(($height - $thumbHeight) / 2);} else {$thumbWidth = floor($sourceWidth * $height / $sourceHeight);$_x = floor(($width - $thumbWidth) / 2);}}} else if ($mode == 'crop') {if ($sourceHeight < $thumbHeight) { //如果原图尺寸小于当前尺寸 $thumbWidth = floor($thumbWidth * $sourceHeight / $thumbHeight);$thumbHeight = $sourceHeight;}if ($sourceWidth < $thumbWidth) {$thumbHeight = floor($thumbHeight * $sourceWidth / $thumbWidth);$thumbWidth = $sourceWidth;}$s1 = $sourceWidth / $sourceHeight; //原图比例$s2 = $width / $height; //新图比例if ($s1 == $s2) {} else if ($s1 > $s2) { //全高度 $y = 0;$ax = floor($sourceWidth * ($thumbHeight / $sourceHeight));$x = ($ax - $thumbWidth) / 2;$w = $thumbWidth / ($thumbHeight / $sourceHeight);} else { //全宽度 $x = 0;$ay = floor($sourceHeight * ($thumbWidth / $sourceWidth)); //模拟原图比例高度$y = ($ay - $thumbHeight) / 2;$h = $thumbHeight / ($thumbWidth / $sourceWidth);}}switch ($p_w_picpathValue[2]) {case 2: $source = p_w_picpathcreatefromjpeg($src);break;case 1: $source = p_w_picpathcreatefromgif($src);break;case 3: $source = p_w_picpathcreatefrompng($src);break;case 6: $source = p_w_picpathcreatefromwbmp($src);break;default: defulat();return;}header("Content-type: p_w_picpath/jpeg");$thumb = p_w_picpathcreatetruecolor($width, $height);p_w_picpathfill($thumb, 0, 0, p_w_picpathcolorallocate($thumb, 255, 255, 255));p_w_picpathcopyresampled($thumb, $source, 0, 0, $x, $y, $width, $height, $w, $h);p_w_picpathjpeg($thumb, null, $quality);p_w_picpathjpeg($thumb, $filename, $quality);p_w_picpathdestroy($thumb);p_w_picpathdestroy($source);
} catch (Exception $ex) {defulat();}
}function defulat() {
/*$default_img = realpath('../pictures/nopic.gif');ob_start();header('Content-type:p_w_picpath/jpeg');readfile($default_img);ob_flush();flush();
*/
echo 'error';
}function mkDirs($dir){if(!is_dir($dir)){if(!mkDirs(dirname($dir))){return false;}if(!mkdir($dir,0755)){return false;}}return true;
}$uri=$_SERVER['REQUEST_URI'];
$p_w_picpath=basename($uri);$temp='./temp/'.dirname($uri).'/';
$imgpath='.'.dirname($uri).'/';/*
//检查本地是否存在文件,原图
if(file_exists($temp.$p_w_picpath)){ob_start();header('Content-type:p_w_picpath/jpeg');readfile($temp.$p_w_picpath);ob_flush();flush();exit();
}
*///检查生成的图片是否曾经生成过,存在即返回,否则重新生成新图
if(!preg_match('/_(\d+)x(\d+)/', $p_w_picpath, $wh)){ob_start();header('Content-type:p_w_picpath/jpeg');readfile($imgpath.$p_w_picpath);ob_flush();flush();exit();
}$width = $wh[1];
$height = $wh[2];
$source_img=preg_replace('/_(\d+)x(\d+)/', '', $p_w_picpath);
//对长宽都超过的图片返回原图
if($width>=2000 || $height>=2000){ob_start();header('Content-type:p_w_picpath/jpeg');readfile($imgpath.$source_img);ob_flush();flush();exit();
}//图片处理
$src=$imgpath.$source_img;
$filename=$temp.$p_w_picpath;
mkDirs($temp);
//thumb(realpath($src), $width, $height, $filename, 'crop', '85');
thumb(realpath($src), $width, $height, $filename, 'crop', '100');

PHP生成尺寸部分参考<PHP图片自动裁切应付不同尺寸的显示>

如果上述服务器出现问题,则降低的Nginx配置为

    server {listen       80;server_name  localhost;access_log  logs/host.access.log  main;location / {rewrite ^/(.*)\_(\d+)x(\d+)\.(.*)$ /$host/$1 permanent;rewrite ^/([0-9a-zA-Z]+)/(.*)$ /$host/$1/$2 permanent;root   html;index  index.html index.htm;}}

转载于:https://blog.51cto.com/fengwan/1769043

Nginx+PHP实时生成不同尺寸图片相关推荐

  1. 图片资源 php,php URL图片资源传参生成对应尺寸图片

    最近项目中需要上传大图,然后不同设备请求不同大小的图片,之前有用过一个通过URL参数来获取不同大小的图片的接口感觉设计方式请不错,于是就百度看看类似是如何实现的,找了几天找个两个功能类似的记录下. 1 ...

  2. 基于.NetCore开发博客项目 StarBlog - (15) 生成随机尺寸图片

  3. python批量生成图片_利用Python批量生成任意尺寸的图片

    实现效果 通过源图片,在当前工作目录的/img目录下生成1000张,分别从1*1到1000*1000像素的图片. 效果如下: 目录结构 实现示例 # -*- coding: utf-8 -*- imp ...

  4. python批量生成图_利用Python批量生成任意尺寸的图片

    实现效果 通过源图片,在当前工作目录的/img目录下生成1000张,分别从1*1到1000*1000像素的图片. 效果如下: 目录结构 实现示例 # -*- coding: utf-8 -*- imp ...

  5. nginx+lua+GraphicsMagick生成实时缩略图-CentOS7

    背景说明 大多数的系统都会涉及缩略图的处理,比如新闻系统和电商系统,特别是电商系统,每个商品大图都会对应一系列尺寸的缩略图用于不同业务场景的使用.部分系统也会生成不同尺寸的缩略图以供PC.手机端.ip ...

  6. CV之IE之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成不同尺寸和质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)—五个架构设计思维导图

    CV之IE之Inception:基于TF框架利用Inception模型+GD算法的某层网络图像生成不同尺寸和质量的Deep Dream幻觉梦境图片(特征可视化实现图像可解释性)-五个架构设计思维导图 ...

  7. WordPress彻底禁用上传媒体图片自动生成缩略图及多尺寸图片(亲测可用)

    WordPress默认上传图片的时候会自动生成缩略图及多尺寸的图片文件,大部分网站都用不到这些多余的图片,不仅仅占用空间,而且上传的时候还会消耗额外的性能. 下面仅需两段函数代码即可彻底禁用该功能. ...

  8. 禁止wordpress自动生成多尺寸的图片

    先来看一下wordpress在上传图片时,会为我们生成多少种图片吧,首先程序会对上传的图片尺寸进行额外检测,再自动生成 1536 宽度的 2x 中大尺寸(2x Medium Large),2048 宽 ...

  9. Asp.net mvc 实时生成缩率图到硬盘

    之前对于缩率图的处理是在图片上传到服务器之后,同步生成两张不同尺寸的缩率供前端调用,刚开始还能满足需求,慢慢的随着前端展示的多样化,缩率图已不能前端展示的需求,所以考虑做一个实时生成图片缩率图服务. ...

最新文章

  1. TypeError: ord() expected string of length 1, but int found
  2. linux增加电子档案空间,Linux 建立 SWAP 档案空间
  3. ElasticSearch知识点整理,值得收藏!
  4. 游国色天香中国馆有感
  5. 软件测试度量计算方法有哪些,软件测试度量(三)
  6. Android笔记 采用httpclient提交数据到服务器demo
  7. 使用Directory.EnumerateFiles进行批处理
  8. github html5 预览,github 上如何直接预览仓库中的html
  9. Bettertouchtool for Mac(鼠标增强软件)
  10. python立体匹配评价_综述翻译:机器学习与立体匹配(一)
  11. win10如何截屏_6个Win10系统使用小技巧,对你一定有用!
  12. 我在 CSDN 的小窝
  13. mysql关联子查询_MySQL 关联子查询
  14. 西门子触摸屏脚本程序_西门子触摸屏实例程序
  15. MATLAB地图工具箱学习总结(二)大圆和恒向线
  16. 网络原理考点之无线网络应用层协议
  17. android 层叠view,RecyclerView进阶之层叠列表(上)
  18. String类型转json格式
  19. .xyz文件的定义及读取
  20. 熟悉的人不认识我了,不熟悉的人认识我了

热门文章

  1. 《Python Cookbook 3rd》笔记(2.9):将Unicode文本标准化
  2. STL源码剖析 list概述
  3. Ubuntu boost库文件安装编译
  4. 基于ECC算法的秘钥协商
  5. 对于以太坊的Solidity语言介绍
  6. 英语口语海报演讲--东软
  7. c++面向对象高级编程 学习三 堆、栈和内存泄漏
  8. 给生命一个助跑的过程(图)
  9. PMT_Stream数据结构
  10. Vue 生命周期中 mounted( ) 和 created( ) 的区别