joris

Upload And Resize Image

Mar 12th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.95 KB | None | 0 0
  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 = "image_files/".$_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: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
  78.         exit;
  79.     }
  80.     else{
  81.         header("Location: submit.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. ?>
  86.  
  87. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  88. <html xmlns="http://www.w3.org/1999/xhtml">
  89. <head>
  90. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  91. <title>Image Upload with resize</title>
  92. <style type="text/css">
  93. <!--
  94. body,td,th {
  95.     font-family: Arial, Helvetica, sans-serif;
  96.     color: #333333;
  97.     font-size: 12px;
  98. }
  99.  
  100. .upload_message_success {
  101.     padding:4px;
  102.     background-color:#009900;
  103.     border:1px solid #006600;
  104.     color:#FFFFFF;
  105.     margin-top:10px;
  106.     margin-bottom:10px;
  107. }
  108.  
  109. .upload_message_error {
  110.     padding:4px;
  111.     background-color:#CE0000;
  112.     border:1px solid #990000;
  113.     color:#FFFFFF;
  114.     margin-top:10px;
  115.     margin-bottom:10px;
  116. }
  117.  
  118. -->
  119. </style></head>
  120.  
  121. <body>
  122.  
  123. <h1 style="margin-bottom: 0px">Submit an image</h1>
  124.  
  125.  
  126.         <?php if(isset($_REQUEST['upload_message'])){?>
  127.             <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
  128.             <?php echo htmlentities($_REQUEST['upload_message']);?>
  129.             </div>
  130.         <?php }?>
  131.  
  132.  
  133. <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
  134. <label>Image file, maximum 4MB. it can be jpg, gif,  png:</label><br />
  135.           <input name="image_upload_box" type="file" id="image_upload_box" size="40" />
  136.           <input type="submit" name="submit" value="Upload image" />    
  137.      
  138.      <br />
  139.     <br />
  140.  
  141.      
  142.       <label>Scale down image? (2592 x 1944 px max):</label>
  143.       <br />
  144.       <input name="max_width_box" type="text" id="max_width_box" value="1024" size="4">
  145.       x      
  146.      
  147.       <input name="max_height_box" type="text" id="max_height_box" value="768" size="4">
  148.       px.
  149.       <br />
  150.       <br />
  151.       <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
  152.       <strong>Notes:</strong><br />
  153.   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 />
  154.   When uploading this script make sure you have a directory called &quot;image_files&quot; next to it and make that directory writable, permissions 777.<br />
  155.   After you uploaded images and made tests on our server please <a href="delete_all_images.php">delete all uploaded images </a> :)<br />
  156.   </p>
  157.  
  158.      
  159.  
  160. <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
  161.           </form>
  162.  
  163.  
  164.  
  165.  
  166. <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
  167. <p>
  168.     <img src="image_files/<?php echo $_REQUEST['show_image'];?>" />
  169. </p>
  170. <?php }?>
  171.  
  172.  
  173.  
  174.  
  175. </body>
  176. </html>
Advertisement
Add Comment
Please, Sign In to add comment