Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88839
  • 博文数量: 14
  • 博客积分: 454
  • 博客等级: 下士
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 00:49
文章分类

全部博文(14)

文章存档

2012年(14)

我的朋友

分类: 系统运维

2012-02-14 14:45:23

 

  1. $FILENAME="image_name";
  2. // 生成图片的宽度
  3. $RESIZEWIDTH=400;
  4. // 生成图片的高度
  5. $RESIZEHEIGHT=400;
  6. function ResizeImage($im,$maxwidth,$maxheight,$name){
  7. $width = imagesx($im);
  8. $height = imagesy($im);
  9. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  10. if($maxwidth && $width > $maxwidth){
  11. $widthratio = $maxwidth/$width;
  12. $RESIZEWIDTH=true;
  13. }
  14. if($maxheight && $height > $maxheight){
  15. $heightratio = $maxheight/$height;
  16. $RESIZEHEIGHT=true;
  17. }
  18. if($RESIZEWIDTH && $RESIZEHEIGHT){
  19. if($widthratio < $heightratio){
  20. $ratio = $widthratio;
  21. }else{
  22. $ratio = $heightratio;
  23. }
  24. }elseif($RESIZEWIDTH){
  25. $ratio = $widthratio;
  26. }elseif($RESIZEHEIGHT){
  27. $ratio = $heightratio;
  28. }
  29. $newwidth = $width * $ratio;
  30. $newheight = $height * $ratio;
  31. if(function_exists("imagecopyresampled")){
  32. $newim = imagecreatetruecolor($newwidth, $newheight);
  33. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  34. }else{
  35. $newim = imagecreate($newwidth, $newheight);
  36. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  37. }
  38. ImageJpeg ($newim,$name . ".jpg");
  39. ImageDestroy ($newim);
  40. }else{
  41. ImageJpeg ($im,$name . ".jpg");
  42. }
  43. }
  44. if($_FILES['image']['size']){
  45. if($_FILES['image']['type'] == "image/pjpeg"){
  46. $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  47. }elseif($_FILES['image']['type'] == "image/x-png"){
  48. $im = imagecreatefrompng($_FILES['image']['tmp_name']);
  49. }elseif($_FILES['image']['type'] == "image/gif"){
  50. $im = imagecreatefromgif($_FILES['image']['tmp_name']);
  51. }
  52. if($im){
  53. if(file_exists("$FILENAME.jpg")){
  54. unlink("$FILENAME.jpg");
  55. }
  56. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
  57. ImageDestroy ($im);
  58. }
  59. }
  60. ?>



阅读(2195) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~