Page 3 of 4
Re: Watermark all tif images
Posted: 2014-06-09T08:54:35-07:00
by Bonzo
The example you have linked to uses GD not Imagemagick and the image is not saved but just displayed in the browser. This can also be done with Imagemagick if you want.
The modified image should be in the path set with $new_image, try modifying your code to this:
Code: Select all
// Uncomment this line to view the image path
// echo "The new image is saved as $new_image<br>";
// Uncomment this line to view the image
// echo "<img src=\"$new_image\"><br>";
echo "Graded image saved as: <a href=\"$new_image\">$new_image</a>";
Re: Watermark all tif images
Posted: 2014-06-10T07:51:16-07:00
by cappsie
Bonzo wrote:The example you have linked to uses GD not Imagemagick and the image is not saved but just displayed in the browser. This can also be done with Imagemagick if you want.
The modified image should be in the path set with $new_image, try modifying your code to this:
Code: Select all
// Uncomment this line to view the image path
// echo "The new image is saved as $new_image<br>";
// Uncomment this line to view the image
// echo "<img src=\"$new_image\"><br>";
echo "Graded image saved as: <a href=\"$new_image\">$new_image</a>";
Many thanks for that. Might you also know how to save the image back to the original location? So if it was added from C:\temp it must be saved there - at the moment, the image is saved into the htdocs folder from where the php script resides.
Thoughts?
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-10T08:42:10-07:00
by Bonzo
I do not think you can save it in C as it is outside the server root; although I have never tried it.
How would the code know where the file came from anyway as the original location would not be recorded anywhere unless manually input into the form when the file was uploaded.
Re: Watermark all tif images
Posted: 2014-06-12T05:17:14-07:00
by cappsie
Hi again,
I seem to have messed something up as I'm receiving this error message:
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\adam.capps\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\php430.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: unable to open image `and': No such file or directory @ error/blob.c/OpenBlob/2658. [10] => IMconvert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. [11] => IMconvert.exe: unable to open image `Settings\adam.capps\Local': No such file or directory @ error/blob.c/OpenBlob/2658. [12] => IMconvert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. [13] => IMconvert.exe: unable to open image `Settings\Temp\php430.tmp': No such file or directory @ error/blob.c/OpenBlob/2658. [14] => 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. [15] => IMconvert.exe: no decode delegate for this image format `TMP' @ error/constitute.c/ReadImage/501. [16] => IMconvert.exe: no images defined `Homer.tif' @ error/convert.c/ConvertImageCommand/3187. )
This is the code I'm using:
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'];
$filePath = realpath($_FILES["filename"]["tmp_name"]);
// Additional variables
// $docroot = $_SERVER['DOCUMENT_ROOT'];
// $scriptroot = $_SERVER['SCRIPT_FILENAME'];
// $location = "\\\\sohced004\\cedscans$\\ENT";
// 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 -font C:/Windows/Fonts/ARIAL.TTF -fill rgba(0,0,0,0.5) -pointsize 50 -gravity Center -annotate 40x40+0+0 \"$text_submitted\" -write $original_image " ;
// Comment 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
// Comment out the if function after debugging
exec("C:/ImageMagick/IMconvert $cmd $new_image 2>&1", $array);
// Uncomment this line to view the image path
// echo "The new image is saved as $new_image<br>";
// Uncomment this line to view the image
echo "<img src=\"$new_image\"><br>";
echo "Graded image saved as: <a href=\"$new_image\">$new_image</a>";
echo "<BR />";
echo "<BR />";
echo "Graded text: <B>$text_submitted</B>";
echo "<BR />";
echo "<BR />";
if ( !empty($array) ){
echo "<br />There were errors grading the image<br />
<pre><br />".print_r($array)."<br>";
echo "</pre>";
}
}
else { ?>
<html>
<head>
<!-- <script type="text/javascript" src="calendarDateInput.js"></script> -->
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<label>Upload file for grading</label>
<input type="file" name="filename" /><BR /><BR />
<input type="text" name="text_submitted" /><br /><BR /><BR />
<!-- <script>DateInput('orderdate', true, 'DD-MON-YYYY')</script><BR /> -->
<!-- <input type="button" onClick="alert(this.form.orderdate.value)" value="Show date value passed"> -->
<input type="Submit" name="Submit" value="Grade File" />
</form>
</body>
</html>
<?php } ?>
The intriguing part is the line:
unable to open image `C:\Documents' IMconvert seems to be trying to save the image to a temporary location or perhaps the C:\Documents and Settings\Username\etc...
I can't imagine where it is getting that from unless it is from the original file's localtion, which it shouldn't be as I've tried variations such as C:\Test.
Any thoughts
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-12T05:28:10-07:00
by snibgo
Your filename contains spaces. At the command line you would put it in double-quotes. Do something similar in php.
Re: Watermark all tif images
Posted: 2014-06-12T05:51:17-07:00
by cappsie
snibgo wrote:Your filename contains spaces. At the command line you would put it in double-quotes. Do something similar in php.
I've scanned the code and I just cannot see it. I think I've been looking at it too long now - can't see the wood for the trees type scenario...!
Would you mind pointing it out for me please
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-12T06:12:42-07:00
by snibgo
exec("C:/ImageMagick/IMconvert $cmd $new_image 2>&1", $array);
Exactly what characters are in the first argument? I suspect it starts off something like:
Code: Select all
C:/ImageMagick/IMconvert C:\Documents and Settings\adam.capps\Local Settings\Temp\php430.tmp ...
I know this because the error message says it tries to read a file called "C:\Documents", then a file called "and", etc.
Re: Watermark all tif images
Posted: 2014-06-12T06:54:18-07:00
by cappsie
snibgo wrote:exec("C:/ImageMagick/IMconvert $cmd $new_image 2>&1", $array);
Exactly what characters are in the first argument? I suspect it starts off something like:
Code: Select all
C:/ImageMagick/IMconvert C:\Documents and Settings\adam.capps\Local Settings\Temp\php430.tmp ...
I know this because the error message says it tries to read a file called "C:\Documents", then a file called "and", etc.
I understand what you mean, but to format fully the command, it would read:
C:/ImageMagick/IMconvert image.tif -font C:/Windows/Fonts/ARIAL.TTF -fill rgba(0,0,0,0.5) -pointsize 50 -gravity Center -annotate 40x40+0+0 "Watermarked Text" -write image.tif 2>&1 (last part is snipped)
Or thereabouts.
I think it is pulling in a variable from somewhere else. Even though the resulting error message shows "C:\Documents" there is no where in the php that specifies this. Is this not more likely a Windows path issue? Where could it be gleaning that "C:\Documents" from?
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-12T07:11:08-07:00
by cappsie
I have figured out the issue. Apache was set to run as my own user account as I was attempting to get the php to write the file to a remove UNC share - that would be absolutely ideal! However, setting it back to the system account correctly restores the path, i.e.
Code: Select all
C:\WINDOWS\Temp\php437.tmp -pointsize 50 -font arial.ttf -fill rgba\(0,0,0,0.4\) -gravity center -annotate +0+0 "HOMER" Array ( [0] => Invalid Parameter - -pointsize )
There were some errors during the Imagemagick conversion:
So even though this errors, my existing script works!
ARGH.
Anyway, if you happen to know of a way to write the image to a remote UNC share, I would be eternally grateful. I have spent a wealth of time googling and trying different php scripts - hence why I ended up changing the Apache logon account details - but to no avail.
I'm stuck
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-12T07:40:33-07:00
by snibgo
Invalid Parameter - -pointsize
You are going backwards. This is an error from Windows "convert". Good thing you have an error: if it works, it will reconfigure your filesystem. You might lose all the files on your disk. I'll repeat that: YOU MIGHT LOSE ALL THE FILES ON YOUR DISK.
Re: Watermark all tif images
Posted: 2014-06-12T08:15:15-07:00
by cappsie
In my code I explicitly referenced the convert.exe - and since I made the Apache login credentials change, it all works ok
So now it's just figuring out how to save the .tif back to the location it was uploaded from.
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-12T14:54:44-07:00
by cappsie
Bonzo wrote:The example you have linked to uses GD not Imagemagick and the image is not saved but just displayed in the browser. This can also be done with Imagemagick if you want.
The modified image should be in the path set with $new_image, try modifying your code to this:
Code: Select all
// Uncomment this line to view the image path
// echo "The new image is saved as $new_image<br>";
// Uncomment this line to view the image
// echo "<img src=\"$new_image\"><br>";
echo "Graded image saved as: <a href=\"$new_image\">$new_image</a>";
This works for .jpg but not .tif. Is this a browser "feature"?
Thanks,
Adam.
Re: Watermark all tif images
Posted: 2014-06-12T15:14:35-07:00
by fmw42
You may have to convert them to jpg or png for display in a browser. I am not sure all (or any) browsers support direct display of (layered) tiff images.
Re: Watermark all tif images
Posted: 2014-06-13T00:20:57-07:00
by cappsie
fmw42 wrote:You may have to convert them to jpg or png for display in a browser. I am not sure all (or any) browsers support direct display of (layered) tiff images.
Hmm I guess I will need to watermark the tif and write that out to a file, while simultaneously converting it to a jpg or png to be displayed in the browser. Somewhat of a challenge.
A.
Re: Watermark all tif images
Posted: 2014-06-13T09:50:59-07:00
by fmw42
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