Why doesn't watermarking work after server change?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
unvago

Why doesn't watermarking work after server change?

Post by unvago »

I use Imagemagick from within a php script to resize and watermark images. I
just moved to a new VPS server (Linux) and I find that while the below line of code still does the resizing OK, it no longer applies the watermark. I don't know
how to tell what version of Imagemagick is installed but it might be version
6. I suppose that the problem must be due to incompatibility of the code with a newer version of IM. Does anyone see anything wrong with the line below that could be causing the watermarking to fail?

Code: Select all

exec("/usr/bin/convert -geometry $image_width x $image_height -font
ariblk.ttf -fill \"#FFFF99\" -stroke \"#585855\" -strokewidth 1.25
-pointsize 25 -draw 'text 50,50 \"Watermark\"' $temp_name
$images_dir/$filename");
P.S. the ariblk font is in the same directory as the php script as it was on the old hosting. Everything in the document root directory is identical to the installation on the old server where it worked properly
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Why doesn't watermarking work after server change?

Post by Bonzo »

To display your version:

Code: Select all

<?php
echo "<pre>";
system("convert -version");  
echo "</pre>";
?> 
I would try your code like:

Code: Select all

exec("/usr/bin/convert $temp_name -geometry {$image_width}x{$image_height} -font ariblk.ttf -fill #FFFF99 -stroke #585855 -strokewidth 1.25 -pointsize 25 -draw 'text 50,50 \"Watermark\"' $images_dir/$filename");
The image to modify is read in first; you could just try something simple first with your texts to prove they work. I would put your font in the same directory as the code unless it is in the list created by:

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>";
?> 
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why doesn't watermarking work after server change?

Post by anthony »

Bonzo, you removed the quotes from the '#' colors. not a good idea, especially if the PHP system using a older bourne shell, rather than bash, such as a SUN solaris server. Remember I an a UNIX expert in scripting for different UNIX-like systems.

Also forget using -draw for text drawing, the double quoting is a pain especially with the PHP quoting around that again! Use -annotate instead.

If the string being annotated is user input, feeed that string to the command using a pipeline rather than as a argument. This does two things. One protects yourself from someone trying to hack the shell using shell meta characters and quotes (on purpose or accidentally), and second prevents IM expanding percent escapes. That is why 'read from file' method of annotation (IE -annotate @- or -annotate @filename.txt ) does not do percent escapes.

Of course any and all user input should be throughly tested by your program, but using strings from the user on a command line argument directly is just asking to be hacked.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
unvago

Re: Why doesn't watermarking work after server change?

Post by unvago »

I checked the IM version, it is Version: ImageMagick 6.0.7 05/03/07 Q16. If it makes any difference, the script was running successfully on the old server under PHP 5. It is running on PHP 5.2.3 on the new server. The font is in the same directory as the script on the new server as it was on the old server.

Bonzo, I tried your modified code and it worked the same as mine did. The resize worked, but drawing the watermark didn't.

As far as hacking is concerned, the image input to that line of code is thoroughly validated earlier in the script so I don't believe there is any danger.

Anyone have any other ideas? Maybe something is missing from the Imagemagick installation?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why doesn't watermarking work after server change?

Post by anthony »

The code also have extra spaces in the -geometry string. The image is not read in before it is operated on.

Basically the whole command is far to complex for IM v5 and it is a wonder it worked for either IM v5 or IM v6. Probably only worked as a fluke!

Hmmm try this.

Code: Select all

exec("/usr/bin/convert $temp_name -resize '${image_width}x${image_height}' -font ariblk.ttf -fill '#FFFF99' -stroke '#585855' -strokewidth 1.25 -pointsize 25 -annotate +50+50 'Watermark'  $images_dir/$filename");
It is very simular to Bonzo's, but more likely to work for machines with non-bash shells.

Note -annotate was added to IM during IM v6 development, so it should be present even on that really old version.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
unvago

Re: Why doesn't watermarking work after server change?

Post by unvago »

Anthony, I tried you code and it still does the same thing, it resizes but does not draw. Might it be possible to do the watermarking in sequential steps instead of all as one command? I hope that I do not need to install a newer version of IM. The thought of installing IM (or anything for that matter) from RPM's makes me shudder.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why doesn't watermarking work after server change?

Post by anthony »

You could replace the -annotate with the a -draw version. But my feeling it you may have a font drawing problem. Can you create a simple label using defaults?

Code: Select all

convert label:This_Works  image.jpg
Then try it with your font. When you verified text drawing works then you can proceed to the full annotation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
unvago

Re: Why doesn't watermarking work after server change?

Post by unvago »

I don't know how to set up your label example, and am a little unclear on what it should do. I did create a very simple script for IM testing. I tried this:

Code: Select all

<?php
$image = "backview.jpg";
$newimage  = "newbackview.jpg";
exec("/usr/bin/convert  $image   -fill white   -annotate +10+141 'Faerie Dragon'  $newimage");


?>
<img src="newbackview.jpg">
and it saves the image with a new filename "newbackview.jpg" but does not draw anything on it. I have not been able to get anything to draw using either -draw or -annotate.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Why doesn't watermarking work after server change?

Post by Bonzo »

I have a feeling we have been here before in fact I rember I had the problem when I first started.

My hosts reinstalled ImageMagick and installed GhostScript and it worked.

Out of interest I am on a shared server and have no access to the libreries etc. Is there anyway I can check the version of Ghostscript and what else is installed ?
unvago

Re: Why doesn't watermarking work after server change?

Post by unvago »

I think that ghostscript wasn't installed so I just installed it and restarted the VPS. It did not help. Does the path to ghostscript need to be configured in IM somewhere?

I tried to generate a list of installed packages, but the problem is that the list is so long that only the bottom part of the list starting with "ip.." fits in the Putty SSH client window and I have not been able to discover how to scroll upward in the list.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why doesn't watermarking work after server change?

Post by anthony »

A TTF font should not require ghostscript. However it does require a freetype library. Also are you sure you are running in the same directory as the TTF font you are attempting to use? Have you tried to see the error output from the convert command? Look at Bonzo's Rubbleweb site where he has examples of seeing the error reports from IM commands.

You can also use other commands in your PHP system calls to locate and list directories and other things on your hosting system to make sure the PHP is running in the directory and environment expected.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
unvago

Re: Why doesn't watermarking work after server change?

Post by unvago »

OK, making progress although I'm darned if I know why. I wrapped my code in Bonzo's error reporting code, and it still did not draw and did not report any errors. Then above my code, I added a piece of Bonzo's error reporting code verbatim from his web page, and strangely enough, now it drew on my image like it was supposed to (Honest! if I wake up in the morning and discover that I was dreaming this, I will let you know).

I experimented further eliminating the error reporting code and using my full watermarking code. It did not work until I replaced the original ariblk.ttf font with verdana.ttf It is curious that the ariblk.ttf font doesn't work when it worked before, but I can live with that problem.

Thanks to you both, Bonzo and Anthony for your kind assistance!
unvago

Re: Why doesn't watermarking work after server change?

Post by unvago »

P.S.
I replaced the copy of ariblk.ttf with a different one, and now everything works fine!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why doesn't watermarking work after server change?

Post by anthony »

Good. Going back to basics is one technique for problem solving.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply