将类似http://127.0.0.1/upload/147332819224704_400x300.jpg地址重写为http://127.0.0.1/resize.php/src=http://developer.51cto.com/art/201711/147332819224704&w=400&h=300&type=jpg。 功能实现 接下来就是功能的实现,逻辑和nodejs版逻辑一致,代码如下:
- function getThumbImg($src,$w,$h,$type)
- {
- global $thumbs;
- return $_SERVER['DOCUMENT_ROOT'].$thumbs.$src.'_'.$w.'_'.$h.'.'.$type;
- }
- function imgResize($f,$th,$w,$h,$t)
- {
- $imagick = new Imagick();
- $imagick->readImage($_SERVER['DOCUMENT_ROOT'].'\\'.$f);
- $width = $imagick->getImageWidth();
- $height = $imagick->getImageHeight();
- if(!$w||!$h||$w>=$width||$h>=$height){
- header('Content-Type:image/'.$t);
- echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'\\'.$f);
- }else{
- $imagick->stripImage();
- $imagick->cropThumbnailImage($w, $h);
- $imagick->writeImage($th);
- header('Content-Type:image/'.$t);
- echo $imagick->getImageBlob();
- $imagick->clear();
- $imagick->destroy();
- }
- }
- $uploadDir = "uploads/images/";
- $thumbs = "uploads/thumbs/";
- $src = $_GET['src'];
- $width = $_GET['w'];
- $height = $_GET['h'];
- $type = $_GET['type'];
- $imgFile = $uploadDir.$src.'.'.$type;
- $notFound = '不好意思,该图片不存在或已被删除!';
- $thumb = getThumbImg($src,$width,$height,$type);
- if(file_exists($imgFile)){
- if(file_exists($thumb)){
- header('Content-Type:image/'.$type);
- echo file_get_contents($thumb);
- }else{
- imgResize($imgFile,$thumb,$width,$height,$type);
- }
- }else{
- header("HTTP/1.0 404 Not Found");
- header(
推荐阅读
Tech Neo技巧沙龙 | 11月25号,九州云/ZStack与您一路商量云时代收集界线治理实践Cartesian针对40多个访谈和跨越100个受访者的在线查询拜访促成其宣布了《收集的将来:应对虚拟范畴的变革》的申报,受访>>>详细阅读
本文标题:nodejs和php实现图片访问实时处理
地址:http://www.17bianji.com/lsqh/39096.html
1/2 1