上传图片时如何自动生成缩略图? 我看了这个页面http://expert.csdn.net/Expert/topic/2064/2064094.xml?temp=8.533877E-02测试报错:Fatal error: Call to undefined function: imagecreatefromjpeg() 是不是先要装个什么东东吧,求教! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //---------------------------------------------------------------//自动生成图片缩略图 // 本函数从源文件取出图象,设定成指定大小,并输出到目的文件 // 源文件格式:gif,jpg,jpe,jpeg,png // 目的文件格式:jpg // 参数说明: // 使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);// $srcFile 源文件 // $dstFile 目标文件 // $dstW 目标图象宽度 // $dstH 目标图象高度 //-----------------------------------------------------------------*/function makethumb($srcFile,$dstFile,$dstW,$dstH) { $data = GetImageSize($srcFile,&$info); switch ($data[2]) { case 1: $im = @ImageCreateFromGIF($srcFile); //从目标图片中取出图形 break; case 2: $im = @imagecreatefromjpeg($srcFile); break; case 3: $im = @ImageCreateFromPNG($srcFile); break; } $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);//如果你要把图片直接输出到浏览器, //那么把第二个参数去掉,并用header()函数指定mine类型先 imagedestroy($im); imagedestroy($ni);}例子:/*---------------------------------------------------- 功能:自动生成图片缩略图使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);$srcFile 源文件 $dstFile 目标文件 $dstW 目标图象宽度 $dstH 目标图象高度 ----------------------------------------------------if(!empty($up_file)){$srcFile=$up_file;$dstFile="companys_images/$account/small_images/$imgname";$dstW="150";$dstH="100";makethumb($srcFile,$dstFile,$dstW,$dstH);//echo "$srcFile<br>";//echo $dstFile;}*//*else{ // 如果没有上传文件,则图片名为空 $filename=''; echo "你还没上传图片";}*/ yykforever(月如钩)你的机子装了gd库了没有? to : xinyaoxp(henonybee) gif可以嘛? //---------------------------------------------------------------//自动生成图片缩略图 // 本函数从源文件取出图象,设定成指定大小,并输出到目的文件 // 源文件格式:gif,jpg,jpe,jpeg,png // 目的文件格式:jpg // 参数说明: // 使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);// $srcFile 源文件 // $dstFile 目标文件 // $dstW 目标图象宽度 // $dstH 目标图象高度 //-----------------------------------------------------------------*/function makethumb($srcFile,$dstFile,$dstW,$dstH) { $data = GetImageSize($srcFile,&$info); switch ($data[2]) { case 1: $im = @ImageCreateFromGIF($srcFile); //从目标图片中取出图形 break; case 2: $im = @imagecreatefromjpeg($srcFile); break; case 3: $im = @ImageCreateFromPNG($srcFile); break; } $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);//如果你要把图片直接输出到浏览器, //那么把第二个参数去掉,并用header()函数指定mine类型先 imagedestroy($im); imagedestroy($ni);}例子:/*---------------------------------------------------- 功能:自动生成图片缩略图使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);$srcFile 源文件 $dstFile 目标文件 $dstW 目标图象宽度 $dstH 目标图象高度 ----------------------------------------------------if(!empty($up_file)){$srcFile=$up_file;$dstFile="companys_images/$account/small_images/$imgname";$dstW="150";$dstH="100";makethumb($srcFile,$dstFile,$dstW,$dstH);//echo "$srcFile<br>";//echo $dstFile;}*//*else{ // 如果没有上传文件,则图片名为空 $filename=''; echo "你还没上传图片";}*/ 楼主,去这里看看吧,也许对你会有所帮助http://www.phpx.com/happy/showthread.php?s=&threadid=1718 function ResizeImage($image,$newname){ global $etn_config; $maxwidth = $etn_config['resize_width']; $maxheight = $etn_config['resize_height']; $name = $etn_config['uploadpath'].$newname; $width = imagesx($image); $height = imagesy($image); if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){ if($maxwidth && $width > $maxwidth){ $widthratio = $maxwidth/$width; $reWidth=true; } if($maxheight && $height > $maxheight){ $heightratio = $maxheight/$height; $reHeight=true; } if($reWidth && $reHeight){ if($widthratio < $heightratio){ $ratio = $widthratio; }else{ $ratio = $heightratio; } }elseif($reWidth){ $ratio = $widthratio; }elseif($reHeight){ $ratio = $heightratio; } $newwidth = $width * $ratio; $newheight = $height * $ratio; if(function_exists("imagecopyresampled")){ //this function needs GD 2.0.1 or later. $newim = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($newim, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); }else{ $newim = imagecreate($newwidth, $newheight); imagecopyresized($newim, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); } ImageJpeg ($newim,$name); ImageDestroy ($newim); }else{ ImageJpeg ($image,$name); }} 到www.btph.com看看,这个生成缩略图用的是ImageMagic http://www.51base.com/article/view_article.asp?id=20159http://www.51base.com/article/view_article.asp?id=3594 http://www.51base.com/article/view_article.asp?id=25618 uphttp://www.51base.com/article/view_article.asp?id=25618 出现phpMyAdmin - 错误,无法加载 mysqli (外链,英语) 扩展,请检查您的 PHP 配置 - 文档的几个成功解决方法 Ajax对象向服务器发送数据请求的问题 新手求解答。 关于读取网页的内容? MYSQL 和 MS SQLSERVER是否冲突问题! 请教php正则基础问题 乱码问题,高手请进 smtp.lib.php发邮件的问题 PHP中的页面跳转使用哪个函数? windy2000 - Upload Manager (PHP Class) 如何删除多维数组中的一组数组 如何用PHP进行分页!!! 问个问题
//---------------------------------------------------------------
//自动生成图片缩略图
// 本函数从源文件取出图象,设定成指定大小,并输出到目的文件
// 源文件格式:gif,jpg,jpe,jpeg,png
// 目的文件格式:jpg
// 参数说明:
// 使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);
// $srcFile 源文件
// $dstFile 目标文件
// $dstW 目标图象宽度
// $dstH 目标图象高度
//-----------------------------------------------------------------
*/
function makethumb($srcFile,$dstFile,$dstW,$dstH) {
$data = GetImageSize($srcFile,&$info);
switch ($data[2]) {
case 1:
$im = @ImageCreateFromGIF($srcFile); //从目标图片中取出图形
break;
case 2:
$im = @imagecreatefromjpeg($srcFile);
break;
case 3:
$im = @ImageCreateFromPNG($srcFile);
break;
}
$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);//如果你要把图片直接输出到浏览器,
//那么把第二个参数去掉,并用header()函数指定mine类型先
imagedestroy($im);
imagedestroy($ni);
}
例子:
/*
----------------------------------------------------
功能:自动生成图片缩略图
使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);
$srcFile 源文件
$dstFile 目标文件
$dstW 目标图象宽度
$dstH 目标图象高度 ----------------------------------------------------if(!empty($up_file))
{
$srcFile=$up_file;
$dstFile="companys_images/$account/small_images/$imgname";
$dstW="150";
$dstH="100";
makethumb($srcFile,$dstFile,$dstW,$dstH);
//echo "$srcFile<br>";
//echo $dstFile;
}*/
/*
else
{
// 如果没有上传文件,则图片名为空
$filename='';
echo "你还没上传图片";
}
*/
//自动生成图片缩略图
// 本函数从源文件取出图象,设定成指定大小,并输出到目的文件
// 源文件格式:gif,jpg,jpe,jpeg,png
// 目的文件格式:jpg
// 参数说明:
// 使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);
// $srcFile 源文件
// $dstFile 目标文件
// $dstW 目标图象宽度
// $dstH 目标图象高度
//-----------------------------------------------------------------
*/
function makethumb($srcFile,$dstFile,$dstW,$dstH) {
$data = GetImageSize($srcFile,&$info);
switch ($data[2]) {
case 1:
$im = @ImageCreateFromGIF($srcFile); //从目标图片中取出图形
break;
case 2:
$im = @imagecreatefromjpeg($srcFile);
break;
case 3:
$im = @ImageCreateFromPNG($srcFile);
break;
}
$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);//如果你要把图片直接输出到浏览器,
//那么把第二个参数去掉,并用header()函数指定mine类型先
imagedestroy($im);
imagedestroy($ni);
}
例子:
/*
----------------------------------------------------
功能:自动生成图片缩略图
使用示例:makethumb($srcFile,$dstFile,$dstW,$dstH);
$srcFile 源文件
$dstFile 目标文件
$dstW 目标图象宽度
$dstH 目标图象高度 ----------------------------------------------------if(!empty($up_file))
{
$srcFile=$up_file;
$dstFile="companys_images/$account/small_images/$imgname";
$dstW="150";
$dstH="100";
makethumb($srcFile,$dstFile,$dstW,$dstH);
//echo "$srcFile<br>";
//echo $dstFile;
}*/
/*
else
{
// 如果没有上传文件,则图片名为空
$filename='';
echo "你还没上传图片";
}
*/
http://www.phpx.com/happy/showthread.php?s=&threadid=1718
{
global $etn_config; $maxwidth = $etn_config['resize_width'];
$maxheight = $etn_config['resize_height'];
$name = $etn_config['uploadpath'].$newname;
$width = imagesx($image);
$height = imagesy($image); if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$reWidth=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$reHeight=true;
}
if($reWidth && $reHeight){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($reWidth){
$ratio = $widthratio;
}elseif($reHeight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio; if(function_exists("imagecopyresampled")){ //this function needs GD 2.0.1 or later.
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name);
ImageDestroy ($newim);
}else{
ImageJpeg ($image,$name);
}
}