title: PHP Image Compression then Upload
body: <p>The following code I have is a combination from two sources. </p>
  
  <p>Image Upload:<a href="http://www.w3schools.com/php/php_file_upload.asp" rel="nofollow">http://www.w3schools.com/php/php_file_upload.asp</a>
  Image Compression:<a href="https://www.apptha.com/blog/how-to-reduce-image-file-size-while-uploading-using-php-code/" rel="nofollow">https://www.apptha.com/blog/how-to-reduce-image-file-size-while-uploading-using-php-code/</a></p>
  
  <p>The Image Upload feature uploads straight to the server. The compression one compresses the image then downloads it. I am trying to integrate this upload feature into my upload feature as it is already embedded in our website. </p>
  
  <p>The following code does two things: </p>
  
  <ol>
  <li>It compresses on upload and downloads</li>
  <li>Uploads the raw image to the server uncompressed</li>
  </ol>
  
  <p>What I want done is compress on upload and send to the server. I don't need it downloading. I am just having trouble integrating the two features together. </p>
  
  <pre><code>&lt;?php 
      $name = ''; $type = ''; $size = ''; $error = ''; 
      function compress_image($source_url, $destination_url, $quality) { 
          $info = getimagesize($source_url); 
          if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); 
          elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); 
          elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); 
          imagejpeg($image, $destination_url, $quality); 
          return $destination_url; 
      } 
  if ($_POST) { 
      if ($_FILES["file"]["error"] &gt; 0) { 
          $error = $_FILES["file"]["error"]; 
      } 
      else if (($_FILES["file"]["type"] == "image/gif") || 
               ($_FILES["file"]["type"] == "image/jpeg") || 
               ($_FILES["file"]["type"] == "image/png") || 
               ($_FILES["file"]["type"] == "image/pjpeg")) { 
          $url = 'destination .jpg'; 
  
          $filename = compress_image($_FILES["file"]["tmp_name"], $url, 80); 
          $buffer = file_get_contents($url); 
  
          /* Force download dialog... */
          header("Content-Type: application/force-download"); 
          header("Content-Type: application/octet-stream"); 
          header("Content-Type: application/download");
  
          /* Don't allow caching... */ 
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  
          /* Set data type, size and filename */ 
          header("Content-Type: application/octet-stream"); 
          header("Content-Transfer-Encoding: binary"); 
          header("Content-Length: " . strlen($buffer)); 
          header("Content-Disposition: attachment; filename=$url"); 
  
          /* Send our file... */ 
          echo $buffer; 
  //**The following is the image upload to the server**********
  $target_dir = "newuploads/";
  $target_file = $target_dir . basename($_FILES["file"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);{
  if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
          echo "";}
      }
  }
  
      else { 
          $error = "Uploaded image should be jpg or gif or png"; 
      } 
  } 
  ?&gt; 
  &lt;html&gt; 
      &lt;head&gt; 
          &lt;title&gt;Php code compress the image&lt;/title&gt; 
      &lt;/head&gt; 
  
      &lt;body&gt; 
          &lt;div class="message"&gt; 
              &lt;?php 
              if($_POST){ 
                  if ($error) { 
              ?&gt; 
              &lt;label class="error"&gt;&lt;?php echo $error; ?&gt;&lt;/label&gt; 
              &lt;?php 
                  } 
              } 
              ?&gt; 
          &lt;/div&gt; 
          &lt;fieldset class="well"&gt; 
              &lt;legend&gt;Upload Image:&lt;/legend&gt; 
              &lt;form action="" name="myform" id="myform" method="post" enctype="multipart/form-data"&gt; 
                  &lt;ul&gt; 
                      &lt;li&gt; 
                          &lt;label&gt;Upload:&lt;/label&gt; 
                          &lt;input type="file" name="file" id="file"/&gt; 
                      &lt;/li&gt; 
                      &lt;li&gt; 
                          &lt;input type="submit" name="submit" id="submit" class="submit btn-success"/&gt; 
                      &lt;/li&gt; 
                  &lt;/ul&gt; 
              &lt;/form&gt; 
          &lt;/fieldset&gt; 
      &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>