title: How to convert contents of .tmp file to png
body: <p>I have a PHP script that, if a user-uploaded image file isn't a .png image, it will use Imagemagick to convert it to .png before saving it to a server. However it can only use the .tmp file from the HTML form, so it has to convert the CONTENTS while keeping the .tmp (and its filename) intact. This is my code so far:</p>
  
  <pre><code>if (exif_imagetype($tmpName) != IMAGETYPE_PNG) {
      $uploadOk = 0; // don't upload later
      $im = new Imagick("$tmpName");
      $im-&gt;setImageFormat( "png" );
      file_put_contents($tmpName, '');
      file_put_contents(file_get_contents($im), $tmpName);
      if (exif_imagetype($tmpName) != IMAGETYPE_PNG) { //check again
          // try again or throw error somehow?
      } else {
          $uploadOk = 1;
      }
  } else {
      $uploadOk = 1;
  }
  </code></pre>
  
  <p>How do I properly use Imagick to convert the contents of my temp file into a png version of the original (non png) image? </p>
  
  <p>EDIT: It could also be that the script should work fine, and the problem is something entirely different. Not sure at this point.</p>