先开启php.ini 中extension=php_gd2.dll类库,重启服务。
如下是根据已有图片文件,创建指定大小图片文件。
static function ImageMakeThumb($srcFile, $dstFile, $dstW, $dstH) {
try {
list ( $width, $height, $type, $attr ) = getimagesize ( $srcFile );
echo $width, $height, $type, $attr;
//1 = GIF,2 = JPG,3 = PNG ,6 = BMP
switch ($type) {
case 1 :
$im = ImageCreateFromGIF ( $srcFile );
break;
case 2 :
$im = @imagecreatefromjpeg( $srcFile );
break;
case 3 :
$im = @ImageCreateFromPNG ( $srcFile );
break;
case 6:
$im = @imagecreatefromwbmp( $srcFile );
break;
default :
return False;
}
if (! $im)
return False;
$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 = ($dstX < 0) ? 0 : $dstX;
$dstY = ($dstX < 0) ? 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 );
return True;
} catch ( Exception $ef ) {
echo $ef ;
return false;
}
}