ImageMagick annotate with PHP variable - imagemagick

I currently run the following command to annotate an image with the variable $handle
When running on a LAMP, everything worked fine and the $handle variable was put into the image. However running on a WAMP, the two apostrophes around the variable are being included into the variable. Which adds a ' to the front and end of the variable. Here is what I'm running. Any ideas why?
exec("C:\ImageMagick\convert $final -font handle_font.ttf -pointsize $font_size -fill
black -annotate $pos_black '$handle' -fill white -annotate $pos_white '$handle'
-flatten $final");

On windows you need to swap the ' for " and in your case escape it with a \
\"$handle\"
Write your code like this and you can then echo out the $cmd and see what it contains:
$cmd = " $final -font handle_font.ttf -pointsize $font_size -fill black -annotate $pos_black \"$handle\" -fill white -annotate $pos_white \"$handle\" -flatten ";
echo $cmd.'<br>';
exec("C:\ImageMagick\convert $cmd $final");

Related

ImageMagick script fails with MISLEADING filename or extension too long (-sparse-color)

Problem
In my quite short script I have the problem that it sometimes reports that the filename or extension is too long. Depending on the $image and $size values in my script this error may occur or not.
E.g. the script below produces this error with the image from here - saved and converted to "example3.png".
I do use Version: ImageMagick 7.0.10-62 Q16 x64 on windows and I don't know what to do with the error message... Any ideas what the problem is here?
Powershell script
#####################
# Setup
#####################
$image = "./example3.png"
$out = "./result.png"
$outPalette = "./palette.png"
$size = 50
$fuzz = 50
$colors = 6
$resizedSize = "$($size)x$($size)`!"
$histogramSize = "$($size)x$($size)"
#####################
# Program
#####################
Write-Host ""
# 1) Scale + change depth + remove unwanted colors (b/w)
Write-Host "- Step 1..." -ForegroundColor Green
magick convert $image -scale $resizedSize -depth 8 `
-fuzz $fuzz -transparent black -transparent white `
$out
#2) create histogram with the help of the sparse colors
Write-Host "- Step 2..." -ForegroundColor Green
$dataHistogram = magick convert -size $histogramSize xc: -sparse-color voronoi ( magick convert $out sparse-color: ) +dither -colors $colors -depth 8 -format %c histogram:info:
# ... more ...
Edit: Adjustments
replaced magick convert with magick
replaced $fuzz = 50 with $fuzz = "50%"
replaced $size = 50 with $size = 100
More images do work now but e.g. following still fails with the same error:
Edit2:
The result of the inner magick command (magick convert $out sparse-color:) looks like following:
# ImageMagick pixel enumeration: 100,100,255,srgba
0,0: (87,72,86,0) #57485600 srgba(87,72,86,0)
1,0: (105,81,91,0) #69515B00 srgba(105,81,91,0)
...
I am not sure what's going on with powershell but if the issue is the length of the command-line, you can supply the sparse colour from a file like this:
magick -size 800x600 xc: -sparse-color voronoi #colors.txt result.png
Or on stdin like this:
echo "10,10 red 200,200 yellow" | magick -size 800x600 xc: -sparse-color voronoi #- result.png

How to use pango in script with imagemagick

Here is a simplified version of the code I'm currently using :
cat $FILES | while read line; do
convert -fill $FG_COLOR -background $BG_COLOR \
-size ${line_width}x${line_height} -page +${x_margin}+${y} \
label:"${varL} and ${varR}" miff:-
done | convert -size ${SCREEN_W}x${SCREEN_H} xc:$BG_COLOR - -flatten image.jpg
And it is working well !
But I wish to have different colors for ${varL} and ${varR}, and I guess I should use pango instead of label. Am I correct about that ?
But by keeping the same code and just replacing label by pango I have an unexpected error :
convert-im6.q16: width or height exceeds limit
I am very confused about how I was able to solve this.
Anyway here is the thing : I don't know why but pango needs to be BEFORE the other arguments.
convert -fill $FG_COLOR -background $BG_COLOR \
-size ${line_width}x${line_height} -page +${x_margin}+${y} \
pango:"${varL} and ${varR}" miff:-
Isn't working, but :
convert pango:"${varL} and ${varR}" \
-fill $FG_COLOR -background $BG_COLOR \
-size ${line_width}x${line_height} -page +${x_margin}+${y} miff:-
Is working !
EDIT: pango: need to be before -page

Converting special characters with ImageMagick

I am using a PowerShell script to batch convert Unicode characters to PNG files. See http://pastebin.com/aGJzk4Hh.
I was able to figure out that to convert the " character one must use the designation label:\".
The other special characters are not so simple.
C:\ImageMagick\convert.exe -background transparent -fill hsb(0,0,0) \
-font Arial -pointsize 18 -size 18x26 -gravity center label:"#" \
C:\Users\erics_000\Desktop\Output\Chars\40.png
convert.exe: UnableToAccessPath # error/property.c/InterpretImageProperties/3330.
convert.exe: NoImagesDefined `C:\Users\erics_000\Desktop\Output\Chars\40.png' # error/convert.c/ConvertImageCommand/3230.
as well as:
C:\ImageMagick\convert.exe -background transparent -fill hsb(0,0,0) \
-font Arial -pointsize 18 -size 18x26 -gravity center label:"\" \
C:\Users\erics_000\Desktop\Output\Chars\5C.png
convert.exe: NoImagesDefined `label:" C:\Users\erics_000\Desktop\Output\Chars\5C.png' # error/convert.c/ConvertImageCommand/3230.
have been very problematic for me.
I have tried every way I know how to escape those characters by way of \ however nothing yet has proven to work. I will need to be able to convert all Unicode characters, \ and # included.
Is it known how these can be converted to PNG files with ImageMagick?
1st question: Did you check that your version of Arial does include glyphs for the Unicode characters in question?
Ok, if it is also about the \ and # characters, then that shouldn't pose a problem. This leads me to the...
2nd question: Which version of ImageMagick do you have installed? Can you report the result of convert -version?
Here is my result with ImageMagick v6.9.0-0 on a Mac OS X Mavericks 10.9 system:
convert -background black -fill red -pointsize 96 label:' # \\ # \\ # ' sample.png
and (note the blanks starting and ending my string!):
convert -background transparent -fill hsb\(0,0,0\) -font Arial \
-pointsize 180 -size 190x210 -gravity center label:' # ' \
-frame 1 \
sample2.png
If you need to emit real Unicode glyphs from an input consisting of Unicode character codes, you can do this with the help of Perl. I guess that Powershell has also a method for this, but I'm not familiar with it):
perl -e 'binmode(STDOUT, ":utf8"); \
print " Smiley: \x{263A} "' \
| convert -background black \
-fill red \
-pointsize 98 \
-font Menlo \
label:#- \
smiley.png
(Note: the #- syntax just tells convert to read input string for the label from standard input...)

Capistrano: deploy files other than used in development

I have a rails application that uses some bash scripts that have different values (paths) in them for development and production. I want to use capistrano to deploy the app using the values suitable for production.
So for example in #{Rails.root}+'script.sh':
#!/bin/bash
/usr/local/bin/convert -colorspace sRGB -background transparent -font Myriad-Pro-Condensed -fill grey0 -stroke yellow -strokewidth 2 -size 640x480 -gravity $3 label:"$2" $1.png
For production I will need:
#!/bin/bash
/usr/bin/convert -colorspace sRGB -background transparent -font Myriad-Pro-Condensed -fill grey0 -stroke yellow -strokewidth 2 -size 640x480 -gravity $3 label:"$2" $1.png
Is there a solution which involves changing files on the fly, while deploying (with some gsub command, that changes values)?
An ERB template is probably the most straightforward. Inside the template you can access all the Capistrano variables so you can tell what stage you are in, etc. The cap code would look something like this:
template = File.read(File.join(File.dirname(__FILE__), "templates/script.sh.erb"))
result = ERB.new(template).result(binding)
put result, "#{release_path}/script.sh"

PHP use of IMAGEMAGICK or imagick or magicwand

I use Imagemagick (www.imagemagick.org)
Since i am on hostgator i also have imagick and magickwand installed.
I can do simple manipulation with imagick and magickwand but if i want to reproduce the advanced tutorials at imagemagick.org i fail.
Goal:
make this working http://www.imagemagick.org/Usage/advanced/#jigsaw
Questions:
How can i do this with imagick or magickwand?
Could i somehow also communicate with the module imagemagick through command line, like exec(....)?
Thanks 4 short help
You can use exec() or shell_exec().
For example:
exec('/path/to/your/imagick/convert jigsaw_tmpl.png -edge .5 -blur 0x.5 jigsaw_edge.png');
One more reminder, exec() will not work in PHP safe mode.
Okay. The solution using the command-line commands straight is like the following:
exec("/usr/lib/convert user/set/seinfeld/image/image/data/apple_cinema_30.jpg -edge .5 -blur 0x.5 jaw_ege.png");
so obviously you have to know the path to the module and set the path to the images correctly (a).
will update if necessary with the instructions for imagick/magickwand.
I think it is to complicated to be run in Imagick - not saying it could not be - and as said above the best bet would be Imagemagick command line and exec( ). Build it up one command at a time; you may be able to combine commands later. Do not use jpg for any intermediate images as you will start to loose quality.
It depends what effect you are after as Anthony has a Bash script you could use linked towards the bottom of that section of the page.
You can run that with php using exec:
Upload the script to your server
CHMOD it to either 755 or 777 depending on your server setup
// Run the script
exec("/FULL PATH TO JIGSAW/jigsaw options input.jpg mask.png output.png 2>&1", $array);
//Display any errors
echo "<br>".print_r($array)."<br></pre>";
I do not know if this will work on a Hostgator account but I can not see why not.
Also I have not tried it and you need a mask image to go with your input image.
I have just tried this on my server and get an error: /bin/bash^M: bad interpreter: No such file or directory
This means nothing to me!
Making one jigsaw piece using Anthony's images and code with Imagemagick commaand line and exec( )
exec("convert jigsaw_tmpl.png -edge .5 -blur 0x.5 jigsaw_edge.png");
$cmd = " holocaust_md.jpg \( jigsaw_edge.png -negate \) -geometry +365+96 ".
" -compose multiply -composite -crop 100x100+365+96 +repage ";
exec("convert $cmd jigsaw_outline.png");
$cmd = " holocaust_md.jpg -crop 100x100+365+96! -background none -flatten ".
" +repage \( jigsaw_tmpl.png +matte \) -compose CopyOpacity -composite ".
" -rotate -20 -gravity center -crop 100x100+0+0 +repage ";
exec("convert $cmd jigsaw_cutout.png");
$cmd = " jigsaw_cutout.png \( +clone -channel A -separate +channel -negate ".
" -background black -virtual-pixel background -blur 0x2 -shade 120x21.78 ".
" -contrast-stretch 0% +sigmoidal-contrast 7x50% -fill grey50 -colorize 10% ".
" +clone +swap -compose overlay -composite \) -compose In -composite ";
exec("convert $cmd jigsaw_bevel.png");
$cmd = " jigsaw_bevel.png \( +clone -fill DarkSlateGrey -colorize 100% -repage +0+1 \) ".
" \( +clone -repage +1+2 \) \( +clone -repage +1+3 \) \( +clone -repage +2+4 \) ".
" \( +clone -repage +2+5 \) -background none -compose DstOver -flatten";
exec("convert $cmd jigsaw_thickness.png");
$cmd = " jigsaw_thickness.png \( +clone -background Black -shadow 50x3+4+4 \) ".
" -background none -compose DstOver -flatten";
exec("convert $cmd jigsaw_shadow.png");

Resources