首页>>表单>>上传图片并可以调整图片大小(2013-10-27)

上传图片并可以调整图片大小

 上传图片并按照设置的长和宽设置大小

上传图片并可以调整图片大小
赞赏支持
立刻微信赞赏支持 关闭

 

PHP Code
  1. <?php ini_set("memory_limit""200000000"); // for large images so that we do not get "Allowed memory exhausted"?>  
  2. <?php  
  3. // upload the file  
  4. if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {  
  5.       
  6.     // file needs to be jpg,gif,bmp,x-png and 4 MB max  
  7.     if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))  
  8.     {  
  9.           
  10.     
  11.         // some settings  
  12.         $max_upload_width = 2592;  
  13.         $max_upload_height = 1944;  
  14.             
  15.         // if user chosed properly then scale down the image according to user preferances  
  16.         if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){  
  17.             $max_upload_width = $_REQUEST['max_width_box'];  
  18.         }      
  19.         if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){  
  20.             $max_upload_height = $_REQUEST['max_height_box'];  
  21.         }     
  22.   
  23.           
  24.         // if uploaded image was JPG/JPEG  
  25.         if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){      
  26.             $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);  
  27.         }         
  28.         // if uploaded image was GIF  
  29.         if($_FILES["image_upload_box"]["type"] == "image/gif"){   
  30.             $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);  
  31.         }     
  32.         // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)     
  33.         // if uploaded image was BMP  
  34.         if($_FILES["image_upload_box"]["type"] == "image/bmp"){   
  35.             $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);  
  36.         }             
  37.         // if uploaded image was PNG  
  38.         if($_FILES["image_upload_box"]["type"] == "image/x-png"){  
  39.             $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);  
  40.         }  
  41.           
  42.   
  43.         $remote_file = "../upload/".$_FILES["image_upload_box"]["name"];  
  44.         imagejpeg($image_source,$remote_file,100);  
  45.         chmod($remote_file,0644);  
  46.       
  47.       
  48.   
  49.         // get width and height of original image  
  50.         list($image_width$image_height) = getimagesize($remote_file);  
  51.       
  52.         if($image_width>$max_upload_width || $image_height >$max_upload_height){  
  53.             $proportions = $image_width/$image_height;  
  54.               
  55.             if($image_width>$image_height){  
  56.                 $new_width = $max_upload_width;  
  57.                 $new_height = round($max_upload_width/$proportions);  
  58.             }         
  59.             else{  
  60.                 $new_height = $max_upload_height;  
  61.                 $new_width = round($max_upload_height*$proportions);  
  62.             }         
  63.               
  64.               
  65.             $new_image = imagecreatetruecolor($new_width , $new_height);  
  66.             $image_source = imagecreatefromjpeg($remote_file);  
  67.               
  68.             imagecopyresampled($new_image$image_source, 0, 0, 0, 0, $new_width$new_height$image_width$image_height);  
  69.             imagejpeg($new_image,$remote_file,100);  
  70.               
  71.             imagedestroy($new_image);  
  72.         }  
  73.           
  74.         imagedestroy($image_source);  
  75.           
  76.           
  77.         header("Location: index.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);  
  78.         exit;  
  79.     }  
  80.     else{  
  81.         header("Location: index.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");  
  82.         exit;  
  83.     }  
  84. }  
  85. ?>  

 

PHP Code
  1. <?php if(isset($_REQUEST['upload_message'])){?>  
  2.             <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">  
  3.             <?php echo htmlentities($_REQUEST['upload_message']);?>  
  4.             </div>  
  5.         <?php }?>  
  6.   
  7.   
  8. <form action="index.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">  
  9. <label>Image file, maximum 4MB. it can be jpg, gif,  png:</label><br />  
  10.           <input name="image_upload_box" type="file" id="image_upload_box" size="40" />  
  11.           <input type="submit" name="submit" value="Upload image" />       
  12.        
  13.      <br />  
  14.     <br />  
  15.   
  16.        
  17.       <label>Scale down image? (2592 x 1944 px max):</label>  
  18.       <br />  
  19.       <input name="max_width_box" type="text" id="max_width_box" value="1024" size="4">  
  20.       x        
  21.         
  22.       <input name="max_height_box" type="text" id="max_height_box" value="768" size="4">  
  23.       px.  
  24.       <br />  
  25.       <br />  
  26.       <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">  
  27.       <strong>Notes:</strong><br />  
  28.   The image will not be resized to this exact size; it will be scalled down so that neider width or height is larger than specified.<br />  
  29.   When uploading this script make sure you have a directory called "image_files" next to it and make that directory writable, permissions 777.<br />  
  30.   After you uploaded images and made tests on our server please <a href="delete_all_images.php">delete all uploaded images </a> :)<br />  
  31.   </p>  
  32.   
  33.         
  34.   
  35. <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />  
  36.           </form>  
  37.   
  38.   
  39.   
  40.   
  41. <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>  
  42. <p>  
  43.     <img src="../upload/<?php echo $_REQUEST['show_image'];?>" />  
  44. </p>  
  45. <?php }?>  

删除图片

PHP Code
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>Delete all images</title>  
  6. </head>  
  7.   
  8. <body>  
  9.   
  10. <?  
  11.    $dir = '../upload/';  
  12.    // open specified directory  
  13.    $dirHandle = opendir($dir);  
  14.    $total_deleted_images = 0;  
  15.    while ($file = readdir($dirHandle)) {  
  16.       // if not a subdirectory and if filename contains the string '.jpg'   
  17.       if(!is_dir($file)) {  
  18.          // update count and string of files to be returned  
  19.          unlink($dir.$file);  
  20.          echo 'Deleted file <b>'.$file.'</b><br />';  
  21.          $total_deleted_images++;  
  22.       }   
  23.    }   
  24.    closedir($dirHandle);  
  25.     if($total_deleted_images=='0'){  
  26.         echo 'There ware no files uploaded there.';  
  27.     }  
  28.     echo '<br />Thank you.';  
  29. ?>  
  30.   
  31. </body>  
  32. </html>  

 


原文地址:http://www.freejs.net/article_biaodan_79.html