php GD库的使用
<?php/***调整图片大小*$path图片的宽度大于$width则等比缩放*$path图片的高度大于$height切成多张图片*@param$path图片地址*@param$widthinteger|string图片宽度*@param@heightinteger|string图片高度*/functionimgResize($path,$width,$height){//存放生成的图片路径$imgPaths=array();//获取图片宽度/高度/类型list($srcW,$srcH,$type)=getp_w_picpathsize($path);$type-=1;//创建源图像$p_w_picpathcreatefromFuns=array('p_w_picpathcreatefromgif','p_w_picpathcreatefromjpeg','p_w_picpathcreatefrompng');$src=$p_w_picpathcreatefromFuns[$type]($path);//图片的宽度大于$width则等比缩放if($srcW>$width){$dstHeight=$srcH*($width/$srcW);//新建一个真彩***像$dst=p_w_picpathcreatetruecolor($width,$dstHeight);p_w_picpathcopyresampled($dst,$src,0,0,0,0,$width,$dstHeight,$srcW,$srcH);$src=$dst;//更新原图的宽度和高度$srcW=$width;$srcH=$dstHeight;}else{//没有调整宽度$width=$srcW;}//图片的高度大于$height切成多张图片if($srcH>$height){//切割的高度位置$srcY=0;do{$dstHeight=($srcY+$height>$srcH)?($srcH-$srcY):($height);$dst=p_w_picpathcreatetruecolor($width,$dstHeight);p_w_picpathcopyresampled($dst,$src,0,0,0,$srcY,$width,$dstHeight,$width,$dstHeight);$filePath='./img/'.md5(microtime(true)).'.jpg';$imgPaths[]=$filePath;ImageJpeg($dst,$filePath);is_resource($dst)andp_w_picpathdestroy($dst);$srcY+=$dstHeight;}while($srcY<$srcH);}else{//如果高度符合则将调整了原图(也有可能是调整了宽度之后的)保存为文件$filePath='./img/'.md5(microtime(true)).'.jpg';$imgPaths[]=$filePath;ImageJpeg($src,$filePath);}//释放内存isset($src)andis_resource($src)andp_w_picpathdestroy($src);isset($dst)andis_resource($dst)andp_w_picpathdestroy($dst);return$imgPaths;}/***调整图片清晰度*@paramstring$path图片路径*@paramstring$quality清晰度*@returnstring新的图片路径*/functionreQuality($path,$quality){$p_w_picpath_type=p_w_picpath_type_to_extension(exif_p_w_picpathtype($path),false);$fun="p_w_picpathcreatefrom{$p_w_picpath_type}";$src=$fun($path);$newPath=generateUniqueFilePath('./img/');ImageJpeg($src,$newPath,$quality);return$newPath;}/***生成唯一的文件路径*@param$basePathstring文件夹路径*@param$suffixstring*@returnstring文件路径*/functiongenerateUniqueFilePath($basePath,$suffix='.jpg'){$filePath=$basePath.md5(microtime(true)).$suffix;return$filePath;}header('Content-Type:p_w_picpath/png');$path='./img/jjj.jpg';$imgPaths=imgResize($path,10000,10000);//reQuality($path,1);
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。