Page 1 of 4

Watermark all tif images

Posted: 2014-06-05T04:53:31-07:00
by cappsie
Hi,

I have lots of tif pages which need to be watermarked as-and-when they are received. Ideally this will be done via a webpage - either html or php - and must include a text option with a date field (perhaps with optional pop-up) which can later be passed onto the image as the watermark; something like "Graded - 01/01/2014". Finally, the output must be to the page or a link to download (or both).

I've read this thread (viewtopic.php?f=1&t=23965) which seems promising, but I'm not brilliant with PHP so could use a good nudge the right direction. I'm running Apache on Windows server.

Thanks,
Adam.

Re: Watermark all tif images

Posted: 2014-06-05T05:20:19-07:00
by Bonzo
Here is a simple example of watermarking an image on upload:

Code: Select all

<?php

function clean($name, $max) {
// Remove everything except letters numbers . and @ in the variable
preg_replace("/[^A-Za-z0-9.\-_@]/","",$name);

// Do not allow excessively long entries - length set in function call
// Could use a word wrap here - add a \n after a certain amount of characters
$name = substr($name, 0, $max);
return $name;
}

// If the form has been submitted do this
if(isset($_FILES['filename']['tmp_name'])) {
 
// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name'];

// Name to save the image as - in this case the same as the original
// The same folder as the code
$new_image = $_FILES['filename']['name'];  

// Cleanup the text.
$text_submitted = clean ( $_POST['text_submitted'], 18 );

// Build the imagemagick command - using rgba so the opacity of the text can be set
$cmd = "$original_image -pointsize 50 -font arial.ttf -fill rgba\(0,0,0,0.4\) ".
" -gravity center -annotate +0+0 \"$text_submitted\" ";

// Coment out to display the command - good for debugging
//echo $cmd;

// Setup the array for the error reporting
$array = array();

// The array is to hold any errors you may get
// Coment out the if function after debugging
exec("convert $cmd  $new_image 2>&1", $array);
if ( !empty($array) ){
echo "<br />There were some errors during the Imagemagick conversion:<br />
<pre><br />".print_r($array)."<br>"; 
echo "</pre>";
}
            }                      
else { ?>

<form method="post" action="<?php echo  $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<label>File to upload</label> 
<input type="file" name="filename"  /> 
<label>Text to add to the photo</label>
<input type="text" name="text_submitted"  /><br />
<input type="Submit" name="Submit" value="Submit" />
</form>
 <?php } ?> 
It is quite old and would need some security/validation added.

Re: Watermark all tif images

Posted: 2014-06-05T07:24:32-07:00
by cappsie
Hi,

So far so good, at least until I hit an error with a blank array:

Code: Select all

Array ( [0] => Invalid Parameter - -pointsize ) 
There were some errors during the Imagemagick conversion:

1
The variable is:

Code: Select all

$cmd = "$original_image -pointsize 12 -font arial.ttf -fill rgba\(0,0,0,0.4\) "." -gravity center -annotate +0+0 \"$text_submitted\" ";
I thought of manually running the command:

Code: Select all

convert image.jpg -pointsize 12 -font arial.ttf -fill rgba(0,0,0,0.4) -gravity center -annotate +0+0 "TEST"
But I was unsuccessful with the error "convert.exe: unable to read font `ARIAL.TTF' @ warning/annotate.c/RenderType/872"


Which file types are supported?

Thanks,
Adam.

Re: Watermark all tif images

Posted: 2014-06-05T07:31:34-07:00
by cappsie
This worked:

Code: Select all

C:\TEST>convert image.jpg -pointsize 12 -font C:\Windows\Fonts\ARIAL.TTF -fill rgba(0,0,0,0.4) -gravity center -annotate +0+0 "TEST" test.jpg
I believe this should work, but doesn't:

Code: Select all

$cmd = "$original_image -pointsize 50 -font C:\\Windows\\Fonts\\ARIAL.TTF -fill rgba\(255,250,205\) " . " -gravity center -annotate +0+0 \"$text_submitted\" ";
Thanks,
Adam.

