当前位置:首页 > PHP教程 > php上传下载 > 列表

php实现从上传文件创建缩略图的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-20 16:53:34 浏览: 评论:0 

这篇文章主要介绍了php实现从上传文件创建缩略图的方法,涉及php操作上传文件及图片操作的技巧,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了php实现从上传文件创建缩略图的方法,分享给大家供大家参考,具体实现方法如下:

  1. <?php 
  2. if ($_REQUEST['action']=="add"){ 
  3. $userfile = $HTTP_POST_FILES['photo']['tmp_name']; 
  4. $userfile_name = $HTTP_POST_FILES['photo']['name']; 
  5. $userfile_size = $HTTP_POST_FILES['photo']['size']; 
  6. $userfile_type = $HTTP_POST_FILES['photo']['type']; 
  7. /////////////////////////  
  8. //GET-DECLARE DIMENSIONS // 
  9. $dimension = getimagesize($userfile); 
  10. $large_width = $dimension[0]; // GET PHOTO WIDTH 
  11. $large_height = $dimension[1]; //GET PHOTO HEIGHT 
  12. $small_width = 120; // DECLARE THUMB WIDTH 
  13. $small_height = 90; // DECLARE THUMB HEIGHT 
  14. ///////////////////////// 
  15. //CHECK SIZE // 
  16. if ($userfile_size>102400){ 
  17.    $error=1; 
  18.    $msg = "The photo is over 100kb. Please try again."; 
  19. } 
  20. //////////////////////////////// 
  21. // CHECK TYPE (IE AND OTHERS) // 
  22. if ($userfile_type="image/pjpeg"){ 
  23.   if ($userfile_type!="image/jpeg"){ 
  24.     $error=1; 
  25.     $msg = "The photo must be JPG"; 
  26.   } 
  27. } 
  28. ////////////////////////////// 
  29. //CHECK WIDTH/HEIGHT // 
  30. if ($large_width!=600 or$large_height!=400){ 
  31. $error=1; 
  32. $msg = "The photo must be 600x400 pixels"; 
  33. } 
  34. /////////////////////////////////////////// 
  35. //CREATE THUMB / UPLOAD THUMB AND PHOTO /// 
  36. if ($error<>1){ 
  37.   $image = $userfile_name; //if you want to insert it to the database 
  38.   $pic = imagecreatefromjpeg($userfile); 
  39.   $small = imagecreatetruecolor($small_width,$small_height); 
  40.   imagecopyresampled($small,$pic,0,0,0,0, $small_width, $small_height, $large_width, $large_height); 
  41.   if (imagejpeg($small,"path/to/folder/to/upload/thumb".$userfile_name, 100)){   
  42.     $large = imagecreatetruecolor($large_width,$large_height); 
  43.   imagecopyresampled($large,$pic,0,0,0,0, $large_width, $large_height, $large_width, $large_height); 
  44.     if (imagejpeg($large,"path/to/folder/to/upload/photo".$userfile_name, 100)) 
  45.    {} 
  46.       else {$msg="A problem has occured. Please try again."; $error=1;} 
  47.   } 
  48.   else { 
  49.    $msg="A problem has occured. Please try again."; $error=1; 
  50.   } 
  51. } 
  52. ////////////////////////////////////////////// 
  53. /// If everything went right a photo (600x400) and 
  54. /// a thumb(120x90) were uploaded to the given folders 
  55. } 
  56. ?> 
  57. <html><head><title>create thumb</title></head> 
  58. <body> 
  59. <form name="form1" enctype="multipart/form-data" action="thisfile.php?action=add" method="post"> 
  60. Select Photo: <input type="file" name="photo"> 
  61. <input type="submit" name="submit" value="CREATE THUMB AND UPLOAD"> 
  62. </form> 
  63. </body 
  64. </html>

Tags: php上传文件 php创建缩略图

分享到: