Watermark all tif images

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?".
cappsie

Re: Watermark all tif images

Post by cappsie »

fmw42 wrote:In command line IM, you can use -write to output multiple images.

convert input <some processing> -write result.jpg result.tiff

or

convert input <some processing> -write result.tif result.jpg
I see where you're going; however, I'm struggling to correctly reference that using PHP:

Code: Select all

$cmd = "$original_image -font C:/Windows/Fonts/ARIAL.TTF -fill rgba(0,0,0,0.5) -pointsize 100 -gravity Center -annotate 40x40+0+0 \"$text_submitted\" -write $original_image " ;
My thinking was to declare the $alternative_image thus:

Code: Select all

// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name'];
$alternative_image =  $_FILES['filename']['tmp_name'];
$filePath = realpath($_FILES["filename"]["tmp_name"]);
Then:

Code: Select all

$cmd = "$original_image -font C:/Windows/Fonts/ARIAL.TTF -fill rgba(0,0,0,0.5) -pointsize 100 -gravity Center -annotate 40x40+0+0 \"$text_submitted\" -write $original_image $alternative_image " ;
Then display the alternative jpg:

Code: Select all

echo "<img src=\"$alternative_image\"><br>";
echo "Graded image saved as: <a href=\"$new_image\">$new_image</a>";
Unfortunately it didn't work.

Not entirely sure how to do it.

Thoughts?

A.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Watermark all tif images

Post by Bonzo »

Here is a simple php example of -write:

Code: Select all

<?php  
$cmd = " input.jpg \( -clone 0 -thumbnail x480 -write 480_wide.jpg +delete \)". 
" \( -clone 0 -thumbnail x250 -write 250_wide.jpg +delete \) ". 
" \( -clone 0 -thumbnail x100 -write 100_wide.jpg +delete \) -thumbnail 64x64! null: "; 
exec("convert $cmd 64_square.jpg "); 
?> 
cappsie

Re: Watermark all tif images

Post by cappsie »

Bonzo wrote:Here is a simple php example of -write:

Code: Select all

<?php  
$cmd = " input.jpg \( -clone 0 -thumbnail x480 -write 480_wide.jpg +delete \)". 
" \( -clone 0 -thumbnail x250 -write 250_wide.jpg +delete \) ". 
" \( -clone 0 -thumbnail x100 -write 100_wide.jpg +delete \) -thumbnail 64x64! null: "; 
exec("convert $cmd 64_square.jpg "); 
?> 
Another question: were I to write the file out to a folder with spaces, how could this be done?

Thanks,
A.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Watermark all tif images

Post by Bonzo »

I would try enclosing the path with " " but it will need escaping: \"folder with name/250_wide.jpg\"
cappsie

Re: Watermark all tif images

Post by cappsie »

Bonzo wrote:I would try enclosing the path with " " but it will need escaping: \"folder with name/250_wide.jpg\"
Interesting. Ok so I declared the path:

Code: Select all

$output = "C:/Test/"; 
Then added in the path and image name:

Code: Select all

$cmd = "$original_image -font C:/Windows/Fonts/ARIAL.TTF -fill rgba(0,0,0,0.5) -pointsize 50 -gravity NorthEast -annotate 0x0+0+0 \"$text_submitted\" -write $output\$original_image -write $text_submitted.jpg  ";
Oddly the file is correctly written out to C:\Test *but* it doesn't correctly use the $original_image name, rather the file name is created as "$original_image".

Any thoughts? Obviously I'm not coding $original_image quite right if IM is writing the literal string.

A.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Watermark all tif images

Post by Bonzo »

You can use an echo $cmd to see what the variable contains; that is why I like this programming method.

Sometimes I find it easier/better to do certain things outside the Imagemagick command and would try:

Code: Select all

$image1 = $output.'\'.$original_image;
$cmd = "$original_image -font C:/Windows/Fonts/ARIAL.TTF -fill rgba(0,0,0,0.5) -pointsize 50 -gravity NorthEast -annotate 0x0+0+0 \"$text_submitted\" -write $image1 -write $text_submitted.jpg  ";
The code may be trying to divide $output by $original_image although I though this would give an error. Perhaps within the imagemagick code the \ is causing the $original_image to overwite the $output
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark all tif images

Post by snibgo »

-write $output\$original_image
In PHP, backslash \ is the escape character. It means "don't interpret the next character". The next character is dollar $. So it doesn't interpret the dollar, so doesn't dereference $original_image, but sends "$original_image" to IM.
snibgo's IM pages: im.snibgo.com
cappsie

Re: Watermark all tif images

Post by cappsie »

What worked for me was this:

Code: Select all

-write $output\\$new_image
Now I just need to map a network drive and save to the UNC location. So far I have:

Code: Select all

// Define the parameters for the shell command
$location = "\\192.168.1.4\folder1\folder_test";
$user = "USERNAME";
$pass = "PASSWORD";
$letter = "Z";

// Map the drive
system("net use ".$letter.": \"".$location."\" ".$pass." /user:".$user." /persistent:no>nul 2>&1");
However, I'm not particularly 'au fait' with PHP so I'm unsure whether simply mapping the drive is sufficient or there is a need for me to tell PHP to actually access or open the directory before attempting to write to it, and in that case, would I also need to close the directory as well?

Any thoughts :D

Link for reference: http://forums.phpfreaks.com/topic/19462 ... using-php/

Thanks,
Adam.
cappsie

Re: Watermark all tif images

Post by cappsie »

Howdy,

I've managed to sort it out.

Here's what worked for me: I'm running Apache 2.2 Zend on Windows Server 2003. So I needed to add the following lines to my httpd.conf file:

Code: Select all

<Directory "//server/share">
  Options  +Indexes
  Order allow,deny
  Allow from all
</Directory>

Alias /folderalias "//server/share"
I also had to use a domain authenticated account to start Apache with. I had to use the "logon as" option in the Windows services to have Apache log on as that account. Once Apache had access, it was just a case of accessing the alias directory to have Apache list the files in the remote server share.

On to my next challenge then: getting ImageMagick to save files to that share :)

A.
cappsie

Re: Watermark all tif images

Post by cappsie »

Hmm this is interesting. It seems that if Apache is running as a domain user, this causes an error when processing the image:

Code: Select all

Array ( [0] => IMconvert.exe: unable to open image `C:\Documents': No such file or directory @ error/blob.c/OpenBlob/2658. [1] => IMconvert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. [2] => IMconvert.exe: unable to open image `and': 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 `Settings\capps.test\Local': 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 open image `Settings\Temp\php584.tmp': No such file or directory @ error/blob.c/OpenBlob/2658. [7] => IMconvert.exe: unable to open module file `C:\ImageMagick\modules\coders\IM_MOD_RL_TMP_.dll': No such file or directory @ warning/module.c/GetMagickModulePath/672. [8] => IMconvert.exe: no decode delegate for this image format `TMP' @ error/constitute.c/ReadImage/501. [9] => IMconvert.exe: no images defined `Homer.tif' @ error/convert.c/ConvertImageCommand/3187. ) 
Any thoughts on this one :-/

A.
cappsie

Re: Watermark all tif images

Post by cappsie »

Ok looks like I was going about this all wrong. So, it turns out that Apache and PHP use a temporary directory - at least according to this page: http://stackoverflow.com/questions/3945 ... estmp-name - so to force Apache to use the directory I want, I added a line to my php.ini as per the below advice:
upload_tmp_dir string
The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default.

If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.

Code: Select all

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = C:\TempUploads
Ok so that's me all sorted.

Thank you all for your help. :D

Adam.
Post Reply