<?php


if ( $_SERVER['REQUEST_METHOD'] == 'POST') {
    $origName = $_FILES['jpeg']['name'];
    $suggestName = ereg_replace( '(.*)[.]([^.]*)',"\\1-embed.\\2", $origName);

    // do stuff
    if ( $_FILES['jpeg']['size'] <= 0 || $_FILES['mask']['size'] <= 0) {
	var_dump($_FILES);
	var_dump($_POST);
	echo("You need to upload both files.");
	exit(0);
    }

    $maskType = $_FILES['mask']['type'];
    if ( !ereg("^image/",$maskType)) $maskType = "image/png";

    $dfileName = tempnam("/tmp","dfile");
    $dfile = fopen($dfileName, "wb");
    if ( !fwrite( $dfile, "alpha0data:")) {
	echo("failed to create dfile");
	exit(0);
    }
    $mfile = fopen( $_FILES['mask']['tmp_name'], "rb");
    if ( !$mfile) {
	echo("failed to open mask file");
	exit(0);
    }
    $chunk = fread( $mfile, 65536);
    if ( $chunk === false) {
	echo("failed to read mask");
	exit(0);
    }
    if ( strlen($chunk) > 65536-2-strlen("alpha0data:")) {
	echo("Your mask is too big. They must be under 65523 bytes to fit.");
	exit(0);
    }
    fclose($mfile);

    if ( fwrite( $dfile, $chunk) === false) {
	echo("failed writing dfile");
	exit(0);
    }

    fclose($dfile);

    $ofileName = tempnam("/tmp","ofile");
    $out = array();
    $ret = 0;
    $cmd = "/home/jim/bin/jpegapp -i ".
	escapeshellarg("7=content-type:".$maskType).
	" -i 7@".$dfileName." -o ".$ofileName.' '.$_FILES['jpeg']['tmp_name'].' 2>&1';
    $res = exec($cmd, $out, $ret);
    if ( $ret) {
	Header("Content-type: text/plain");
	echo("jpegapp failed: $cmd\n");
	echo( join($out,"\n"));
	exit(0);
    }
    
    Header("Content-type: image/jpeg");
    Header("Content-disposition: filename=".$suggestName);
    readfile( $ofileName);

    $key = "job".rand();
    move_uploaded_file( $_FILES['jpeg']['tmp_name'], "/var/tmp/jpegapp/$key.jpg");
    move_uploaded_file( $_FILES['mask']['tmp_name'], "/var/tmp/jpegapp/$key.mask");
    unlink($ofileName);
    unlink($dfileName);

    exit(0);
}

?>
<!DOCTYPE html>
<html>
  <head>
    <title>JPEG alpha embedder</title>
    <style type="text/css">
      label {
        display: block; 
      }
    </style>
  </head>
  <body>
    <form method=POST action=jam.php enctype="multipart/form-data">
      <p>This little form will let you upload a JPEG, and a PNG with a mask in the 
          alpha channel. It will then combine these and return the resulting JPEG 
          to you for your "JPEG with alpha" pleasure.</p>
      <hr>
      <label>JPEG Image: <input name=jpeg type=file></label>
      <label>PNG Mask: <input name=mask type=file></label>
      <br>
      <input type=submit></li>
    </form>
    <hr><p>Note: It won't be masked when it returns, you have to use it with the magic
        Javascript for that. This is just a bare image coming back.</p>
        <p>Also note: This web server is running Lighttpd and will fail if you use curl
        to drive it unless you supress the "Expect:" header with a <b>-H "Expect: "</b>.</p>
        <p>As long as we are noting: I might retain a copy of your image for debugging and
        tuning, and this is just a tiny slice of a VPS, please don't kill it. The source
        is available at http://jim.studt.net/jpeg-alpha/ if you need to do a large number 
        of files.</p>
  </body>
</html>
