PHP缩略图生成程序源代码

  核心提示:一个非常简单的PHP生成缩略图的代码程序,参数及代码都算得上精简,有兴趣的朋友可以试下它的功能,有不太完善的地方还请指正。?$FILENAME='image_name';// 生成图片的宽度$RESIZEWIDTH=400;// 生成图片的高度$RESIZEHEIGHT=400;function R…

itE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(34,34,34); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px">一个非常简单的PHP生成缩略图的代码程序,参数及代码都算得上精简,有兴趣的朋友可以试下它的功能,有不太完善的地方还请指正。

 

<?

$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); www~phperz~com

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>

未经允许不得转载:445IT之家 » PHP缩略图生成程序源代码

赞 (0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