老了,写不动代码了,求缩图代码./**
1 缩放规则
例如:原图 1000 * 1000 缩略图为 100 * 50 的,则取原图 1000* 500 的上半张图,缩成100*50 的.
例如:原图 1000 * 1000 缩略图为 50 * 100 的,则取原图 500* 1000 的上半张图,缩成50*100 的.
2 生产新的图片,不删原图.
*/不要思路,仅要结果,非常感激.
1 缩放规则
例如:原图 1000 * 1000 缩略图为 100 * 50 的,则取原图 1000* 500 的上半张图,缩成100*50 的.
例如:原图 1000 * 1000 缩略图为 50 * 100 的,则取原图 500* 1000 的上半张图,缩成50*100 的.
2 生产新的图片,不删原图.
*/不要思路,仅要结果,非常感激.
http://prato.bokele.com/?ArticleID=19533
http://www.storyday.com/html/y2009/2174_php-image-resizing.html
http://www.chinaz.com/Program/PHP/03301101R2010.html
http://www.desteps.com/wprogram/php/1056.html
http://zhidao.baidu.com/question/164754012
http://bbs.blueidea.com/viewthread.php?tid=1038060&page=
http://www.v6online.com/html/Programming/Php/2010/1226/3755.html
/**
* 生成缩略图
* $srcName----为原图片路径
* $newWidth,$newHeight----分别缩略图的最大宽,高
* $newName----为缩略图文件名(含路径),默认为加入thumbnail
* @param string $srcName
* @param int $newWidth
* @param int $newHeight
* @param string $newName
* return viod
*/
function resizeImg($srcName,$newWidth,$newHeight,$newName="")
{
if($newName=="")
{
$nameArr=explode('.',$srcName);
$expName=array_pop($nameArr);
$expName='thumbnail.'.$expName;
array_push($nameArr,$expName);
$newName = implode('.',$nameArr);
}
$info = "";
$data = getimagesize($srcName,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";
exit();
}
$im = ImageCreateFromGIF($srcName);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
exit();
}
$im = ImageCreateFromJpeg($srcName);
break;
case 3:
$im = ImageCreateFromPNG($srcName);
break;
}
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$Rate = $srcW /$newWidth;
$destW = $srcW;
$destH = $srcW * $newHeight / $newWidth;
if($Rate > $srcH /$newHeight) {
$Rate = $srcH /$newHeight;
$destH = $newHeight;
$destW = $destH*$newWidth/$newHeight;
}
if($srcW>$newWidth||$srcH>$newHeight)
{
if(function_exists("imagecreatetruecolor"))
{
@$ni = ImageCreateTrueColor($newWidth,$newHeight);
if($ni)
{
ImageCopyResampled($ni,$im,0,0,0,0,$newWidth,$newHeight,$destW,$destH);
}
else
{
$ni=ImageCreate($newWidth,$newHeight);
ImageCopyResized($ni,$im,0,0,0,0,$newWidth,$newHeight,$destW,$destH);
}
}
else
{
$ni=ImageCreate($newWidth,$newHeight);
ImageCopyResized($ni,$im,0,0,0,0,$newWidth,$newHeight,$destW,$destH);
}
if(function_exists('imagejpeg')) ImageJpeg($ni,$newName);
else ImagePNG($ni,$newName);
ImageDestroy($ni);
}
ImageDestroy($im);
}resizeImg('123.JPG',200,400,"test.jpg");?>
<img src="test.jpg" />
http://www.dayanmei.com/download.php?filename=resizeimage.rar
可以按比例生成缩略图
<?phpdefine("GIF",1);define("JPG",2);define("PNG",3);/**$src_file sourse file$dest_file dest file$new_size array('width'=>?,'height'=?)$quantity the quantity of desc$method gd1.0 or gd2.0**/function ResizeImage($src_file="", $dest_file="", $new_size=array('width'=>100,'height'=>78), $quantity="80",$method="gd2"){ //get infomation of iamge /* 0 => width 1 => height 2 => 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM 3 => height="yyy" width="xxx" */ $imginfo = getimagesize($src_file); if ($imginfo == null) return false; // GD can only handle JPG & PNG images if ($imginfo[2] != JPG && $imginfo[2] != PNG && $inginfo[2] != GIF && ($method == 'gd1' || $method == 'gd2')){ die("file is not be supported"); return false; } $srcWidth = $imginfo[0]; $srcHeight = $imginfo[1]; $radio=max(($srcWidth/$new_size['width']),($srcHeight/$new_size['height'])); $destWidth=(int)$srcWidth/$radio; $destHeight=(int)$srcHeight/$radio; switch ($method) { case "gd1" : if (!function_exists('imagecreatefromjpeg')) { die("PHP running on your server does not support the GD image library"); } if ($imginfo[2] == JPG) $src_img = imagecreatefromjpeg($src_file); elseif($imginfo[2] == PNG) $src_img = imagecreatefrompng($src_file); else $src_img = imagecreatefromgif($src_file); if (!$src_img){ return false; } $dst_img = imagecreate($destWidth, $destHeight); imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight); imagejpeg($dst_img, $dest_file, $quantity); imagedestroy($src_img); imagedestroy($dst_img); break; case "gd2" : // check if support GD2 if (!function_exists('imagecreatefromjpeg')) { die("PHP running on your server does not support the GD image library"); } if (!function_exists('imagecreatetruecolor')) { die("PHP running on your server does not support GD version 2.x, please change to GD version 1.x on your method"); } if ($imginfo[2] == JPG) $src_img = imagecreatefromjpeg($src_file); elseif($imginfo[2] == PNG) $src_img = imagecreatefrompng($src_file); else $src_img = imagecreatefromgif($src_file); if (!$src_img){ return false; } $dst_img = imagecreatetruecolor($destWidth, $destHeight); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight); imagejpeg($dst_img, $dest_file, $quantity); imagedestroy($src_img); imagedestroy($dst_img); break; } // Set mode of uploaded picture chmod($dest_file, 0755); // We check that the image is valid $imginfo = @getimagesize($dest_file); if ($imginfo == null){ @unlink($dest_file); return false; } else { return true; }}?>
嗯,汗一个,写得太草率了.
if($Rate > $srcH /$newHeight) {
$Rate = $srcH /$newHeight;
$destH = $newHeight; //这里写错了,改成 $destH = $srcH; 就可以了.
}
if($dst_width <1 || $dst_height <1) {
echo "params width or height error !";
exit();
}
if(!file_exists($src_file)) {
echo $src_file . " is not exists !";
exit();
} $type=exif_imagetype($src_file);
$support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF); if(!in_array($type, $support_type,true)) {
echo "this type of image does not support! only support jpg , gif or png";
exit();
} switch($type) {
case IMAGETYPE_JPEG :
$src_img=imagecreatefromjpeg($src_file);
break;
case IMAGETYPE_PNG :
$src_img=imagecreatefrompng($src_file);
break;
case IMAGETYPE_GIF :
$src_img=imagecreatefromgif($src_file);
break;
default:
echo "Load image error!";
exit();
}
$src_w=imagesx($src_img);
$src_h=imagesy($src_img);
$ratio_w=1.0 * $dst_width/$src_w;
$ratio_h=1.0 * $dst_height/$src_h;
if ($src_w<=$dst_width && $src_h<=$dst_height) {
$x = ($dst_width-$src_w)/2;
$y = ($dst_height-$src_h)/2;
$new_img=imagecreatetruecolor($dst_width,$dst_height);
imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
} else {
$dstwh = $dst_width/$dst_height;
$srcwh = $src_w/$src_h;
if ($ratio_w <= $ratio_h) {
$zoom_w = $dst_width;
$zoom_h = $zoom_w*($src_h/$src_w);
} else {
$zoom_h = $dst_height;
$zoom_w = $zoom_h*($src_w/$src_h);
} $zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
$new_img=imagecreatetruecolor($dst_width,$dst_height);
$x = ($dst_width-$zoom_w)/2;
$y = ($dst_height-$zoom_h)/2+1;
imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
}
}