可以有二种方法,一是直接把原来的大图放上去,在htm中限制图片大小,这种方法不是太好。
还有一种是用gd库。
图片最好不要放在数据库里,特别像新闻系统这种记录 数较多的程序。影响查询速度。用GD库生成高质量的缩略图片<? $FILENAME="image_name"; // 生成图片的宽度 
$RESIZEWIDTH=400; // 生成图片的高度 
$RESIZEHEIGHT=400; 
function ResizeImage($im,$maxwidth,$maxheight,$name){ 
    $width = imagesx($im); 
    $height = imagesy($im); 
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){ 
        if($maxwidth && $width > $maxwidth){ 
            $widthratio = $maxwidth/$width; 
            $RESIZEWIDTH=true; 
        } 
        if($maxheight && $height > $maxheight){ 
            $heightratio = $maxheight/$height; 
            $RESIZEHEIGHT=true; 
        } 
        if($RESIZEWIDTH && $RESIZEHEIGHT){ 
            if($widthratio < $heightratio){ 
                $ratio = $widthratio; 
            }else{ 
                $ratio = $heightratio; 
            } 
        }elseif($RESIZEWIDTH){ 
            $ratio = $widthratio; 
        }elseif($RESIZEHEIGHT){ 
            $ratio = $heightratio; 
        } 
        $newwidth = $width * $ratio; 
        $newheight = $height * $ratio; 
        if(function_exists("imagecopyresampled")){ 
              $newim = imagecreatetruecolor($newwidth, $newheight); 
              imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        }else{ 
            $newim = imagecreate($newwidth, $newheight); 
              imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        } 
        ImageJpeg ($newim,$name . ".jpg"); 
        ImageDestroy ($newim); 
    }else{ 
        ImageJpeg ($im,$name . ".jpg"); 
    } 
} if($_FILES['image']['size']){ 
    if($_FILES['image']['type'] == "image/pjpeg"){ 
        $im = imagecreatefromjpeg($_FILES['image']['tmp_name']); 
    }elseif($_FILES['image']['type'] == "image/x-png"){ 
        $im = imagecreatefrompng($_FILES['image']['tmp_name']); 
    }elseif($_FILES['image']['type'] == "image/gif"){ 
        $im = imagecreatefromgif($_FILES['image']['tmp_name']); 
    } 
    if($im){ 
        if(file_exists("$FILENAME.jpg")){ 
            unlink("$FILENAME.jpg"); 
        } 
        ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME); 
        ImageDestroy ($im); 
    } 
} ?> <img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br> <form enctype="multipart/form-data" method="post"> 
<br> 
<input type="file" name="image" size="50" value="浏览"><p> 
<input type="submit" value="上传图片"> 
</form> </body> 
</html>

解决方案 »

  1.   

    PHP中大图缩小图的程序实例:(在WINDOWS和LINUX平台都调试通过了。)
    1)上传图片页面:(片段)
    在<head></head>中间加入下面程序,(这段程序很有用,可以在前台,用户端判断用户上传的文件类型,控制用户的上传文件)
    <script language="JavaScript">
    <!--
    function CheckForm(theForm)
    {
    var fileext=theForm.zhaopian_up.value.substring(theForm.zhaopian_up.value.length-4,theForm.zhaopian_up.value.length)
    fileext=fileext.toLowerCase()
    if (!(fileext=='.jpg' || fileext=='.png'))
    {alert("对不起,不正确的照片文件,必须为*.jpg或*.png文件 !");
    theForm.zhaopian_up.focus();
    return false;
    }
    return true;
    }
    // -->页面中加入上传表单:
    <FORM ENCTYPE="multipart/form-data" ACTION="photo_upload2.php" METHOD="POST" onsubmit="return CheckForm(this)">
    注:请先上传照片,后填下表。第一张为封面照片,只能传jpg图片,小于300K,少于9张。
    <p class="big"> 上传照片:</p>
    <input type="file" name="zhaopian_up" size="20"> <INPUT TYPE="submit" value="上传">
    </form>2)处理页面程序,包含判断图片大小,上传拷贝,缩图,生成小图文件,图片ID存入数据库等:(片段)
    <?
    //取得上传文件大小,300K:
    if ($zhaopian_up_size>307200) {
    $wrong_massage="对不起,你上传的文件大小超过了300K,请缩小图再传!";
    Header("Location:include/wrong_massage.php?wrong_massage=$wrong_massage");
    exit();
    }
    //自动缩图$srcFile原文件,大图;$photo_small目标文件,小图;$dstW,$dstH是小图的宽,高。
    function makethumb($srcFile,$photo_small,$dstW,$dstH) {
    $data = GetImageSize($srcFile,&$info);
    switch ($data[[2]]) {
    case 1: //图片类型,1是GIF图
    $im = @ImageCreateFromGIF($srcFile);
    break;
    case 2: //图片类型,2是JPG图
    $im = @imagecreatefromjpeg($srcFile);
    break;
    case 3: //图片类型,3是PNG图
    $im = @ImageCreateFromPNG($srcFile);
    break;
    }
    $srcW=ImageSX($im);
    $srcH=ImageSY($im);
    $ni=ImageCreate($dstW,$dstH);
    ImageCopyResized($ni,$im,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
    ImageJpeg($ni,$photo_small);
    //ImageJpeg($ni); //在显示图片时用,把注释取消,可以直接在页面显示出图片。
    }
    $srcFile=$zhaopian_up;
    //取得文件扩展名:
    $type=substr(strrchr($zhaopian_up_name,"."),1);
    $photo_ID=time();
    $photo_path="user_picture/$user/";
    if(is_dir($photo_path)!=TRUE) mkdir($photo_path,0777);
    $photo_small=$photo_path."/".$photo_ID."_s.".$type; //小图
    $photo_s=fopen($photo_small,"w+");
    if ($type=="png" || $type=="jpg") makethumb($srcFile,$photo_small,110,130);//110,130为小图宽,高
    ?>在显示图片的程序中,只需从数据库中读出图片ID,用$photo_path."/".$photo_ID."_s.".$type就是显示小图,加个链接,就可以显示大图$photo_path."/".$photo_ID.".".$type;
      

  2.   

    偶是这样做带图片的新闻系统的:
    类似邮件系统的附件,将图片上传至统一图片文件架并提示出文件名(如15103092373f5d26c4dddd1.JPG)。
    在发表新闻页面,只需要在文章编辑框中预期位置上顶头写上“#attach 15103092373f5d26c4dddd1.JPG”就可以了,生成静态页面时,程序会自己解析出图片路径。这样,就可以在文章中任意定位图片的位置了~!
      

  3.   

    至于数据库,只纪录文章分类和静态页面的文件名。这种做法应该是大多数大型新闻管理系统常用的方法吧~!毕竟跟检索数据库比起来,直接调用html文件还是要快很多啊,特别是想sina那样的新闻站点~!如果新闻量小,把所有数据都放数据库里,也无所谓的~!