Re: Watermark all tif images

Posted: 2014-06-05T10:37:50-07:00
by Bonzo
I missed the windows server part; you do not need to escape the ( and ) so \( and \) should be ( and ). Also wherever you see a ' you will need to use a " I usually write my code taking that into account anyway as I use Imagemagick on my Windows localhost.

"Code" .
" More code ";
The above allows you to carry the code across multiple lines when it gets to long for the screen and makes it easier to read.

You may have arial as a Imagemagick recognised font; you can find out which fonts are recognised using this code:

Code: Select all

<?php 
// Select the version number from the configeration file
preg_match('/^LIB_VERSION_NUMBER ([0-9,]+)$/m', shell_exec("convert -list configure "), $vnums);

// Seperate the numbers at the ,
$number = explode( ",", $vnums[1] );

// Format the version from 6,3,4,1 format to 06030401
$version = "";

for($i=0;$i<4;$i++)
{
$version .= str_pad( $number[$i], 2, '0', STR_PAD_LEFT );
} 

// The 'list' method used to get the fonts changed in IM v6.3.5-7
$font_list = ( $version > "06030507" ) ? "font" : "type";

// Display the version of Imagemagick, the method to read the fonts and the list of fonts
echo "<h2>IM version: ".$version." </h2>\n
<h3>Method used: convert -list $font_list</h3>\n
<hr>\n<pre>";
system("convert -list $font_list");
echo "</pre>\n<hr>";
?> 

Re: Watermark all tif images

Posted: 2014-06-06T00:39:12-07:00
by cappsie
Hi,

Thanks for that :) Oddly the script failed though with this error:
IM version: 00000000
Method used: convert -list type
--------------------------------------------------------------------------------
Invalid Parameter - type
But more importantly, the command I'm using, it seems php doesn't like the parameters I'm passing it:

Code: Select all

$cmd = "$original_image -pointsize 70 -font ARIAL.TTF -fill rgba(0,0,0) "." -gravity North -annotate +0+0 \"$text_submitted\" out.tif" ;
Thanks,
Adam.

Re: Watermark all tif images

Posted: 2014-06-06T01:35:56-07:00
by snibgo
cappsie wrote:-font ARIAL.TTF -fill rgba(0,0,0)
I expect you need to provide the full path to ARIAL.TTF.

EDIT: I should say that if IM knows about the font, you should give its name, which isn't the filename.

rgba() needs 4 parameters. If you have only three, you probably want rgb().

Re: Watermark all tif images

Posted: 2014-06-06T02:44:26-07:00
by cappsie
Hi

I've updated the command to:

Code: Select all

$cmd = "$original_image -pointsize 70 -font C:\Windows\ARIAL.TTF -fill rgb(0,0,0) "." -gravity North -annotate +0+0 \"$text_submitted\" out.tif" ;
I'm still getting the error:
Array ( [0] => Invalid Parameter - -pointsize )
There were some errors during the Imagemagick conversion:

1
There was some suggestion that Windows doesn't know the IM 'convert.exe' without being explicitly told. I.e. "C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe" - unfortunately I wasn't sure of how to escape the space so I tried this:

Code: Select all

exec("'C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe' $cmd  $new_image 2>&1", $array);
Which then threw up another error:
Array ( [0] => The filename, directory name, or volume label syntax is incorrect. )
There were some errors during the Imagemagick conversion:

1
So this:

Code: Select all

exec("convert.exe $cmd  $new_image 2>&1", $array);
Would become this:

Code: Select all

exec("C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe $cmd  $new_image 2>&1", $array);
Thoughts welcome :D

Thanks,
Adam

Re: Watermark all tif images

Posted: 2014-06-06T09:42:25-07:00
by snibgo

Code: Select all

Invalid Parameter - -pointsize
That's an error from Windows convert program, not ImageMagick.

I think you should get your code to print out the contents of the entire string given to exec. It simplifies debugging.

And start off simple: "convert logo: x.png" etc.

Re: Watermark all tif images

