如下在window中使用伪静态处理缩略图,很多地方都需要缩略图。但是生成出来很占位置,oss的确不错选择。但是流量大了oss烧的慌。
新建如下php文件,使用convert来进行处理缩略图到tmp临时目录缓存。
其实原理还是利用传上来w 、h参数,自动裁剪图片大小尺寸,并且将文件缓存起来防止重复裁剪!
<?php $file = isset($_GET['file']) ? $_GET['file'] : ''; $w = isset($_GET['w']) ? $_GET['w'] : '200'; $h = isset($_GET['h']) ? $_GET['h'] : '100'; $xossprocess = isset($_GET['x-oss-process']) ? $_GET['x-oss-process'] : ''; $disposeUpload = './tmp/'; $file = './' . $file; if (!is_file($file)) { header('HTTP/1.1 404 Not Found'); exit; } //如果没有参数 if (empty($xossprocess)) { out($file); exit; } else { $oss = explode(',', $xossprocess); $h = str_replace('h_', '', $oss[2]); $w = str_replace('w_', '', $oss[3]); if (!is_numeric($h) || !is_numeric($w)) { header('HTTP/1.1 404 Not Found'); exit; } } $fileArray = pathinfo($file); $filename = md5($file . $w . $h); $disposeUpload = $disposeUpload . $filename . '.' . $fileArray['extension']; //如果存在就直接发送 if (is_file($disposeUpload)) { out($disposeUpload); exit; } $extension = strtolower($fileArray['extension']); if (!in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) { header('HTTP/1.1 404 Not Found'); exit; } disposeResize($file, $disposeUpload, $w, $h); out($disposeUpload); exit; function out($imgsrc) { $info = getimagesize($imgsrc); $imgdata = fread(fopen($imgsrc, 'rb'), filesize($imgsrc)); header("content-type:{$info['mime']}"); echo $imgdata; } function disposeResize($file, $newfile, $w, $h) { $cmd = " convert -resize {$w}x{$h}! {$file} {$newfile}"; system($cmd, $r); if ($r == 0) { return true; } else { return false; } }
iis下伪静态规则:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="zip" stopProcessing="true"> <match url="^(uploads/.*jpg|uploads/.*jpeg|uploads/.*png|uploads/.*gif)$" /> <action type="Rewrite" url="/test.php?file={R:1}" /> <conditions> </conditions> </rule> </rules> </rewrite> </system.webServer> </configuration>
请求方法:
https://wstatic.apizl.com/uploads/allimg/181008/1-1Q00P923140-L.png?x-oss-process=image/resize,m_fill,h_141,w_232
请求参数可以自定义为其他
关键字词: