Board logo

标题: [推荐]看看这个php文件能给你什么启示 [打印本页]

作者: cnangel    时间: 2003-9-7 15:48     标题: [推荐]看看这个php文件能给你什么启示

  1. <?
  2. $FILENAME="image_name";
  3. // 生成图片的宽度
  4. $RESIZEWIDTH=400;
  5. // 生成图片的高度
  6. $RESIZEHEIGHT=400;
  7. function ResizeImage($im,$maxwidth,$maxheight,$name){
  8.     $width = imagesx($im);
  9.     $height = imagesy($im);
  10.     if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  11.         if($maxwidth && $width > $maxwidth){
  12.             $widthratio = $maxwidth/$width;
  13.             $RESIZEWIDTH=true;
  14.         }
  15.         if($maxheight && $height > $maxheight){
  16.             $heightratio = $maxheight/$height;
  17.             $RESIZEHEIGHT=true;
  18.         }
  19.         if($RESIZEWIDTH && $RESIZEHEIGHT){
  20.             if($widthratio < $heightratio){
  21.                 $ratio = $widthratio;
  22.             }else{
  23.                 $ratio = $heightratio;
  24.             }
  25.         }elseif($RESIZEWIDTH){
  26.             $ratio = $widthratio;
  27.         }elseif($RESIZEHEIGHT){
  28.             $ratio = $heightratio;
  29.         }
  30.         $newwidth = $width * $ratio;
  31.         $newheight = $height * $ratio;
  32.         if(function_exists("imagecopyresampled")){
  33.               $newim = imagecreatetruecolor($newwidth, $newheight);
  34.               imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  35.         }else{
  36.             $newim = imagecreate($newwidth, $newheight);
  37.               imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  38.         }
  39.         ImageJpeg ($newim,$name . ".jpg");
  40.         ImageDestroy ($newim);
  41.     }else{
  42.         ImageJpeg ($im,$name . ".jpg");
  43.     }
  44. }
  45. if($_FILES['image']['size']){
  46.     if($_FILES['image']['type'] == "image/pjpeg"){
  47.         $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  48.     }elseif($_FILES['image']['type'] == "image/x-png"){
  49.         $im = imagecreatefrompng($_FILES['image']['tmp_name']);
  50.     }elseif($_FILES['image']['type'] == "image/gif"){
  51.         $im = imagecreatefromgif($_FILES['image']['tmp_name']);
  52.     }
  53.     if($im){
  54.         if(file_exists("$FILENAME.jpg")){
  55.             unlink("$FILENAME.jpg");
  56.         }
  57.         ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
  58.         ImageDestroy ($im);
  59.     }
  60. }
  61. ?>
  62. <img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>
  63. <form enctype="multipart/form-data" method="post">
  64. <br>
  65. <input type="file" name="image" size="50" value="浏览"><p>
  66. <input type="submit" value="上传图片">
  67. </form>  
  68. </body>
  69. </html>
复制代码





欢迎光临 星星博客 (http://commerce.huhoo.net/) Powered by Discuz! 7.0.0