缩略图的生成有多种方式,如使用java生成缩略图,也可以使用nginx+lua实现,下面我们讲解一下使用nginx自带的模块生成缩略图,模块:-with-http_image_filter_module。
一、安装nginx
下载地址:http://nginx.org/download/
1.使用root安装依赖
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel gd gd-devel
2.下载nginx,安装
wget http://nginx.org/download/nginx-1.7.9.tar.gz
mkdir nginx
tar -zxvf nginx-1.7.9.tar.gz
cd nginx-1.7.9
./configure --prefix=/home/slim/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_image_filter_module
make
make install
二、配置
生成缩略是个消耗cpu的操作,如果访问量比较大的站点,最好考虑使用程序生成缩略图到硬盘上,或者在前端加上cache或者使用CDN。所以下面我们配置将生成的缩略图保存到硬盘供下次访问
创建图片目录:mkdir img_site
location ~* ^/resize { root /home/slim/img_site/$server_name; set $width 150; set $height 100; set $dimens ""; if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) { set $width $1; set $height $2; set $image_path $3; set $demins "_$1x$2"; } if ($uri ~* "^/resize/(.*)" ) { set $image_path $1; } set $image_uri image_resize/$image_path?width=$width&height=$height; if (!-f $request_filename) { proxy_pass http://192.168.36.54:8080/$image_uri; break; } proxy_store /home/slim/img_site/$server_name/resize$demins/$image_path; proxy_store_access user:rw group:rw all:r; proxy_set_header Host $host; expires 30d; access_log off; } location /image_resize { alias /home/slim/img_site/$server_name;#原图目录 image_filter resize $arg_width $arg_height; #指令根据height和width参数生成相应缩略图 image_filter_jpeg_quality 75; image_filter_buffer 10m; access_log off; }生成缩略图只是image_filter功能中的一个,它一共支持4种参数:
test:返回是否真的是图片
size:返回图片长短尺寸,返回json格式数据
corp:截取图片的一部分,从左上角开始截取,尺寸写小了,图片会被剪切
resize:缩放图片,等比例缩放
生成缩略图流程如下:
1、原图在/home/slim/img_site/localhost/images/a.png。我需要一份100×100的缩略图。
2、请求http://192.168.36.54:8080/resize_100x100/image/1.jpg.
3、这个请求进入了location ~* ^/resize,接着判断image_path这个目录下是否存在这张图片,如果存在直接放回给用户,
4、不存在那么跳转到http://192.168.36.54:8080/image_resize/image/1.jpg?width=100&height=100;
5、location /image_resize根据传入的width和height执行缩略功能,并且设置图像质量为75
6、接着生成文件/home/slim/img_site/localhost/resize_100x100/images/a.png缩略图到硬盘上
三、启动,测试
在/home/slim/img_site/localhost/images下新建图片目录images,copy几张图片过来。
启动nginx:
./nginx/sbin/nginx
访问http://192.168.36.54:8080/resize_100x100/images/a.png试试。
参考文章:
1.Nginx图片剪裁模块探究 http_image_filter_module
以上就介绍了Nginx 实时生成缩略图,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
