Chinaunix

标题: php制作缩略图图片 [打印本页]

作者: 三里屯摇滚    时间: 2011-12-20 22:42
标题: php制作缩略图图片

php制作缩略图图片










Php代码
  1. 1.<?php  
  2. 2.if (! isset ( $_POST ["submit"] )) {  
  3. 3.    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"";  
  4. 4.} else {  
  5. 5.    if (isset ( $_FILES ["file"] ) || $_POST ["file"] != "") {  
  6. 6.        function resizeimage($file, $rate = .5) {  
  7. 7.            $size = getimagesize ( $file );  
  8. 8.            switch ($size [2]) {  
  9. 9.                case 1 :  
  10. 10.                    $img = imagecreatefromgif ( $file );  
  11. 11.                    break;  
  12. 12.                case 2 :  
  13. 13.                    $img = imagecreatefromjpeg ( $file );  
  14. 14.                    break;  
  15. 15.                case 3 :  
  16. 16.                    $img = imagecreatefrompng ( $file );  
  17. 17.                    break;  
  18. 18.            }  
  19. 19.            $srcw = imagesx ( $img );  
  20. 20.            $srch = imagesy ( $img );  
  21. 21.            $detw = floor ( $srcw * $rate );  
  22. 22.            $deth = floor ( $srch * $rate );  
  23. 23.            $im = imagecreatetruecolor ( $detw, $deth );  
  24. 24.            $black = imagecolorallocate ( $im, 255, 255, 255 );  
  25. 25.            imagefilledrectangle ( $im, 0, 0, $detw, $deth, $black );  
  26. 26.            imagecopyresized ( $im, $img, 0, 0, 0, 0, $detw, $deth, $srcw, $srch );  
  27. 27.            header ( 'Content-type:image/png' );  
  28. 28.            imagepng ( $im );  
  29. 29.            imagedestroy ( $im );  
  30. 30.            imagedestroy ( $img );  
  31. 31.        }  
  32. 32.        resizeimage ( $_FILES ["file"] ["tmp_name"] );  
  33. 33.    }  
  34. 34.}  
  35. 35.?>  
  36. 36.  
  37. 37.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  38. 38.<html xmlns="http://www.w3.org/1999/xhtml">  
  39. 39.<head>  
  40. 40.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  41. 41.<title>Insert title here</title>  
  42. 42.<style type="text/css">  
  43. 43.td {  
  44. 44.    margin: 0;  
  45. 45.}  
  46. 46.</style>  
  47. 47.<script type="text/javascript">  
  48. 48.function submit(){  
  49. 49.    obj=document.form1.submit();  
  50. 50.    return obj;  
  51. 51.}  
  52. 52.</script>  
  53. 53.</head>  
  54. 54.<body>  
  55. 55.<form name="form1" method="post" enctype="multipart/form-data"  
  56. 56.    action="<?php  
  57. 57.    echo $_SERVER ["PHP_SELF"]?>">  
  58. 58.<table cellpadding="0" cellspacing="0" border="0"  
  59. 59.    bordercolordark="#ffffff" bordercolorlight="#0000ff">  
  60. 60.    <tr>  
  61. 61.        <td><label>FileUpload : </label></td>  
  62. 62.        <td><input type="file" name="file"></input></td>  
  63. 63.        <td><input type="submit" name="submit" value="Submit"></input></td>  
  64. 64.    </tr>  
  65. 65.</table>  
  66. 66.</form>  
  67. 67.</body>  
  68. 68.</html>  
复制代码

作者: 我是软件狂    时间: 2011-12-21 21:53
学习鸟 谢谢分享




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2