Home  >  Article  >  php教程  >  PHP 怎么样把一张图片缩小到指定大小

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

WBOY
WBOYOriginal
2016-06-06 19:42:282246browse

如果想真正改变,你看看这个代码(没试验过): 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这几个函数

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:.net调用phpNext article:PHP编写rss源(续)