Source of Image Encrypter

crypt.inc.php

<?php

// Image Encrypter / Tim <http://pwnt.be/>, 2008-01-01

$color_mapping = array('red''green''blue');

function 
merge_images ($background$secret) {
    
$width imagesx($secret);
    
$height imagesy($secret);
    
$image imagecreatetruecolor($width$height);
    
$random_colors = !isset($background);
    
$background_tc = (!$random_colors
        
? (imagecolorstotal($background) == 0)
        : 
false);
    
$secret_tc = (imagecolorstotal($secret) == 0);
    for (
$x 0$x $width$x++) {
        for (
$y 0$y $height$y++) {
            
$full = ($random_colors
                
random_color()
                : 
get_color($background$x$y$background_tc));
            
$gray to_grayscale(get_color($secret$x$y$secret_tc));
            
$color merge_colors($full$gray);
            
$color imagecolorallocate($image,
                
$color['red'], $color['green'], $color['blue']);
            
imageline($image$x$y$x$y$color);
        }
    }
    return 
$image;
}

function 
merge_colors ($full$gray) {
    global 
$color_mapping;
    for (
$i 0$i 3$i++) {
        
$color $color_mapping[$i];
        
$lim = ($i == 3);
        for (
$j 0$j $lim$j++) {
            
$gray_bit = ($gray & (<< ($i $j)));
            if (
$gray_bit$full[$color] |= (<< $j);
            else 
$full[$color] &= ~(<< $j);
        }
    }
    return 
$full;
}

function 
decrypt_image ($img) {
    
$width imagesx($img);
    
$height imagesy($img);
    
$image imagecreatetruecolor($width$height);
    
$tc = (imagecolorstotal($img) == 0);
    for (
$x 0$x $width$x++) {
        for (
$y 0$y $height$y++) {
            
$color get_color($img$x$y$tc);
            
$color decrypt_color($color);
            
$color imagecolorallocate($image$color$color$color);
            
imageline($image$x$y$x$y$color);
        }
    }
    return 
$image;
}

function 
decrypt_color ($color) {
    global 
$color_mapping;
    
$result 0;
    for (
$i 0$i 3$i++) {
        
$c $color[$color_mapping[$i]];
        
$lim = ($i == 3);
        for (
$j 0$j $lim$j++) {
            
$bit $c & (<< $j);
            if (
$bit$result |= << ($i $j);
        }
    }
    return 
$result;
}

function 
get_color ($image$x$y$full) {
    
$color imagecolorat($image$x$y);
    if (
$full) return to_rgb($color);
    return 
imagecolorsforindex($image$color);
}

function 
to_grayscale ($color) {
    return 
round(($color['red'] + $color['green'] + $color['blue']) / 3);
}

function 
to_rgb ($num) {
    
$r = ($num >> 16) & 0xFF;
    
$g = ($num >> 8) & 0xFF;
    
$b $num 0xFF;
    return array(
'red' => $r'green' => $g'blue' => $b);
}

function 
same_size ($img1$img2) {
    return (
imagesx($img1) == imagesx($img2)
        && 
imagesy($img1) == imagesy($img2));
}

function 
random_color () {
    global 
$color_mapping;
    
$color = array();
    foreach (
$color_mapping as $c)
        
$color[$c] = rand(0255);
    return 
$color;
}

function 
create_image ($filename$type) {
    switch (
$type) {
        case 
'gif':
            return 
imagecreatefromgif($filename);
        case 
'jpg':
        case 
'jpeg':
            return 
imagecreatefromjpeg($filename);
        case 
'png':
            return 
imagecreatefrompng($filename);
    }
    return 
null;
}

function 
get_extension ($filename) {
    return 
strtolower(substr($filenamestrrpos($filename'.') + 1));
}

?>

encrypt.php

<?php

require_once 'crypt.inc.php';

$bg_file $_FILES['background'];
$ol_file $_FILES['image'];

$bg_path $bg_file['tmp_name'];
$ol_path $ol_file['tmp_name'];

if (!isset(
$ol_file) || !is_uploaded_file($ol_path))
    die(
'File upload error');

if (isset(
$bg_file) && is_uploaded_file($bg_path)) {
    
$background_img = @create_image($bg_pathget_extension($bg_file['name']));
    if (!
$background_img)
        die(
'Background image error');
}
else
    
$background_img null;

$overlay_img = @create_image($ol_pathget_extension($ol_file['name']));

if (!
$overlay_img)
    die(
'Image error');

if (!
is_null($background_img) && !same_size($background_img$overlay_img))
    die(
'Image dimensions differ: '
        
imagesx($background_img) . 'x' imagesy($background_img) . ' vs. '
        
imagesx($overlay_img) . 'x' imagesy($overlay_img));

$image merge_images($background_img$overlay_img);
$filename sprintf('%X'time()) . '.png';

header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="' $filename '"');
imagepng($image);

?>

decrypt.php

<?php

require_once 'crypt.inc.php';

$file $_FILES['image'];
$path $file['tmp_name'];

if (!isset(
$file) || !is_uploaded_file($path))
    die(
'File upload error');

$img = @imagecreatefrompng($path);

if (!
$img)
    die(
'Image error');

$image decrypt_image($img);
$filename preg_replace('/(\.[^.]+)$/''-decrypted$1'$file['name']);

header('Content-Type: image/png');
header('Content-Disposition: inline; filename="' $filename '"');
imagepng($image);

?>