Solution Timeline

All versions (edits) of solutions to PHP script Image merge appear below in the order they were created. Comments that appear under revisions were those created when that particular revision was current.

To see the revision history of a single solution (with diffs), click on the solution number (ie. "#1") in the upper right corner of a solution revision below.

← Bounty Expand all edits

From what I gather, you need the script to serve the raw JPEG image when type=2. ("output to browser as data URI" isn't really the right way to put it.)

The problem is that the calls to debug_to_console print plain text, so you end up with the binary data of the image being intertwined with text, which creates an invalid image.

A simple solution (albeit bad programming) is to silence the function when $myReturnType == 2:

function debug_to_console( $data ) {
    global $myReturnType;
    if ($myReturnType == 2) {
        return;
    }
    $output = $data;
    if ( is_array( $output ) )
        $output = implode( ',', $output);
    echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}

The script will then correctly output the image.

Winning solution

If nothing works, replace the code block at line 88

else if ($myReturnType == 2) {  // output to browser as data URI
    header("Content-Type: image/jpeg");
    imagejpeg($image_1, NULL, $myQuality);
    debug_to_console("2");
}

with

else if ($myReturnType == 2) {  // output to browser as data URI
    echo '<img src="'.$myImage.'" width="'.$myWidthPX.'">';
    debug_to_console("2");
}

This simply looks consistant with type 1 and type 3

Regards.

If nothing works, replace the code block at line 88

else if ($myReturnType == 2) {  // output to browser as data URI
    header("Content-Type: image/jpeg");
    imagejpeg($image_1, NULL, $myQuality);
    debug_to_console("2");
}

with

else if ($myReturnType == 2) {  // output to browser as data URI
    echo '<img src="'.$myImage.'" width="'.$myWidthPX.'">';
    debug_to_console("2");
}

This simply looks consistant with type 1 and type 3

Regards.

Tip: For better programming

It would usually show Undefined offset: 1 during testing at line 25

Divide the call debug_to_console at line 25 between the above if condition as follows

if ($myLock == "yes") {
    $myImageArray = array($myImage,$myLockImage);
    debug_to_console("myImageArray - ".$myImageArray[0]." & ".$myImageArray[1]);
}else {
    $myImageArray = array($myImage);
    debug_to_console("myImageArray - ".$myImageArray[0]);
}