nodejs和php实现图片访问实时处理(2)

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("status: 404"); echo $notFound; }

至此,图片访问实时处理也就完成了。其实大部分图片服务器都需要这样的功能,而不是事先生成好几套尺寸的图片。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjwfj.html