基于PHP服务端图片生成缩略图的方法详解
发布时间:2020-05-28 16:47:16 所属栏目:PHP 来源:互联网
导读:本篇文章是对PHP服务端图片生成缩略图的方法进行了详细的分析介绍,需要的朋友参考下
|
<div class="codetitle"><a style="CURSOR: pointer" data="60261" class="copybut" id="copybut60261" onclick="doCopy('code60261')"> 代码如下:<div class="codebody" id="code60261"> //定义缩略图片尺寸$picSize = array('100_100'=> 1, '200_100'=> 1 ); $imagePath = "../image/"; function parseUrl($url){ preg_match("/(?P return $match; } $urlArr = explode("/",$_SERVER['REQUEST_URI']); $imgName = $urlArr[count($urlArr)-1]; $picInfo = parseUrl($imgName); //错误尺寸if(empty($picInfo['width']) || empty($picInfo['height']) ||!array_keyexists($picInfo['width'].''.$picInfo['height'],$picSize)) die('不存在该尺寸图片'); $originalPic = $imagePath.$picInfo['name'].'/'.$picInfo['name'].'.'.$picInfo['ext']; //原始图不存在if(!file_exists($originalPic)) die("图片不存在!");/* 等比例压缩图片/switch($picInfo['ext']){ case 'jpg': $orgImg = ImageCreateFromJpeg($originalPic); break; default: break; } $owidth = ImageSX($orgImg); //原始尺寸$oheight = ImageSY($orgImg);$tW = $picInfo['width']; $tH = $picInfo['height']; //获取缩略图尺寸if($owidth/$oheight > $tW/$tH){$tH = intval($tW $oheight/$owidth); }else{ $tW = intval($tH * $owidth/$oheight); } //生成背景图$new_img = ImageCreateTrueColor($picInfo['width'],$picInfo['height']);$bgColor = imagecolorallocate($new_img,255,255); if (!@imagefilledrectangle($new_img,$picInfo['width']-1,$picInfo['height']-1,$bgColor)) { echo "无法创建背景图"; //@todo记录日志 exit(0); } if (!@imagecopyresampled($new_img,$orgImg,($picInfo['width']-$tW)/2,($picInfo['height']-$tH)/2,$tW,$tH,$owidth,$oheight)) { echo "生成图片失败"; exit(0); } //生成图片ob_start();imagejpeg($new_img); $_newImg = ob_get_contents(); ob_end_clean(); file_put_contents($imagePath.$picInfo['name']."/".$imgName,$_newImg); header("Content-type:image/jpeg; charset=utf-8"); imagejpeg($new_img); ?> 使用时候绑定apache conf 的 documentError 404 的handler 为此文件。。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
