Watermark all tif images
Watermark all tif images
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.
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
Here is a simple example of watermarking an image on upload:
It is quite old and would need some security/validation added.
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 } ?>
Re: Watermark all tif images
Hi,
So far so good, at least until I hit an error with a blank array:
The variable is:
I thought of manually running the command:
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.
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
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\" ";
Code: Select all
convert image.jpg -pointsize 12 -font arial.ttf -fill rgba(0,0,0,0.4) -gravity center -annotate +0+0 "TEST"
Which file types are supported?
Thanks,
Adam.
Re: Watermark all tif images
This worked:
I believe this should work, but doesn't:
Thanks,
Adam.
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
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\" ";
Adam.
Re: Watermark all tif images
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" .
" 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
Hi,
Thanks for that
Oddly the script failed though with this error:
Thanks,
Adam.
Thanks for that

But more importantly, the command I'm using, it seems php doesn't like the parameters I'm passing it:IM version: 00000000
Method used: convert -list type
--------------------------------------------------------------------------------
Invalid Parameter - type
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" ;
Adam.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Watermark all tif images
I expect you need to provide the full path to ARIAL.TTF.cappsie wrote:-font ARIAL.TTF -fill rgba(0,0,0)
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().
snibgo's IM pages: im.snibgo.com
Re: Watermark all tif images
Hi
I've updated the command to:
I'm still getting the error:
Which then threw up another error:
Would become this:
Thoughts welcome 
Thanks,
Adam
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" ;
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:Array ( [0] => Invalid Parameter - -pointsize )
There were some errors during the Imagemagick conversion:
1
Code: Select all
exec("'C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe' $cmd $new_image 2>&1", $array);
So this:Array ( [0] => The filename, directory name, or volume label syntax is incorrect. )
There were some errors during the Imagemagick conversion:
1
Code: Select all
exec("convert.exe $cmd $new_image 2>&1", $array);
Code: Select all
exec("C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe $cmd $new_image 2>&1", $array);

Thanks,
Adam
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Watermark all tif images
Code: Select all
Invalid Parameter - -pointsize
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.
snibgo's IM pages: im.snibgo.com
Re: Watermark all tif images
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:
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
I wouldn't know how to do that using PHPsnibgo wrote:That's an error from Windows convert program, not ImageMagick.Code: Select all
Invalid Parameter - -pointsize
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
I renamed convert.exe to IMconvert.exe as you suggested. So a new error: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);
Is there a way to turn track each step? Perhaps by echoing out the raw variables?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
Re: Watermark all tif images
Hi,
Out of desperation, I uninstalled and reinstalled ImageMagick and to th newer path: C:\ImageMagick.
I updated the code:
Perhaps we're getting somewhere now as there was a new error:
Thanks,
Adam
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);
Is there a very basic ImageMagick php script which will work with no parameters but just the 'convert' part?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
Thanks,
Adam
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Watermark all tif images
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.
Once you find where your IM convert is located, then do
Where for example it might be at /usr/local/bin (on a unix system), so
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.
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>";}
?>
Code: Select all
<?php
exec("/usr/local/bin/convert -version",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Watermark all tif images
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?exec("C:\ImageMagick\IMconvert.exe $cmd $new_image 2>&1", $array);
snibgo's IM pages: im.snibgo.com