您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP 怎么样把一张图片缩小到指定大小

2024/12/5 14:38:37发布19次查看
如果想真正改变,你看看这个代码(没试验过): function makethumb( $srcfile , $dstfile , $dstw , $dsth ) { $data = getimagesize ( $srcfile , $info ); switch (coreutil :: getfileextension( $dstfile )){ case ' gif ' : $im = @imagecreatefromgif
如果想真正改变,你看看这个代码(没试验过):
function makethumb($srcfile,$dstfile,$dstw,$dsth) {
$data=getimagesize($srcfile,&$info);
switch (coreutil::getfileextension($dstfile)){
case'gif':
$im= @imagecreatefromgif($srcfile); break;
case'jpg':
case'jpeg':
$im= @imagecreatefromjpeg($srcfile); break;
case'png':
$im= @imagecreatefrompng($srcfile); break;
default:returnfalse;
}
if(!$im) returnfalse;
$srcw=imagesx($im);
$srch=imagesy($im);
$dstx=0;
$dsty=0;
if ($srcw*$dsth>$srch*$dstw){
$fdsth=round($srch*$dstw/$srcw);
$dsty=floor(($dsth-$fdsth)/2); $fdstw=$dstw;
} else {
$fdstw=round($srcw*$dsth/$srch); $dstx=floor(($dstw-$fdstw)/2);
$fdsth=$dsth;
}
$ni=imagecreate($dstw,$dsth);
$dstx=($dstx0)?0:$dstx;
$dsty=($dstx0)?0:$dsty;
$dstx=($dstx>($dstw/2))?floor($dstw/2):$dstx;
$dsty=($dsty>($dsth/2))?floor($dsth/s):$dsty;
$black= imagecolorallocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstw,$dsth,$black);
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$fdstw,$fdsth,$srcw,$srch);
imagejpeg($ni,$dstfile);
imagedestroy($im);
imagedestroy($ni);
returntrue;
}
大概就是用到imagecreatefromjpeg、imagecreatetruecolor、imagecopyresampled 、 imagepng这几个函数
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product