返回列表 回复 发帖

[转帖]用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>
?>

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表