博客小子:记录我们对互联网和生活的探索! 注册 | 登陆

php上传图片和等比例缩略图图片的例子

Tags: php, 上传, 缩略图, 图片

好久没有搞这方面的东西,忘光光了。

今天要做一个这样的东西:上传图片然后按照比例缩略图,指定缩略图的最大高度或者最大宽度。

网上有很多现成的代码可以用,我基本上是网上搜刮一圈就找到自己想要的东西了。感谢原作者。

上传以及等比例缩小代码

 

PHP代码
  1. <?php   
  2. function _UPLOADPIC($upfile$maxsize$updir$newname = 'date') {   
  3.        
  4.     if ($newname == 'date')   
  5.         $newname = date ( "Ymdhis" ); //使用日期做文件名     
  6.     $name = $upfile ["name"];   
  7.     $type = $upfile ["type"];   
  8.     $size = $upfile ["size"];   
  9.     $tmp_name = $upfile ["tmp_name"];   
  10.        
  11.     switch ($type) {   
  12.         case 'image/pjpeg' :   
  13.         case 'image/jpeg' :   
  14.             $extend = ".jpg";   
  15.             break;   
  16.         case 'image/gif' :   
  17.             $extend = ".gif";   
  18.             break;   
  19.         case 'image/png' :   
  20.             $extend = ".png";   
  21.             break;   
  22.     }   
  23.     if (emptyempty ( $extend )) {   
  24.         echo  ( "警告!只能上传图片类型:GIF JPG PNG" );   
  25.         exit ();   
  26.     }   
  27.     if ($size > $maxsize) {   
  28.         $maxpr = $maxsize / 1000;   
  29.         echo  ( "警告!上传图片大小不能超过" . $maxpr . "K!" );   
  30.         exit ();   
  31.     }   
  32.     if (move_uploaded_file ( $tmp_name$updir . $newname . $extend )) {   
  33.         return $updir . $newname . $extend;   
  34.     }   
  35. }   
  36.   
  37. function show_pic_scal($width$height$picpath) {   
  38.     $imginfo = GetImageSize ( $picpath );   
  39.     $imgw = $imginfo [0];   
  40.     $imgh = $imginfo [1];   
  41.        
  42.     $ra = number_format ( ($imgw / $imgh), 1 ); //宽高比   
  43.     $ra2 = number_format ( ($imgh / $imgw), 1 ); //高宽比   
  44.        
  45.   
  46.     if ($imgw > $width or $imgh > $height) {   
  47.         if ($imgw > $imgh) {   
  48.             $newWidth = $width;   
  49.             $newHeight = round ( $newWidth / $ra );   
  50.            
  51.         } elseif ($imgw < $imgh) {   
  52.             $newHeight = $height;   
  53.             $newWidth = round ( $newHeight / $ra2 );   
  54.         } else {   
  55.             $newWidth = $width;   
  56.             $newHeight = round ( $newWidth / $ra );   
  57.         }   
  58.     } else {   
  59.         $newHeight = $imgh;   
  60.         $newWidth = $imgw;   
  61.     }   
  62.     $newsize [0] = $newWidth;   
  63.     $newsize [1] = $newHeight;   
  64.        
  65.     return $newsize;   
  66. }   
  67.   
  68.   
  69.   
  70. function getImageInfo($src)   
  71. {   
  72.     return getimagesize($src);   
  73. }   
  74. /**  
  75. * 创建图片,返回资源类型  
  76. * @param string $src 图片路径  
  77. * @return resource $im 返回资源类型   
  78. * **/  
  79. function create($src)   
  80. {   
  81.     $info=getImageInfo($src);   
  82.     switch ($info[2])   
  83.     {   
  84.         case 1:   
  85.             $im=imagecreatefromgif($src);   
  86.             break;   
  87.         case 2:   
  88.             $im=imagecreatefromjpeg($src);   
  89.             break;   
  90.         case 3:   
  91.             $im=imagecreatefrompng($src);   
  92.             break;   
  93.     }   
  94.     return $im;   
  95. }   
  96. /**  
  97. * 缩略图主函数  
  98. * @param string $src 图片路径  
  99. * @param int $w 缩略图宽度  
  100. * @param int $h 缩略图高度  
  101. * @return mixed 返回缩略图路径  
  102. * **/  
  103.   
  104. function resize($src,$w,$h)   
  105. {   
  106.     $temp=pathinfo($src);   
  107.     $name=$temp["basename"];//文件名   
  108.     $dir=$temp["dirname"];//文件所在的文件夹   
  109.     $extension=$temp["extension"];//文件扩展名   
  110.     $savepath="{$dir}/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg   
  111.   
  112.     //获取图片的基本信息   
  113.     $info=getImageInfo($src);   
  114.     $width=$info[0];//获取图片宽度   
  115.     $height=$info[1];//获取图片高度   
  116.     $per1=round($width/$height,2);//计算原图长宽比   
  117.     $per2=round($w/$h,2);//计算缩略图长宽比   
  118.   
  119.     //计算缩放比例   
  120.     if($per1>$per2||$per1==$per2)   
  121.     {   
  122.         //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先   
  123.         $per=$w/$width;   
  124.     }   
  125.     if($per1<$per2)   
  126.     {   
  127.         //原图长宽比小于缩略图长宽比,则按照高度优先   
  128.         $per=$h/$height;   
  129.     }   
  130.     $temp_w=intval($width*$per);//计算原图缩放后的宽度   
  131.     $temp_h=intval($height*$per);//计算原图缩放后的高度   
  132.     $temp_img=imagecreatetruecolor($temp_w,$temp_h);//创建画布   
  133.     $im=create($src);   
  134.     imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);   
  135.     if($per1>$per2)   
  136.     {   
  137.         imagejpeg($temp_img,$savepath, 100);   
  138.         imagedestroy($im);   
  139.         return addBg($savepath,$w,$h,"w");   
  140.         //宽度优先,在缩放之后高度不足的情况下补上背景   
  141.     }   
  142.     if($per1==$per2)   
  143.     {   
  144.         imagejpeg($temp_img,$savepath, 100);   
  145.         imagedestroy($im);   
  146.         return $savepath;   
  147.         //等比缩放   
  148.     }   
  149.     if($per1<$per2)   
  150.     {   
  151.         imagejpeg($temp_img,$savepath, 100);   
  152.         imagedestroy($im);   
  153.         return addBg($savepath,$w,$h,"h");   
  154.         //高度优先,在缩放之后宽度不足的情况下补上背景   
  155.     }   
  156. }   
  157. /**  
  158. * 添加背景  
  159. * @param string $src 图片路径  
  160. * @param int $w 背景图像宽度  
  161. * @param int $h 背景图像高度  
  162. * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比  
  163. * @return 返回加上背景的图片  
  164. * **/  
  165. function addBg($src,$w,$h,$fisrt="w")   
  166. {   
  167.     $bg=imagecreatetruecolor($w,$h);   
  168.     $white = imagecolorallocate($bg,255,255,255);   
  169.     imagefill($bg,0,0,$white);//填充背景   
  170.   
  171.     //获取目标图片信息   
  172.     $info=getImageInfo($src);   
  173.     $width=$info[0];//目标图片宽度   
  174.     $height=$info[1];//目标图片高度   
  175.     $img=create($src);   
  176.     if($fisrt=="wh")   
  177.     {   
  178.         //等比缩放   
  179.         return $src;   
  180.     }   
  181.     else  
  182.     {   
  183.         if($fisrt=="w")   
  184.         {   
  185.             $x=0;   
  186.             $y=($h-$height)/2;//垂直居中   
  187.         }   
  188.         if($fisrt=="h")   
  189.         {   
  190.             $x=($w-$width)/2;//水平居中   
  191.             $y=0;   
  192.         }   
  193.         imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);   
  194.         imagejpeg($bg,$src,100);   
  195.         imagedestroy($bg);   
  196.         imagedestroy($img);   
  197.         return $src;   
  198.     }   
  199.   
  200. }   
  201.   
  202.   
  203. ?>  

 

使用方法:  

  $filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date'));
  $show_pic_scal=show_pic_scal(230, 230, $filename);
  resize($filename,$show_pic_scal[0],$show_pic_scal[1]);

唔!大概就是这样吧,备忘而已。

Tags: php, 上传, 缩略图, 图片

« 上一篇:vc中使用字符串分割字符串的函数 | 下一篇:抓取分析网页加载数据的软件:httpwatch »

只显示10条记录相关文章

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):