Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 19:42:28
Original
2250 people have browsed it

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template