PHP create thumbnail from uploaded file and moving to server
<p>I have encountered a problem with creating a thumbnail from an uploaded image file, then to upload it to the server.</p>

<p>As of now, I have a function that creates the thumbnail, saves it as a temp and returns it to the caller.</p>

<p>Then what I try to do, is upload that created thumb image with move_uploaded_file(tempthumb, path);</p>

<p>Here is the createThumb function and the caller:</p>

<pre><code>function createThumb( $image, $thumbWidth )
{
  // load image and get image size
  $img = imagecreatefromjpeg( "{$image}" );
  $width = imagesx( $img );
  $height = imagesy( $img );

  // calculate thumbnail size
  $new_width = $thumbWidth;
  $new_height = floor( $height * ( $thumbWidth / $width ) );

  // create a new temporary image
  $tmp_img = imagecreatetruecolor( $new_width, $new_height );

  // copy and resize old image into new image 
  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

  return $tmp_img;

}

return $tmp_img; // Here I return the new image. Is this the proper way to get a binary image back??
</code></pre>

<p>Here is the caller:</p>

<pre><code>$thumb = createThumb($_FILES['propform-previmg']['tmp_name'], $max_previmg_width); 
        $filenamepath = $src_dir . '/thumb/' . $_FILES['propform-previmg']['name'];
        if ( !move_uploaded_file($thumb, $filenamepath ))
            echo "Error moving file {$filenamepath}";
</code></pre>

<p>I tried to just upload the uploaded file directly without trying to make a thumbnail first, and that worked fine. So I guess there is some error with the variable I return from the createThumb function, but I can't figure out exactly what. </p>

<p>Also, I need to do the upload from the caller code, and not inside the createThumb function with imagejpeg(file, path).</p>

<p>Thank you!</p>
