title: Using imagejpeg to save & serve image file
body: <p>I'm doing a bit of an experiment with PHP + Image manipulation.  I'm trying to convert some images into black and white versions.  I'm mostly figured it out but have one slight issue.</p>
  
  <p>In order to reduce the strain on the server, I wanted to save the B&amp;W versions and only run the image filtering on images that haven't been run through the script before.  So, I have something like this:</p>
  
  <pre><code>&lt;?php 
  header("Content-type: image/jpeg");
  
  $file = $_GET['img'];
  $name = md5($file).".jpg";
  
  if(file_exists("/path/to/file" . $name)) {
  
      ob_clean();
      flush();
      readfile("path/to/file" . $name);
      exit;
  
  }
  else {
  
   $image = imagecreatefromjpeg($file);
  
   imagefilter($image, IMG_FILTER_GRAYSCALE);
   imagejpeg($image, "/path/to/file" . $name);
  
   imagedestroy($image);
  };
  
  ?&gt; 
  </code></pre>
  
  <p>This does create the B&amp;W versions of the file and save them to the server.  The initial "if" statement is also working - it correctly serves the image if it already exists.</p>
  
  <p>The issue is that for new images that are run through, this saves them but doesn't output them to the browser.  What can I use/change in order to do that?</p>
  
  <p>Also, this is my first time doing anything like this.  Any general tips you have about doing the above would be appreciated.</p>