title: Image resizing from a database with PHP
body: <p>I currently have a script which will pull in image contents from a database and, using createimagefromstring(), returns a resized (resampled) image for display:</p>
  
  <pre><code>$max_width = 170;
  
  // Get the image
  
  @mysql_connect('host', 'user', 'pass');
  @mysql_select_db('db');
  
  $query = "SELECT `thumbnail_contents`, `thumbnail_mimetype` FROM `videos` WHERE video_number  = '$video_number'";
  $result = mysql_query($query) or die(mysql_error());
  $row = mysql_fetch_object($result);
  
  // Resize if pic bigger than $max_width
  // Set the header
  header('Content-type:' . $row-&gt;thumbnail_mimetype);
  
  // Get original size of image
  $image = imagecreatefromstring($row-&gt;thumbnail_contents);
  $current_width = imagesx($image);
  $current_height = imagesy($image);
  
  // Set thumbnail width
  $widths = array($current_width, $max_width);
  $new_width = min($widths);
  
  // Calculate thumbnail height from given width to maintain ratio
  $new_height = $current_height / $current_width*$new_width;
  
  // Create new image using thumbnail sizes
  $thumb = imagecreatetruecolor($new_width,$new_height);
  
  // Copy original image to thumbnail
  imagecopyresampled($thumb,$image,0,0,0,0,$new_width,$new_height,imagesx($image),imagesy($image));
  
  // Show thumbnail on screen
  $show = imagejpeg($thumb);
  
  // Clean memory
  imagedestroy($image);
  imagedestroy($thumb);
  </code></pre>
  
  <p>Currently, this script works for most thumbnails and shows them on-screen as desired.  However, when I upload an image with a high width, say over 1000px, it gets stored in the database, but the script doesn't show the resampled image.</p>
  
  <p>To troubleshoot, I've checked the db, as well as print_r'd the output from the query.  Everything seems to be kosher...  Could someone tell me what might be causing such behavior?  Any help or guidance is greatly appreciated!</p>