最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php生成缩略图代码
时间:2008-03-28 编辑:简简单单 来源:一聚教程网
/**
* 生成缩略图
* $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);
$newWidthH=$newWidth/$newHeight;
$srcWH=$srcW/$srcH;
if($newWidthH<=$srcWH){
$ftoW=$newWidth;
$ftoH=$ftoW*($srcH/$srcW);
}
else{
$ftoH=$newHeight;
$ftoW=$ftoH*($srcW/$srcH);
}
if($srcW>$newWidth||$srcH>$newHeight)
{
if(function_exists("imagecreatetruecolor"))
{
@$ni = ImageCreateTrueColor($ftoW,$ftoH);
if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
else
{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
}
else
{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
if(function_exists('imagejpeg')) ImageJpeg($ni,$newName);
else ImagePNG($ni,$newName);
ImageDestroy($ni);
}
ImageDestroy($im);
}
resizeImg('123.JPG',150,150);
?>
-
上一个: php+mysql图片输入输出
-
下一个: 不定数量表单生成缩略较长
相关文章
- php生成缩略图的例子 05-08
- php利用Imagick把pdf生成png缩略图 07-02
- php缩略图生成几种方法代码 06-24
- 一款php批量生成缩略图代码 02-11
- 一款php按比例生成缩略图代码 09-30
- php生成小图_php 生成缩略图代码 08-16