把上传好的图片,读取出来的时候是大小一样的缩略图显示,要怎么写?

解决方案 »

  1.   


    <?php
    /*构造函数-生成缩略图+水印,参数说明:
    $srcFile-图片文件名,
    $dstFile-另存文件名,
    $words-水印文字,
    $image-水印图片,
    $dstW-图片保存宽度,
    $dstH-图片保存高度,
    $rate-图片保存品质*/ 
    makethumb("a.jpg","b.jpg","50","50");
    function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$words=null,$image=null) 

    $data = GetImageSize($srcFile); 
    switch($data[2]) 

    case 1: 
    $im=@ImageCreateFromGIF($srcFile); 
    break; 
    case 2: 
    $im=@ImageCreateFromJPEG($srcFile); 
    break; 
    case 3: 
    $im=@ImageCreateFromPNG($srcFile); 
    break; 

    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=ImageCreateTrueColor($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; 
    $white = ImageColorAllocate($ni,255,255,255); 
    $black = ImageColorAllocate($ni,0,0,0); 
    imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色 
    ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH); 
    if($words!=null) 

    $words=iconv("gb2312","UTF-8",$words); 
    //转换文字编码 
    ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$words); //写入文字水印 
    //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容 

    elseif($image!=null) 

    $wimage_data = GetImageSize($image); 
    switch($wimage_data[2]) 

    case 1: 
    $wimage=@ImageCreateFromGIF($image); 
    break; 
    case 2: 
    $wimage=@ImageCreateFromJPEG($image); 
    break; 
    case 3: 
    $wimage=@ImageCreateFromPNG($image); 
    break; 

    imagecopy($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31 
    imagedestroy($wimage); 

    ImageJpeg($ni,$dstFile,$rate); 
    ImageJpeg($ni,$srcFile,$rate); 
    imagedestroy($im); 
    imagedestroy($ni); 

    ?> 
      

  2.   

    GD库弄好了。直接在PHP页面调用这个类就行了吗?我现在页面读出来的是大的原图。
      

  3.   

    对,在php.ini 配置文件中 把 gd2 的配置打开,就可以了,然后上网搜 php 缩略图, 很多的,就可以用了,写个方法,调用就 OK 了
      

  4.   

    <?php 
    /* 
    *        $o_photo 原图路径 
    *        $d_photo 处理后图片路径 
    *        $width   定义宽 
    *        $height  定义高 
    *        调用方法  cutphoto("test.jpg","temp.jpg",256,146); 
    */ function cutphoto($o_photo,$d_photo,$width,$height){ $temp_img = imagecreatefromjpeg($o_photo); 
    $o_width  = imagesx($temp_img);                                //取得原图宽 
    $o_height = imagesy($temp_img);                                //取得原图高 //判断处理方法 
    if($width>$o_width || $height>$o_height){        //原图宽或高比规定的尺寸小,进行压缩         $newwidth=$o_width; 
            $newheight=$o_height;         if($o_width>$width){ 
                    $newwidth=$width; 
                    $newheight=$o_height*$width/$o_width; 
            }         if($newheight>$height){ 
                    $newwidth=$newwidth*$height/$newheight; 
                    $newheight=$height; 
            }         //缩略图片 
            $new_img = imagecreatetruecolor($newwidth, $newheight);  
            imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);  
            imagejpeg($new_img , $d_photo);                 
            imagedestroy($new_img); 
    }else{                                                                                //原图宽与高都比规定尺寸大,进行压缩后裁剪         if($o_height*$width/$o_width>$height){        //先确定width与规定相同,如果height比规定大,则ok 
                    $newwidth=$width; 
                    $newheight=$o_height*$width/$o_width; 
                    $x=0; 
                    $y=($newheight-$height)/2; 
            }else{                                                                        //否则确定height与规定相同,width自适应 
                    $newwidth=$o_width*$height/$o_height; 
                    $newheight=$height; 
                    $x=($newwidth-$width)/2; 
                    $y=0; 
            }         //缩略图片 
            $new_img = imagecreatetruecolor($newwidth, $newheight);  
            imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);  
            imagejpeg($new_img , $d_photo);                 
            imagedestroy($new_img); 
             
            $temp_img = imagecreatefromjpeg($d_photo); 
            $o_width  = imagesx($temp_img);                                //取得缩略图宽 
            $o_height = imagesy($temp_img);                                //取得缩略图高         //裁剪图片 
            $new_imgx = imagecreatetruecolor($width,$height); 
            imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height); 
            imagejpeg($new_imgx , $d_photo); 
            imagedestroy($new_imgx); 
    } } 
    ?>找了1个源码,但是使用的时候没有生成新的图片是怎么回事?
    我是这样套用的:
    $filepath  = "/var/www/html/test/images/" . $_FILES["file"]["name"];
    $smallpath = "/var/www/html/test/images/small/" . $_FILES["file"]["name"];
    cutphoto($filepath,$smallpath,100,100);