resize files after being uploaded
<p>im trying to resize files being uploaded all this with php, 
here's the code that writes the images to the disk</p>

<pre><code>    if (isset($_FILES['file']['tmp_name']) &amp;&amp; is_uploaded_file($_FILES['file']['tmp_name'])) {
    // Open temp file
    $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
    if ($out) {
    // Read binary input stream and append it to temp file
    $in = fopen($_FILES['file']['tmp_name'], "rb");
    if ($in) {

    while ($buff = fread($in, 4096)) {
    fwrite($out, $buff);
            }

        }
    else        
 die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
    fclose($in);
    fclose($out);
    @unlink($_FILES['file']['tmp_name']);
    } else
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');

        }
</code></pre>

<p>my resize function goes here:</p>

<pre><code>if ($in) {
while ($buff = fread($in, 4096)) {
       fwrite($out, $buff);
    }
resize();
</code></pre>

<p>resizing does happen but the images are black</p>

<p>the resize function:</p>

<pre><code>function resize(){

$dir="photos/".$_COOKIE["event_id"]."/";
$img= array_slice(scandir("photos/".$_COOKIE["event_id"]."/"),2);


 foreach($img as $_img){
//  echo  $_img;
   list($width, $height, $type, $attr) = getimagesize($dir.$_img);
//echo  $width.'px  &lt;br /&gt;';

if($width &gt; 630){

    if($type=="2"){
        $new_image=  imagecreatefromjpeg($dir.$_img);
        $newwidth=630;
      $newheight=($height/$width)*$newwidth;
      $tmp=imagecreatetruecolor($newwidth,$newheight);
       imagecopyresampled($tmp,$new_image,0,0,0,0,$newwidth,$newheight,$width,$height);
       imagejpeg($tmp,$dir.$_img,100);

     }

         if($type=="1"){
        $new_image=  imagecreatefromgif($dir.$_img);
        $newwidth=630;
      $newheight=($height/$width)*$newwidth;
      $tmp=imagecreatetruecolor($newwidth,$newheight);
        imagecopyresampled($tmp,$new_image,0,0,0,0,$newwidth,$newheight,$width,$height);
      imagegif($tmp,$dir.$_img);
       }

                if($type=="3"){
        $new_image=  imagecreatefrompng($dir.$_img);
        $newwidth=630;
      $newheight=($height/$width)*$newwidth;
      $tmp=imagecreatetruecolor($newwidth,$newheight);
        imagecopyresampled($tmp,$new_image,0,0,0,0,$newwidth,$newheight,$width,$height);
      imagepng($tmp,$dir.$_img,6);

     }
   }
 }
}

}
</code></pre>
