当前位置:首页 > PHP教程 > php图像处理 > 列表

php上传图片生成等比例缩略图代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-18 15:26:58 浏览: 评论:0 

一个简单的使用php上传图片文件生然后再生成一张等比例的缩略图效果,有需要学习的朋友可参考参考,实例代码如下:

  1. <?php  
  2. function _UPLOADPIC($upfile, $maxsize, $updir, $newname = 'date') { 
  3.  
  4. if ($newname == 'date') 
  5.  
  6. $newname = date ( "Ymdhis" ); //使用日期做文件名  
  7.  
  8. $name = $upfile ["name"]; 
  9.  
  10. $type = $upfile ["type"]; 
  11.  
  12. $size = $upfile ["size"]; 
  13.  
  14. $tmp_name = $upfile ["tmp_name"]; 
  15.  
  16. switch ($type) { 
  17.  
  18. case 'image/pjpeg' : 
  19.  
  20. case 'image/jpeg' : 
  21.  
  22. $extend = ".jpg"; 
  23.  
  24. break; 
  25.  
  26. case 'image/gif' : 
  27.  
  28. $extend = ".gif"; 
  29.  
  30. break; 
  31.  
  32. case 'image/png' : 
  33.  
  34. $extend = ".png"; 
  35.  
  36. break; 
  37.  
  38. } 
  39.  
  40. if (emptyempty ( $extend )) { 
  41.  
  42. echo ( "警告!只能上传图片类型:GIF JPG PNG" ); 
  43.  
  44. exit (); 
  45.  
  46. } 
  47.  
  48. if ($size > $maxsize) { 
  49.  
  50. $maxpr = $maxsize / 1000; 
  51.  
  52. echo ( "警告!上传图片大小不能超过" . $maxpr . "K!" ); 
  53.  
  54. exit (); 
  55.  
  56. } 
  57.  
  58. if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) { 
  59.  
  60. return $updir . $newname . $extend; 
  61.  
  62. } 
  63.  
  64. } 
  65.  
  66.   
  67.  
  68. function show_pic_scal($width, $height, $picpath) { 
  69.  
  70. $imginfo = GetImageSize ( $picpath ); 
  71.  
  72. $imgw = $imginfo [0]; 
  73.  
  74. $imgh = $imginfo [1]; 
  75.  
  76.   
  77.  
  78. $ra = number_format ( ($imgw / $imgh), 1 ); //宽高比 
  79.  
  80. $ra2 = number_format ( ($imgh / $imgw), 1 ); //高宽比 
  81.  
  82.   
  83.  
  84.   
  85.  
  86. if ($imgw > $width or $imgh > $height) { 
  87.  
  88. if ($imgw > $imgh) { 
  89.  
  90. $newWidth = $width; 
  91.  
  92. $newHeight = round ( $newWidth / $ra ); 
  93.  
  94.   
  95.  
  96. } elseif ($imgw < $imgh) { 
  97.  
  98. $newHeight = $height; 
  99.  
  100. $newWidth = round ( $newHeight / $ra2 ); 
  101.  
  102. } else { 
  103.  
  104. $newWidth = $width; 
  105.  
  106. $newHeight = round ( $newWidth / $ra ); 
  107.  
  108. } 
  109.  
  110. } else { 
  111.  
  112. $newHeight = $imgh; 
  113.  
  114. $newWidth = $imgw; 
  115.  
  116. } 
  117.  
  118. $newsize [0] = $newWidth; 
  119.  
  120. $newsize [1] = $newHeight; 
  121.  
  122.   
  123.  
  124. return $newsize; 
  125.  
  126. } 
  127.  
  128.   
  129.  
  130.   
  131.  
  132.   
  133.  
  134. function getImageInfo($src) 
  135.  
  136. { 
  137.  
  138. return getimagesize($src); 
  139.  
  140. } 
  141.  
  142. /** 
  143.  
  144. * 创建图片,返回资源类型 
  145.  
  146. * @param string $src 图片路径 
  147.  
  148. * @return resource $im 返回资源类型  
  149.  
  150. * **/  
  151.  
  152. function create($src) 
  153.  
  154. { 
  155.  
  156. $info=getImageInfo($src); 
  157.  
  158. switch ($info[2]) 
  159.  
  160. { 
  161.  
  162. case 1: 
  163.  
  164. $im=imagecreatefromgif($src); 
  165.  
  166. break; 
  167.  
  168. case 2: 
  169.  
  170. $im=imagecreatefromjpeg($src); 
  171.  
  172. break; 
  173.  
  174. case 3: 
  175.  
  176. $im=imagecreatefrompng($src); 
  177.  
  178. break; 
  179.  
  180. } 
  181.  
  182. return $im; 
  183.  
  184. } 
  185.  
  186. /** 
  187.  
  188. * 缩略图主函数 
  189.  
  190. * @param string $src 图片路径 
  191.  
  192. * @param int $w 缩略图宽度 
  193.  
  194. * @param int $h 缩略图高度 
  195.  
  196. * @return mixed 返回缩略图路径 
  197.  
  198. * **/  
  199.  
  200.   
  201.  
  202. function resize($src,$w,$h) 
  203.  
  204. { 
  205.  
  206. $temp=pathinfo($src); 
  207.  
  208. $name=$temp["basename"];//文件名 
  209.  
  210. $dir=$temp["dirname"];//文件所在的文件夹 
  211.  
  212. $extension=$temp["extension"];//文件扩展名 
  213.  
  214. $savepath="{$dir}/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg 
  215.  
  216.   
  217.  
  218. //获取图片的基本信息 
  219.  
  220. $info=getImageInfo($src); 
  221.  
  222. $width=$info[0];//获取图片宽度 
  223.  
  224. $height=$info[1];//获取图片高度 
  225.  
  226. $per1=round($width/$height,2);//计算原图长宽比 
  227.  
  228. $per2=round($w/$h,2);//计算缩略图长宽比 
  229.  
  230.   
  231.  
  232. //计算缩放比例 
  233.  
  234. if($per1>$per2||$per1==$per2) 
  235.  
  236. { 
  237.  
  238. //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先 
  239.  
  240. $per=$w/$width; 
  241.  
  242. } 
  243.  
  244. if($per1<$per2) 
  245.  
  246. { 
  247.  
  248. //原图长宽比小于缩略图长宽比,则按照高度优先 
  249.  
  250. $per=$h/$height; 
  251.  
  252. } 
  253.  
  254. $temp_w=intval($width*$per);//计算原图缩放后的宽度 
  255.  
  256. $temp_h=intval($height*$per);//计算原图缩放后的高度 
  257.  
  258. $temp_img=imagecreatetruecolor($temp_w,$temp_h);//创建画布 
  259.  
  260. $im=create($src); 
  261.  
  262. imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height); 
  263.  
  264. if($per1>$per2) 
  265.  
  266. { 
  267.  
  268. imagejpeg($temp_img,$savepath, 100); 
  269.  
  270. imagedestroy($im); 
  271.  
  272. return addBg($savepath,$w,$h,"w"); 
  273.  
  274. //宽度优先,在缩放之后高度不足的情况下补上背景 
  275.  
  276. } 
  277.  
  278. if($per1==$per2) 
  279.  
  280. { 
  281.  
  282. imagejpeg($temp_img,$savepath, 100); 
  283.  
  284. imagedestroy($im); 
  285.  
  286. return $savepath; 
  287.  
  288. //等比缩放 
  289.  
  290. } 
  291.  
  292. if($per1<$per2) 
  293.  
  294. { 
  295.  
  296. imagejpeg($temp_img,$savepath, 100); 
  297.  
  298. imagedestroy($im); 
  299.  
  300. return addBg($savepath,$w,$h,"h"); 
  301.  
  302. //高度优先,在缩放之后宽度不足的情况下补上背景 
  303.  
  304. } 
  305.  
  306. } 
  307.  
  308. /** 
  309.  
  310. * 添加背景 
  311.  
  312. * @param string $src 图片路径 
  313.  
  314. * @param int $w 背景图像宽度 
  315.  
  316. * @param int $h 背景图像高度 
  317.  
  318. * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比 
  319.  
  320. * @return 返回加上背景的图片 
  321.  
  322. * **/  
  323.  
  324. function addBg($src,$w,$h,$fisrt="w") 
  325.  
  326. { 
  327.  
  328. $bg=imagecreatetruecolor($w,$h); 
  329.  
  330. $white = imagecolorallocate($bg,255,255,255); 
  331.  
  332. imagefill($bg,0,0,$white);//填充背景 
  333.  
  334.   
  335.  
  336. //获取目标图片信息 
  337.  
  338. $info=getImageInfo($src); 
  339.  
  340. $width=$info[0];//目标图片宽度 
  341.  
  342. $height=$info[1];//目标图片高度 
  343.  
  344. $img=create($src); 
  345.  
  346. if($fisrt=="wh") 
  347.  
  348. { 
  349.  
  350. //等比缩放 
  351.  
  352. return $src; 
  353.  
  354. } 
  355.  
  356. else  
  357.  
  358. { 
  359.  
  360. if($fisrt=="w") 
  361.  
  362. { 
  363.  
  364. $x=0; 
  365.  
  366. $y=($h-$height)/2;//垂直居中 
  367.  
  368. } 
  369.  
  370. if($fisrt=="h") 
  371.  
  372. { 
  373.  
  374. $x=($w-$width)/2;//水平居中 
  375.  
  376. $y=0; 
  377.  
  378. } 
  379.  
  380. imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100); 
  381.  
  382. imagejpeg($bg,$src,100); 
  383.  
  384. imagedestroy($bg); 
  385.  
  386. imagedestroy($img); 
  387.  
  388. return $src; 
  389.  
  390. }  
  391. }  
  392. ?>  
  393.  
  394. //使用方法:  
  395. //开源代码phpfensi.com 
  396. $filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date')); 
  397. $show_pic_scal=show_pic_scal(230, 230, $filename); 
  398. resize($filename,$show_pic_scal[0],$show_pic_scal[1]); 

Tags: php上传图片 比例缩略图

分享到: