Search code examples
imagicksteganography

Imagick::steganoImage() Unable to show the watermark


I'm trying to use Imagick::steganoImage with an example image from wikimedia commons. If I try to show the decoded image of the watermark, I always obtain the 1 pixel image you can see below, whatever image I choose as source. Anyone may help me to understand why and how I can sort this out?

enter image description here

<?
header("Content-Type: image/png");   
// Create a new imagick object 
$image = new Imagick('https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Frostedbubble2.jpg/640px-Frostedbubble2.jpg'); 
$watermark = new Imagick('LABEL:Hello World!'); 

// The decoding process must "know" about the watermarks size, and starting
// pixel offset.
define('STEGANO_OFFSET', 64); // Secret offset
define('STEGANO_WIDTH', $watermark->getImageWidth());
define('STEGANO_HEIGHT', $watermark->getImageHeight());

$stegano = $image->steganoImage($watermark, STEGANO_OFFSET);
$stegano->writeImage('output.png');

$decoded = new Imagick();
$decoded->setSizeOffset(STEGANO_WIDTH, STEGANO_HEIGHT, STEGANO_OFFSET);
$decoded->readImage('STEGANO:output.png');
$decoded->writeImage('decoded.png');

// Show the output 
$decoded->setImageFormat('png'); 
echo $decoded->getImageBlob(); 
?> 

I tried also the code at this page https://www.geeksforgeeks.org/php-imagick-steganoimage-function/ and the geeksforgeeks image is shown correctly but the stegano image appear as totally black.

<?php 

// Create a new imagick object 
$imagick = new Imagick( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 

// Create another Imagick object containing watermark 
$watermark = new Imagick('label:This is my secret.'); 

// Hide $watermark inside $imagick 
$imagick = $imagick->steganoImage($watermark, 64); 

// Write image to the local folder 
$imagick->writeImage('output.png'); 

// Set the offset 
$imagick->setSizeOffset($watermark->getImageWidth(), 
                   $watermark->getImageHeight(), 64); 

// Read the encoded image and extract secret 
$imagick->readImage('STEGANO:output.png'); 

// Show the output 
$imagick->setImageFormat('png'); 
header("Content-Type: image/png"); 
echo $imagick->getImageBlob(); 
?>

Solution

  • Looks like new Imagick('LABEL:Hello World!'); is no longer enough for a simple label. You'll need to set the size of the canvas, and the point size BEFORE reading the label.

    define('STEGANO_OFFSET', 64); // Secret offset
    define('STEGANO_WIDTH', 88);
    define('STEGANO_HEIGHT', 14);
    
    $watermark = new Imagick();
    $watermark->setSize(STEGANO_WIDTH, STEGANO_HEIGHT);
    $watermark->setPointSize(16);
    $watermark->readImage('LABEL:Hello World!');
    
    $stegano = $image->steganoImage($watermark, STEGANO_OFFSET);
    

    I can't speak when/why this changed, but if you run the following...

    convert 'label:Hello World!' decoded.png
    

    ... you would have the same image previously extracted from STEGANO: