下一篇 » « 上一篇

用GD库生成高质量的缩略图片

作者:爱好者    时间:2008-01-22    来源:php之家    点击:1778    本文共1篇文章 字体:[ ]
标签:php与gd

用GD库生成高质量的缩略图片

PHP代码:



<?



$FILENAME
="image_name";



// 生成图片的宽度

$RESIZEWIDTH=400;



// 生成图片的高度

$RESIZEHEIGHT=400;





function
ResizeImage($im,$maxwidth,$maxheight,$name){
php学习之家http://www.444p.com


    
$width = imagesx($im);

    
$height = imagesy($im);

    if((
$maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){

        if(
$maxwidth && $width > $maxwidth){

www.444p.com



            
$widthratio = $maxwidth/$width;

            
$RESIZEWIDTH=true;

        }

        if(
$maxheight && $height > $maxheight){

            
$heightratio = $maxheight/$height;

www.444p.com



            
$RESIZEHEIGHT=true;

        }

        if(
$RESIZEWIDTH && $RESIZEHEIGHT){

            if(
$widthratio < $heightratio){

                
$ratio = $widthratio;

            }else{ www.444p.com php学习之家

                
$ratio = $heightratio;

            }

        }elseif(
$RESIZEWIDTH){

            
$ratio = $widthratio;

        }elseif(
$RESIZEHEIGHT){

            
$ratio = $heightratio;

www.444p.com php学习之家



        }

        
$newwidth = $width * $ratio;

        
$newheight = $height * $ratio;

        if(
function_exists("imagecopyresampled")){

              
$newim = imagecreatetruecolor($newwidth, $newheight);

www.444p.com



              
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        }else{

            
$newim = imagecreate($newwidth, $newheight); php学习之家

              
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        }

        
ImageJpeg ($newim,$name . ".jpg");
php学习之家


        
ImageDestroy ($newim);

    }else{

        
ImageJpeg ($im,$name . ".jpg");

    }

}







if(
$_FILES['image']['size']){

    if(
$_FILES['image']['type'] == "image/pjpeg"){ www.444p.com

        
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);

    }elseif(
$_FILES['image']['type'] == "image/x-png"){

        
$im = imagecreatefrompng($_FILES['image']['tmp_name']); 本文来自 www.444p.com

    }elseif(
$_FILES['image']['type'] == "image/gif"){

        
$im = imagecreatefromgif($_FILES['image']['tmp_name']);

    }

    if(
$im){

        if(
file_exists("$FILENAME.jpg")){
www.444p.com版权所有


            
unlink("$FILENAME.jpg");

        }

        
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);

        
ImageDestroy ($im);

    }

}



?>


本文来自 www.444p.com
<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>




责任编辑:semirock
发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


《PHP精通》点击排行