php图片上传并生成缩略图效果

本教程是一款php图片上传然后,把上传的图片生成小图片,是一款非常好的文件上传类,如果你正在找类程序可以进来看看.

php图片上传并生成缩略图,实例代码如下:

  1. function uploadimage($upname,$smallmark=1,$dstsw,$dstsh=0,$path_dim,$path_xim,$newname,$smallname=0,$filetype="null") {
  2. global $webaddr,$_files,$my;
  3. $phpv=str_replace('.', '', php_version);
  4. $filename=$upname;
  5. $max_file_size = 2147483648; //上传文件大小限制, 单位byte 2m
  6. $path_im = $path_dim; //生成大图保存文件夹路径
  7. $path_sim = $path_xim; //缩略图保存文件夹路径
  8. $simclearly=75;
  9. $simclearlypng =$phpv>=512?7:75; //缩略图清晰度0-100,数字越大越清晰,文件尺寸越大
  10. $smallmark = $smallmark; //是否生成缩略图(1为加生成,其他为不);
  11. $dst_sw =$dstsw; //定义缩略图宽度,高度我采用等比例缩放,所以只要比较宽度就可以了
  12. $uptypes=array(
  13. 'image/jpg',
  14. 'image/jpeg',
  15. 'image/png',
  16. 'image/pjpeg',
  17. 'image/gif',
  18. 'image/bmp',
  19. 'image/x-png'
  20. );
  21. if (!is_uploaded_file($_files[$filename][tmp_name])) {
  22. dsetcookie('setok','upload1');
  23. header("location:111cn.net/profile");
  24. exit;
  25. }
  26. $file = $_files[$filename];
  27. $pinfo = pathinfo($file["name"]);
  28. if ($filetype=="null") {
  29. $filetype = $pinfo['extension'];
  30. }
  31. if (!in_array(strtolower($pinfo['extension']),array("jpg","jpeg","png","gif"))) {
  32. dsetcookie('setok','upload3');
  33. header("location:111cn.net/profile");
  34. exit;
  35. }
  36. if($max_file_size < $file["size"]) {//检查文件大小
  37. dsetcookie('setok','upload2');
  38. header("location:111cn.net/profile");
  39. exit;
  40. }
  41. if(!in_array($file["type"],$uptypes)) { //检查文件类型
  42. dsetcookie('setok','upload3');
  43. header("location:111cn.net/profile");
  44. exit;
  45. }
  46. if(!file_exists($path_im)) {
  47. mkdir($path_im);
  48. }
  49. $filename = $file["tmp_name"];
  50. $im_size = getimagesize($filename);
  51. $src_w = $im_size[0];
  52. $src_h = $im_size[1];
  53. $src_type = $im_size[2];
  54. $all_path = $path_im.$newname.".".$filetype;//路径+文件名,目前以上传时间命名
  55. if (file_exists($all_path)) {
  56. @unlink($all_path);
  57. }
  58. if(!move_uploaded_file ($filename,$all_path)) {
  59. dsetcookie('setok','upload4');
  60. header("location:111cn.net/profile");
  61. exit;
  62. }
  63. $pinfo = pathinfo($all_path);
  64. $fname = $pinfo[basename];
  65. switch($src_type) {//判断源图片文件类型
  66. case 1://gif
  67. $src_im = @imagecreatefromgif($all_path);//从源图片文件取得图像
  68. break;
  69. case 2://jpg
  70. $src_im = @imagecreatefromjpeg($all_path);
  71. break;
  72. case 3://png
  73. $src_im = @imagecreatefrompng($all_path);
  74. break;
  75. //case 6:
  76. //$src_im=imagecreatefromwbmp($all_path);
  77. //break;
  78. default:
  79. dsetcookie('setok','upload3');
  80. header("location:111cn.net/profile");
  81. exit;
  82. }
  83. if($smallmark == 1) {
  84. if(!file_exists($path_sim)) {//检查缩略图目录是否存在,不存在创建
  85. mkdir($path_sim);
  86. }
  87. if ($smallname) $newname=$smallname;
  88. $sall_path = $path_sim.$newname.".".$filetype;
  89. if (file_exists($sall_path)) {
  90. @unlink($sall_path);
  91. }
  92. if($src_w <= $dst_sw) { // 原图尺寸 <= 缩略图尺寸
  93. if ($dstsh==0) {
  94. $dst_sim = @imagecreatetruecolor($src_w,$src_h); //新建缩略图真彩位图
  95. $sx=$sy=0;
  96. } else {
  97. $dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建缩略图真彩位图
  98. $sx=($dstsw-$src_w)/2;
  99. $sy=($dstsh-$src_h)/2;
  100. }
  101. $img = @imagecreatefrompng("images/phbg.png");
  102. @imagecopymerge($dst_sim,$img,0,0,0,0,$dstsw,$dstsh,100); //原图图像写入新建真彩位图中
  103. @imagecopymerge($dst_sim,$src_im,$sx,$sy,0,0,$src_w,$src_h,100); //原图图像写入新建真彩位图中
  104. }
  105. if($src_w > $dst_sw) { // 原图尺寸 > 缩略图尺寸
  106. $dst_sh = $dst_sw/$src_w*$src_h;
  107. if ($dst_sh<$dstsh) {
  108. $dst_sh=$dstsh;
  109. $dst_sw=$dst_sh/$src_h*$src_w;
  110. }
  111. if ($dstsh==0) {
  112. $dst_sim = @imagecreatetruecolor($dst_sw,$dst_sh); //新建缩略图真彩位图(等比例缩小原图尺寸)
  113. } else {
  114. $dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建缩略图真彩位图(等比例缩小原图尺寸)
  115. }
  116. @imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //原图图像写入新建真彩位图中
  117. }
  118. switch($src_type) {
  119. case 1:@imagegif($dst_sim,$sall_path,$simclearly);//生成gif文件,图片清晰度0-100
  120. break;
  121. case 2:@imagejpeg($dst_sim,$sall_path,$simclearly);//生成jpg文件,图片清晰度0-100
  122. break;
  123. case 3:@imagepng($dst_sim,$sall_path,$simclearlypng);//生成png文件,图片清晰度0-100
  124. break;
  125. //case 6:
  126. //imagewbmp($dst_sim,$sall_path);
  127. break;
  128. }//开源代码phpfensi.com
  129. //释放缓存
  130. @imagedestroy($dst_sim);
  131. }
  132. @imagedestroy($src_im);
  133. return $newname.".".$filetype;
  134. }