PHP头条
热点:

php GD库生成缩略图


php GD库
php利用GD库生成缩略图。 
代码:
  1. <form method="post" action="suo_do.php" enctype="multipart/form-data">   
  2. <input type="file" name="pic" />   
  3. <input type="submit" value="上传1" />   
  4. </form>   
  5. <?php   
  6. header("content-type:text/html;charset=gbk");   
  7. ini_set("date.timezone","Asia/chong");   
  8. //判断文件是否为空   
  9. if(empty($_FILES)){   
  10. echo"上传文件过大";   
  11. exit;   
  12. }   
  13. //判断文件上传是否有错误   
  14. if($_FILES['pic']['error']){   
  15. echo "上传文件";   
  16. exit;   
  17. }   
  18. //判断文件类型是否非法获取文件后缀   
  19. $allowtype=array("jpg","png","jpeg","gif");   
  20. $a=explode('.',$_FILES['pic']['name']);   
  21. $index=count($a)-1;   
  22. $ex=strtolower($a[$index]);   
  23. if(!in_array($ex,$allowtype)){   
  24. echo "上传文件非法";   
  25. exit;   
  26. }   
  27. $file=date('YmdHis').rand().".".$ex;   
  28. $src=$_FILES['pic']['tmp_name'];   
  29. $des="upload/".$file;   
  30. $rs=move_uploaded_file($src,$des);   
  31.   
  32. //缩略图   
  33. //读取已经上传图片   
  34. $image=imagecreatefromjpeg($des);   
  35. $a=getimagesize($des);   
  36. $w=$a[0];   
  37. $h=$a[1];   
  38. if($w>$h){   
  39. $width=300;   
  40. $height=$width/$w*$h;   
  41. }else if($w<$h){   
  42. $height=300;   
  43. $width=$height/$h*$w;   
  44. }else{   
  45. $width=300;   
  46. $height=300;   
  47. } www.jbxue.com  
  48. //创建空白新图片   
  49. $newimage=imagecreatetruecolor($width$height);   
  50. //copy源图片内容 copy新图片   
  51. imagecopyresized($newimage$image, 0,0, 0,0, $width$height$w$h);   
  52. $filename="upload/s_".$file;   
  53. imagejpeg($newimage,$filename);   

www.phpzy.comtrue/php/6060.htmlTechArticlephp GD库生成缩略图 php GD库 php利用GD库生成缩略图。 代码: formmethod= "post" action= "suo_do.php" enctype= "multipart/form-data" inputtype= "file" name= "pic" / inputtype= "submit" value= "上传1" / /form ?php header( "co...

相关文章

相关频道:

PHP之友评论

今天推荐