PHP实现图片上传并压缩

这篇文章主要介绍了PHP实现图片上传并压缩的相关资料,上传图片然后按照比例缩略图,感兴趣的小伙伴们可以参考一下,本文实例讲解了PHP图片上传并压缩的实现方法,分享给大家供大家参考,具体内容如下

使用到三个文件

connect.php:连接数据库

test_upload.php:执行SQL语句

upload_img.php:上传图片并压缩

三个文件代码如下:

连接数据库:connect.php

  1. <?php
  2. $db_host = '';
  3. $db_user = '';
  4. $db_psw = '';
  5. $db_name = '';
  6. $db_port = '';
  7. $sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);
  8. $q="set names utf8;";
  9. $result=$sqlconn->query($q);
  10. if (mysqli_connect_errno()) {
  11. printf("Connect failed: %s\n", mysqli_connect_error());
  12. exit();
  13. }
  14. ?>

执行SQL语句:test_upload.php

  1. <?php
  2. require ("connect.php");
  3. require ("upload_img.php");
  4. $real_img=$uploadfile;
  5. $small_img=$uploadfile_resize;
  6. $insert_sql = "insert into img (real_img,small_img) values (?,?)";
  7. $result = $sqlconn -> prepare($insert_sql);
  8. $result -> bind_param("ss", $real_img,$small_img);
  9. $result -> execute();
  10. ?>

上传图片并压缩:upload_img.php

  1. <?php
  2. //设置文件保存目录
  3. $uploaddir = "upfiles/";
  4. //设置允许上传文件的类型
  5. $type=array("jpg","gif","bmp","jpeg","png");
  6. //获取文件后缀名函数
  7. function fileext($filename)
  8. {
  9. return substr(strrchr($filename, '.'), 1);
  10. }
  11. //生成随机文件名函数
  12. function random($length)
  13. {
  14. $hash = 'CR-';
  15. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  16. $max = strlen($chars) - 1;
  17. mt_srand((double)microtime() * 1000000);
  18. for($i = 0; $i < $length; $i++)
  19. {
  20. $hash .= $chars[mt_rand(0, $max)];
  21. }
  22. return $hash;
  23. }
  24. $a=strtolower(fileext($_FILES['filename']['name']));
  25. //判断文件类型
  26. if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type))
  27. {
  28. $text=implode(",",$type);
  29. $ret_code=3;//文件类型错误
  30. $page_result=$text;
  31. $retArray = array('ret_code' => $ret_code,'page_result'=>$page_result);
  32. $retJson = json_encode($retArray);
  33. echo $retJson;
  34. return;
  35. }
  36. //生成目标文件的文件名
  37. else
  38. {
  39. $filename=explode(".",$_FILES['filename']['name']);
  40. do
  41. {
  42. $filename[0]=random(10); //设置随机数长度
  43. $name=implode(".",$filename);
  44. //$name1=$name.".Mcncc";
  45. $uploadfile=$uploaddir.$name;
  46. }
  47. while(file_exists($uploadfile));
  48. if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))
  49. {
  50. if(is_uploaded_file($_FILES['filename']['tmp_name']))
  51. {
  52. $ret_code=1;//上传失败
  53. }
  54. else
  55. {//上传成功
  56. $ret_code=0;
  57. }
  58. }
  59. $retArray = array('ret_code' => $ret_code);
  60. $retJson = json_encode($retArray);
  61. echo $retJson;
  62. }
  63. //压缩图片
  64. $uploaddir_resize="upfiles_resize/";
  65. $uploadfile_resize=$uploaddir_resize.$name;
  66. //$pic_width_max=120;
  67. //$pic_height_max=90;
  68. //以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩
  69. $file_type=$_FILES["filename"]['type'];
  70. function ResizeImage($uploadfile,$maxwidth,$maxheight,$name)
  71. {
  72. //取得当前图片大小
  73. $width = imagesx($uploadfile);
  74. $height = imagesy($uploadfile);
  75. $i=0.5;
  76. //生成缩略图的大小
  77. if(($width > $maxwidth) || ($height > $maxheight))
  78. {
  79. /*
  80. $widthratio = $maxwidth/$width;
  81. $heightratio = $maxheight/$height;
  82. if($widthratio < $heightratio)
  83. {
  84. $ratio = $widthratio;
  85. }
  86. else
  87. {
  88. $ratio = $heightratio;
  89. }
  90. $newwidth = $width * $ratio;
  91. $newheight = $height * $ratio;
  92. */
  93. $newwidth = $width * $i;
  94. $newheight = $height * $i;
  95. if(function_exists("imagecopyresampled"))
  96. {
  97. $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
  98. imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  99. }
  100. else
  101. {
  102. $uploaddir_resize = imagecreate($newwidth, $newheight);
  103. imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  104. }
  105. ImageJpeg ($uploaddir_resize,$name);
  106. ImageDestroy ($uploaddir_resize);
  107. }
  108. else
  109. {
  110. ImageJpeg ($uploadfile,$name);
  111. }
  112. }
  113. if($_FILES["filename"]['size'])
  114. {
  115. if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg")
  116. {
  117. //$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
  118. $im = imagecreatefromjpeg($uploadfile);
  119. }
  120. elseif($file_type == "image/x-png")
  121. {
  122. //$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
  123. $im = imagecreatefromjpeg($uploadfile);
  124. }
  125. elseif($file_type == "image/gif")
  126. {
  127. //$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
  128. $im = imagecreatefromjpeg($uploadfile);
  129. }
  130. else//默认jpg
  131. {
  132. $im = imagecreatefromjpeg($uploadfile);
  133. }
  134. if($im)
  135. {
  136. ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
  137. ImageDestroy ($im);
  138. }
  139. }
  140. ?>

请按照现实情况更改connect.php,test_upload.php中对应的信息,以上就是PHP实现图片上传并压缩的方法,希望对大家的学习php程序设计有所帮助