PHP创建图片缩略图函数

  1. //创建缩略图 
  2. function ResizeImage($im,$maxwidth,$maxheight,$name,$sldir,$type){
  3. $width = imagesx($im);
  4. $height = imagesy($im);
  5. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  6. if($maxwidth && $width > $maxwidth){
  7. $widthratio = $maxwidth/$width;
  8. $RESIZEWIDTH=true;
  9. }
  10. if($maxheight && $height > $maxheight){
  11. $heightratio = $maxheight/$height;
  12. $RESIZEHEIGHT=true;
  13. }
  14. if($RESIZEWIDTH && $RESIZEHEIGHT){
  15. if($widthratio < $heightratio){
  16. $ratio = $widthratio;
  17. }else{
  18. $ratio = $heightratio;
  19. }
  20. }elseif($RESIZEWIDTH){
  21. $ratio = $widthratio;
  22. }elseif($RESIZEHEIGHT){
  23. $ratio = $heightratio;
  24. }
  25. $newwidth = $width * $ratio;
  26. $newheight = $height * $ratio;
  27. if(function_exists("imagecopyresampled")){
  28. $newim = imagecreatetruecolor($newwidth$newheight);
  29. imagecopyresampled($newim$im, 0, 0, 0, 0, $newwidth$newheight$width$height);
  30. }else{
  31. $newim = imagecreate($newwidth$newheight);
  32. imagecopyresized($newim$im, 0, 0, 0, 0, $newwidth$newheight$width$height);
  33. }
  34. ImageJpeg ($newim,$sldir.$name.".".$type);
  35. ImageDestroy ($newim);
  36. }else{
  37. ImageJpeg ($im,$sldir.$name.".".$type);
  38. }
  39. }
  40. function create_small_img($new_name,$width,$height,$file_dir,$file_name,$file_tmpname,$file_type){
  41. file_exists($file_diror mkdir($file_dir,0777);
  42. $imgtype = end(explode(".",$file_name));
  43. $new_imgname = $new_name.".".$imgtype;
  44. if($file_name){
  45. if($file_type == "image/pjpeg"){
  46. $im = imagecreatefromjpeg($file_tmpname);
  47. }elseif($file_type == "image/x-png"){
  48. $im = imagecreatefrompng($file_tmpname);
  49. }elseif($file_type == "image/gif"){
  50. $im = imagecreatefromgif($file_tmpname);
  51. }
  52. if($im){
  53. if(file_exists($file_dir.$new_imgname)){
  54. unlink($file_dir.$new_imgname);
  55. }
  56. ResizeImage($im,$width,$height,$new_name,$file_dir,$imgtype);
  57. ImageDestroy ($im);
  58. }
  59. }
  60.                          }
  61. //创建缩略图 
  62. //用法 
  63. create_small_img($store_img_new_name,$width,$height,$small_img_dir,$upload_img_name,$upload_img_temp,$upload_img_type);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值