php 上传图片自动生成缩略图

一款实现的生成小图功能的实现代码,有需要的朋友可以参考,每个都有详细的说明参数,php 上传图片自动生成缩略图实例代码如下:

  1. <form action="uploads.php" method="post" enctype="multipart/form-data">
  2. <input type='file' name='image'><br>
  3. <input type='submit' name='sub' value='提交'>
  4. </form>
  5. //uploads.php文件
  6. <?php
  7. class image_upload{
  8. private $srcimg; //原图片
  9. private $destimg; // 目标图片
  10. private $width; //原图片的宽度
  11. private $height; //原图片的高度
  12. private $type; //原文件的图片类型
  13. private $thumb_width; //缩略图的宽度
  14. private $thumb_height; //缩略图的高度
  15. private $cut; //是否剪切图片到指定高度
  16. private $tmp; //上传图片的临时地址
  17. private $error;
  18. private $im; // 创建一个临时的图片句柄
  19. private $new_name; //上传文件的新名字
  20. //开源代码phpfensi.com
  21. function __construct($srcimg,$t_width,$t_height,$cut,$tmp,$error){
  22. $this->srcimg=$srcimg;
  23. $this->thumb_width=$t_width;
  24. $this->thumb_height=$t_height;
  25. $this->cut=$cut;
  26. $this->tmp=$tmp;
  27. $this->error=$error;
  28. $this->get_srcimg_type();
  29. $this->get_new_upload_name();
  30. }
  31. function img_upload(){
  32. //文件上传的方法
  33. $this->check_error($this->error);
  34. $this->in_type();
  35. $dst_dir='./images';
  36. if(!is_dir($dst_dir)){
  37. mkdir($dst_dir);
  38. echo "%%%<BR>";
  39. }
  40. if(is_uploaded_file($this->tmp)){
  41. if(move_uploaded_file($this->tmp, $this->new_name)){
  42. echo "文件上传成功<br>";
  43. return true;
  44. }else{
  45. echo '文件不能被移动,上传失败';
  46. exit;
  47. }
  48. }else{
  49. echo '文件上传可能被攻击';
  50. exit;
  51. }
  52. }
  53. function make_thumbnail(){
  54. //生成缩略图的方法
  55. $this->get_dest_imgpath();
  56. $this->make_im();
  57. $this->width=imagesx($this->im);
  58. $this->height=imagesy($this->im);
  59. $thumb_ratio=$this->thumb_width/$this->thumb_height;
  60. $ratio=$this->width/$this->height;
  61. if($this->cut==1){ //是否裁剪
  62. if($ratio>=$thumb_ratio){
  63. $img=imagecreatetruecolor($this->thumb_width, $this->thumb_height);
  64. imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_width, $this->thumb_height, $this->height*$thumb_ratio, $this->height);
  65. imagejpeg($img,$this->destimg);
  66. echo "缩略图生成成功";
  67. }else{
  68. $img=imagecreatetruecolor($this->thumb_width, $this->thumb_height);
  69. imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_width, $this->thumb_height, $this->width, $this->width/$thumb_ratio);
  70. imagejpeg($img,$this->destimg);
  71. echo "缩略图生成成功";
  72. }
  73. }else{
  74. if($ratio>=$thumb_ratio){
  75. $img=imagecreatetruecolor($this->thumb_height*$thumb_ratio, $this->thumb_height);
  76. imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_height*$thumb_ratio, $this->thumb_height, $this->width, $this->height);
  77. imagejpeg($img,$this->destimg);
  78. echo "缩略图生成成功";
  79. }else{
  80. $img=imagecreatetruecolor($this->thumb_width, $this->thumb_width/$thumb_ratio);
  81. imagecopyresampled($img, $this->im, 0, 0, 0, 0, $this->thumb_width, $this->thumb_width/$thumb_ratio, $this->width, $this->height);
  82. imagejpeg($img,$this->destimg);
  83. echo "缩略图生成成功";
  84. }
  85. }
  86. imagedestroy($this->im);
  87. imagedestroy($img);
  88. }
  89. private function check_error($error){
  90. //检查文件上传传得错误;
  91. if($error>0){
  92. switch($error){
  93. case 1:
  94. echo "上传文件的大小超过了PHP.INI文件中得配置<br>";
  95. break;
  96. case 2:
  97. echo "上传文件的大小超过了表单中的限制大小<br>";
  98. break;
  99. case 3:
  100. echo "只有部分文件被上传<br>";
  101. break;
  102. case 4:
  103. echo "没有文件被上传<br>";
  104. break;
  105. case 6:
  106. echo "php.ini中没有设置图片存放的临时未知<br>";
  107. break;
  108. case 7:
  109. echo "硬盘不可以写入,上传失败<br>";
  110. break;
  111. default:
  112. echo "未知错误";
  113. break;
  114. }
  115. }
  116. }
  117. private function get_srcimg_type(){
  118. //判断源文件的图片类型
  119. $this->type=substr(strrchr($this->srcimg, '.'),'1');
  120. }
  121. private function in_type(){
  122. //检查文件是否符合类型
  123. $type_arr=array('gif','jpg','png');
  124. if(!in_array($this->type, $type_arr)){
  125. echo "只支持PNG,GIF,JPG 三种类型的文件格式……,请重新上传正确的格式";
  126. exit;
  127. }
  128. }
  129. private function get_new_upload_name(){
  130. //上传的文件生成新的名字
  131. $this->new_name='images/'.date('YmdHis').'.'.$this->type;
  132. }
  133. private function make_im(){
  134. //从原文件新建一幅图像
  135. switch($this->type){
  136. case 'jpg':
  137. $this->im=imagecreatefromjpeg($this->new_name);
  138. break;
  139. case 'gif':
  140. $this->im=imagecreatefromgif($this->new_name);
  141. break;
  142. case 'png':
  143. $this->im=imagecreatefrompng($this->new_name);
  144. break;
  145. }
  146. }
  147. private function get_dest_imgpath(){
  148. //得到缩略图的存储路径
  149. $len1=strlen($this->new_name);
  150. $len2=strlen(strrchr($this->new_name,'.'));
  151. $len3=$len1-$len2;
  152. $this->destimg=substr($this->new_name,0,$len3).'_small.'.$this->type;
  153. }
  154. }
  155. print_r($_FILES);
  156. $file=$_FILES['image'];
  157. echo $file['name'];
  158. $uploads=new image_upload($file['name'], 120, 160, 1, $file['tmp_name'],$file['error'] );
  159. if($uploads->img_upload()){
  160. $uploads->make_thumbnail();
  161. }
  162. ?>