<?

include ("connection.php");
print "<title>ANISEED CURATOR - Add Morphogenetic Process Picture</title>";

$morphoid=$_POST["id"];

$t="/tmp2/aniseed/";
$file=tempnam($t, "PICTMP");


if (! move_uploaded_file($_FILES['picfile']['tmp_name'], $file)) {
   print "error: ";
   print_r($_FILES);
}

unset($picfile);


	
	list($width, $height, $type, $attr) = getimagesize($file);
	
    if( $type !=2 ){
	    print "<br><center><strong>Error: invalid format.</strong></center>";
		print '</center></body></html>';
		return;
	}
	
    $size = 397;  // new image width

    if ($type == 2) {    
      $src = imagecreatefromjpeg($file);
	
    }
	
	
    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;
    

	if ($width <= $size) {
	  $new_w = $width;
	  $new_h = $height;
	} else {
	  $new_w = $size;
	  $new_h = abs($new_w * $aspect_ratio);
	}

	
	$img = imagecreatetruecolor($new_w,$new_h); 
    imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);


	$temp1 = tempnam("/tmp2/aniseed/","");
	 Imagejpeg($img,$temp1);;



$f1=fopen($file,"r");
$text1=fread($f1,filesize($file));
fclose($f1);


pg_query($c,"begin"); /*Debut de la transaction*/

						$oid1=pg_lo_create($c); /*Creation de l'oid de la sequence*/


$query="insert into \"ARTICLE_MORPHOGEN_PICTURE\" (\"PICTURE_MORPHOGEN_ID\", \"MORPHOGEN_ID\", \"PICTURE\") values(nextval('\"S_PICTURE_MORPHOGEN_ID\"'), '$morphoid', '$oid1')";


$stmt = pg_query($c,$query) or die ("<strong> ERROR </strong>".pg_last_error($c));

	$handler=pg_lo_open($c,$oid1,"w");
	pg_lo_write($handler,$text1);
	pg_lo_close($handler);

pg_query($c,"commit");



?>

<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>
