title: Upload, compress and save an image into oracle database as blob
body: <p>I'm new here and this is the first question I'm doing to this community, I'm gonna be as clear as possible, so first of all I explain the issue:</p>
  
  <p>at the company I work for, we use a web system that manages all the expenses made by the consultants, and for every expense they UPLOAD an IMAGE of the receipt (that can be made directly with their phone's camera). This IMAGES are then INSERTED into an ORACLE DB as BLOB FILES.</p>
  
  <p>The issue then is that the amount of files uploaded it's slowing down the system so the aim is to find a way to COMPRESS large images (because quality it's not important, they only need to read the payment info) during the UPLOAD and then SAVE into the DB into a BLOB file.</p>
  
  <p>Long story short I show you the already working code and some method I've tried.. let's find a solution together.</p>
  
  <p>This is the Upload Form: (I've omitted the hidden info which are usefull for the query but that are not important here)</p>
  
  <pre><code>&lt;form action="uploadPage.php" method="POST" data-ajax="false" enctype="multipart/form-data"&gt;
      &lt;br&gt;
          &lt;center&gt;&lt;h3&gt;Allega foto&lt;/h3&gt;&lt;/center&gt;
              &lt;input  type="file"     id="uploadfile"     name="lob_upload"       value="" accept="image/*" onchange="controlloAllegato();" /&gt;
              &lt;button type="submit"   id="allegaSpese"    name="allegaSpese"      value="submit" onclick="loadFile();" disabled&gt;Inserisci&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>So after the file gets uploaded the action send it in a Php File which handle the insert into db:</p>
  
  <p>First step, it retrieves the information about the file</p>
  
  <pre><code>$tipo = $_FILES['lob_upload']['type'];
  $dimensione = $_FILES['lob_upload']['size'];
  $name = pathinfo($filename, PATHINFO_FILENAME);
  $estensione =  strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  </code></pre>
  
  <p>Then it runs the query</p>
  
  <pre><code>$lob = oci_new_descriptor($conn, OCI_D_LOB);
      $stmt = oci_parse($conn, "INSERT INTO ALLEGATI_SPESE (NUMRIGA, COMMESSA, RISORSA, DATA, DATA_INS, NOME, ESTENSIONE, TIPO, DIMENSIONE, ALLEGATO) "        
          ."VALUES(:NUM,:COM,:RIS,TO_DATE(:DATA,'DD/MM/YYYY'),DEFAULT,:NOME,:EXT,:TIPO,:DIMENSIONE,empty_blob()) returning allegato into :ALLEGATO");
  
          oci_bind_by_name($stmt, ":NUM", $numriga);
          oci_bind_by_name($stmt, ":COM", $commessa);
          oci_bind_by_name($stmt, ":RIS", $risorsa);
          oci_bind_by_name($stmt, ":DATA", $data);
          oci_bind_by_name($stmt, ":NOME", $name);
          oci_bind_by_name($stmt, ":EXT", $estensione);
          oci_bind_by_name($stmt, ":TIPO", $tipo);
          oci_bind_by_name($stmt, ":DIMENSIONE", $dimensione);
          oci_bind_by_name($stmt, ":ALLEGATO", $lob, -1, OCI_B_BLOB);
          oci_execute($stmt, OCI_DEFAULT);
  </code></pre>
  
  <p>Finally there is the part where the image is actually being saved into the db, also there is a resize but it isn't affecting the file size.(this resize function it's here for other reasons and I'm aware that it's not affecting the file ;) ) I kinda want to do the same thing but before the file gets saved.</p>
  
  <pre><code>    if ($lob-&gt;savefile($_FILES['lob_upload']['tmp_name'])) {
          $blob=$lob-&gt;load();
          $image = new Imagick();
          $image-&gt;readImageBlob($blob);
  
          $width = $image-&gt;getImageWidth();
          $height=$image-&gt;getImageHeight();
          $scale=$width/$height;
          $height=1000/$scale;
          $image-&gt;resizeImage(1000,$height,Imagick::FILTER_LANCZOS,1);
          $lob-&gt;save($image);
          oci_commit($conn);
  
      }else{
          // echo "Errore";
      }
      $lob-&gt;free();
  </code></pre>
  
  <p>So my final question: is there a way to process/compress the file before it gets saved into the database?
  I've already tried to assign this</p>
  
  <pre><code>$_FILES['lob_upload']['tmp_name']
  </code></pre>
  
  <p>to a variable in order to then apply some Imagick() compression functions to it but it's not working.</p>
  
  <p>That's all, if there is something not clear just let me know, thanks in advance for your time.</p>
  
  <p>ps: this is an example of what I want to do, eccept that I'm using an Oracle Database and at the moment I can't enable GD Library but only Imagick
  <a href="http://talkerscode.com/webtricks/reduce%20the%20size%20and%20make%20thumbnail%20of%20any%20image%20before%20uploading%20using%20PHP.php" rel="nofollow noreferrer">http://talkerscode.com/webtricks/reduce%20the%20size%20and%20make%20thumbnail%20of%20any%20image%20before%20uploading%20using%20PHP.php</a></p>