Posted: 2014-06-06T11:13:14-07:00
by Bonzo
Have you had anything working with php and exec() ?

Windows has a convert program for changing folder names? You should not be picking it up but what a lot of windows users do is rename the Imagemagick convert program to something like IMconvert and change the code to:

Code: Select all

exec("IMconvert $cmd  $new_image 2>&1", $array);

Re: Watermark all tif images

Posted: 2014-06-06T13:13:08-07:00
by cappsie
snibgo wrote:

Code: Select all

Invalid Parameter - -pointsize
That's an error from Windows convert program, not ImageMagick.

I think you should get your code to print out the contents of the entire string given to exec. It simplifies debugging.

And start off simple: "convert logo: x.png" etc.
I wouldn't know how to do that using PHP :(

Re: Watermark all tif images

Posted: 2014-06-06T13:49:49-07:00
by cappsie
Bonzo wrote:Have you had anything working with php and exec() ?

Windows has a convert program for changing folder names? You should not be picking it up but what a lot of windows users do is rename the Imagemagick convert program to something like IMconvert and change the code to:

Code: Select all

exec("IMconvert $cmd  $new_image 2>&1", $array);
I renamed convert.exe to IMconvert.exe as you suggested. So a new error:
Array ( [0] => 'IMconvert' is not recognized as an internal or external command, [1] => operable program or batch file. )
There were some errors during the Imagemagick conversion:

1
Is there a way to turn track each step? Perhaps by echoing out the raw variables?

Re: Watermark all tif images

Posted: 2014-06-06T14:18:46-07:00
by cappsie
Hi,

Out of desperation, I uninstalled and reinstalled ImageMagick and to th newer path: C:\ImageMagick.

I updated the code:

Code: Select all

exec("C:\ImageMagick\IMconvert.exe $cmd  $new_image 2>&1", $array);
Perhaps we're getting somewhere now as there was a new error:
Array ( [0] => IMconvert.exe: unable to read font `C:\Windows\ARIAL.TTF' @ warning/annotate.c/RenderType/872. [1] => IMconvert.exe: unable to open image `out.tif': No such file or directory @ error/blob.c/OpenBlob/2658. [2] => IMconvert.exe: unable to open image `Futurama': No such file or directory @ error/blob.c/OpenBlob/2658. [3] => IMconvert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. [4] => IMconvert.exe: unable to open image `Full': No such file or directory @ error/blob.c/OpenBlob/2658. [5] => IMconvert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. [6] => IMconvert.exe: unable to read font `C:\Windows\ARIAL.TTF' @ warning/annotate.c/RenderType/872. )
There were some errors during the Imagemagick conversion:
1
Is there a very basic ImageMagick php script which will work with no parameters but just the 'convert' part?

Thanks,
Adam

Re: Watermark all tif images

Posted: 2014-06-06T14:30:34-07:00
by fmw42
I would suggest you write your command with the exact arguments inserted so that it is easier for others to duplicate. Have you checked your delegates? Do they include libtiff. You can list them with convert -version. Are you sure you are referencing the correct version/location of IM. You might include the full path to IM or check to see how may versions you have installed and where with type -a convert or which convert.

Code: Select all

<?php
echo "<pre>";
system("type -a convert");  
echo "</pre>";
?> 

Code: Select all

<?php
echo "<pre>";
system("which -a convert");  
echo "</pre>";
?> 

Once you find where your IM convert is located, then do

Code: Select all

<?php
exec("fullpath2/convert -version",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
Where for example it might be at /usr/local/bin (on a unix system), so

Code: Select all

<?php
exec("/usr/local/bin/convert -version",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
Once you find out if that is the version you want to use or installed, then use the full path to convert in your other commands.

Re: Watermark all tif images

Posted: 2014-06-06T14:40:48-07:00
by snibgo
exec("C:\ImageMagick\IMconvert.exe $cmd $new_image 2>&1", $array);
Debugging is difficult without knowing the value of $cmd and $new_image. Why don't you put the first argument to exec() into a string and echo it?