I have a simple php file that I use to run imageMagick convert commands from.
The issue that i have is that when I run my code, it doesn't create the image that it suppose to and I don't have any errors on my page either so I cannot figure out what the issue is!
I know I have the imageMagick installed properly because this works file:
<?php
shell_exec("convert input.jpg \
-gravity Southwest -background '#f48fb0' -splice 0x44 \
-pointsize 30 -fill white -annotate +10+4 'Some image Caption Goes here' output.jpg");
?>
But this doesn't create anything and no errors at all either:
<?php
shell_exec("width=`identify -format %w input.jpg`; \
convert -background '#0008' -fill white -gravity center -size ${width}x30 \
caption:"Faerie Dragons love hot apple pies\!" \
dragon.gif +swap -gravity south -composite anno_caption.jpg");
?>
I'm following the tutorials on here:
http://www.imagemagick.org/Usage/annotating/
Could someone please advise on this issue?
Thanks in advance.
As suggested I tried this code and it doesn't have any output and the image is not being created:
shell_exec("width=identify -format %w input.jpg \
convert -background '#0008' -fill white -gravity center -size ${width}x30 \
caption:'Faerie Dragons love hot apple pies\!' input.jpg +swap -gravity south -composite anno_caption.jpg");
shell_exec("width=`convert input.jpg -format '%w' info:` \
convert -background '#0008' -fill white -gravity center -size ${width}x30 \
caption:'Faerie Dragons love hot apple pies\!' dragon.gif +swap -gravity south -composite anno_caption.jpg");
Try this! I'm assuming that you have input.jpg and dragon.gif files.
This is how I would write the code although it "works" it is probably not creating the image you are looking for, but I do not know the result you require.
If you are using V7 you could miss out the identify part
** Code modified to change Shell_exec to exec on identify line **
<?php
// Read the width into a variable
$width= exec("identify -format %w input.jpg");
// Put the command into a variable
// Allows you to echo the command to show what you are actually running
$cmd = "-background \"#0008\" -fill white -gravity center -size {$width}x30".
" caption:\"Faerie Dragons love hot apple pies\!\" ".
" dragon_sm.gif +swap -gravity south -composite ";
echo $cmd;
// Run the command with some error reporting
// In production I would either disable the error display
// or remove the error reporting totally
$array=array();
echo "<pre>";
exec ("convert $cmd anno_caption.jpg 2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";
?>
Related
I am attempting to write an imagemagick command to trim the transparent pixels from the bottom of a transparent PNG. I found these commands and modified them to take off just the bottom. However the output is not as expected.
With some trial and error I've identified that the command appears to be generating an image of a minimum height. If the design is too high it leaves transparent pixels underneath. But nothing jumps out at me as being the element of the command that causes that?
I've included my 3 files that I trimmed and the 3 results that the following command(s)
generates:
convert \( ORIGINAL.png -bordercolor none -border 1x0 \) -size 1x1 xc:black -gravity west -composite -size 1x1 xc:black -gravity east -composite -size 1x1 xc:black -gravity north -composite -fuzz 10% -trim +repage -bordercolor white -shave 1x0 TRIMMED.png
convert \( ORIGINAL2.png -bordercolor none -border 1x0 \) -size 1x1 xc:black -gravity west -composite -size 1x1 xc:black -gravity east -composite -size 1x1 xc:black -gravity north -composite -fuzz 10% -trim +repage -bordercolor white -shave 1x0 TRIMMED2.png
convert \( ORIGINAL3.png -bordercolor none -border 1x0 \) -size 1x1 xc:black -gravity west -composite -size 1x1 xc:black -gravity east -composite -size 1x1 xc:black -gravity north -composite -fuzz 10% -trim +repage -bordercolor white -shave 1x0 TRIMMED3.png
If someone could please explain what I am missing in terms of this height issue that would be really appreciated.
These are the 3 ORIGINAL files and the 3 outcomes showing how the outcome changes with the original file changes.
This is what I am trying to acheive - delete the empty space from the bottom of the image. I want no space after the image at the moment if it is too high I still get space underneath (see the last outcome, bottom right)
---- BELOW ARE JUST THE ORIGINAL FILES IF ANYONE WANTED TO TRY IT ON THEIR SETUP ETC ----
If you're trying to remove what a "-trim" would remove, but only from the bottom edge of an image, this command should give you that result...
convert input.png -background none -set page %[#] \
-set option:distort:viewport %[w]x%[fx:page.y+page.height] \
+repage -distort SRT 0 result.png
It starts by setting some variables that contain the results of a "-trim" operation, but without actually removing anything. Then it uses those variables to calculate the after-trim dimensions for the output viewport. Then it uses a no-op "-distort" to effectively crop the image to the calculated output dimensions, removing only the excess transparent pixels toward the bottom.
I need to be able to write some text automatically inside an image. According to the image lightness, the script must write in white or black.
So how do I check the lightness/darkness of an image with Imagick?
You could do something like this:
// Load the image
$imagick = new Imagick("image.jpg");
// convert to HSL - Hue, Saturation and LIGHTNESS
$imagick->transformImageColorspace(imagick::COLORSPACE_HSL);
// Get statistics for the LIGHTNESS
$Lchannel = $imagick->getImageChannelMean(imagick::CHANNEL_BLUE);
$meanLightness = $Lchannel['mean']/65535;
printf("Mean lightness: %f",$meanLightness);
If you want to do undercoloured text, per Fred's suggestion, you can do that in PHP with:
$image = new Imagick("image.jpg");
$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFontSize(24);
$draw->setTextUnderColor('#ff000080');
$image->annotateImage($draw,30,50,0,"Undercoloured Text");
$image->writeImage('result.jpg');
You could also just create a text image on some background color and overlay that on the image. Or use -undercolor with -draw or -annotate. That way, you do not have to worry about the color of the image. Or you could specify the region where you want to write text over, then get the average lightness of that region. Then test if the region is brighter or darker than mid-gray. If brighter, then create a text image of the same size with transparent background and use black text color. Similarly if darker, use white text color. So in ImageMagick command line, these would be:
Input:
Pink Undercolor:
convert logo.png \
\( -size 110x -background pink -font ubuntu-bold -fill $textcolor label:"Testng" \) \
-gravity northwest -geometry +395+400 -compose over -composite result3.png
Testing (dark region) - Unix syntax:
test=`convert logo.png -crop 110x36+395+400 +repage -colorspace gray -format "%[fx:(mean>0.5)?1:0]" info:`
if [ $test -eq 1 ]; then
textcolor="black"
else
textcolor="white"
fi
convert logo.png \
\( -size 110x -background none -font ubuntu-bold -fill $textcolor label:"Testng" \) \
-gravity northwest -geometry +395+400 -compose over -composite result1.png
Testing (bright region):
test=`convert logo.png -crop 110x36+100+400 +repage -colorspace gray -format "%[fx:(mean>0.5)?1:0]" info:`
if [ $test -eq 1 ]; then
textcolor="black"
else
textcolor="white"
fi
convert logo.png \
\( -size 110x -background none -font ubuntu-bold -fill $textcolor label:"Testng" \) \
-gravity northwest -geometry +100+400 -compose over -composite result2.png
Sorry, I do not know Imagick. So someone else may need to help on that.
I'm trying to add an animated gif with transparency to a simple jpeg background image using imagemagick, I'm a novice when it comes to imagemagick, and command line stuff is greek to me. This is what I have mostly an amalgam of different solutions i've tried from the web.
$fname = 'tmp_'.uniqid();
//exec('convert testgif.gif -coalesce -trim \ -set dispose previous testgif.gif');
exec("convert ".$fname.".jpg \( testgif.gif \) \ -set dispose previous -gravity Center -layers Composite \-layers Optimize -loop 0 ".$fname.".gif");
I also tried the simpler format of
exec("convert bunny_grass.gif bunny_anim.gif -loop 0 bunny_on_grass.gif");
I am using this as a reference http://www.imagemagick.org/Usage/anim_basics/
The result here
Edit: I found a solution, I don't know if its the best way, but its working if anyone has an easier cleaner method, I'm all ears.
exec('convert testgif.gif -coalesce -trim \ -set dispose background \ -gravity Center -layers Composite \
-layers Optimize cleared_testgif.gif');
exec("convert ".$fname.".jpg \(cleared_testgif.gif -repage \)-loop 0 ".$fname.".gif");
I been working all day to add shadow to text using command line imagemagick. Googled many pages and experimented with gaussian, blur, shadow commands for dropping shadow but no success.
Here is my command in php:
$img_save = 'C:\Users\abc\Desktop\testimage.jpg';
$line = "Anatidaephobia is the fear that somewhere in the world there is a duck watching you. ";
$line = wordwrap($line, 25, "\\n");
exec("convert -background white -weight bold -size 500x -pointsize 35 -font arial-italic -gravity center -fill black caption:\"$line\" $img_save");
The above code gives image without difficulty. Can anyone tell how to add shadow to text in the image?
Thanks
With caption just clone and shadow it with,
convert logo: -resize 40%x40 \
\( -size "80x40" -background none -gravity west \
-fill green caption:"Caption text" \
\( +clone -background navy -shadow 80x3+5+5 \) \
+swap -background none -layers merge +repage \) -composite out.png
You just have to draw the shadow first then draw over it with the font
convert -size 500x500 xc:white -pointsize 35 -font arial-italic -gravity center - fill red -draw "text 2,2 'text'" -fill black -draw "text 0,0 'text'" outfile.jpg
I got 2 seprate Imagemagick commands (resize and crop circle). Is it possible to combine both commands into single PHP exec.
exec('convert original.jpg -resize x100 -gravity center -crop 100x100+0+0 +repage thumbnail.jpg');
exec('convert -size 100x100 xc:none -fill thumbnail.jpg -draw "circle 50,50 50,0" circle.png');
start a shell in your exec command and supply the executables as parameters to the shell, separated by ';'
e.g. bash -c "ls /tmp/; echo bla"
I can't test this right now, but have you tried simply literally combining them?
exec('convert original.jpg -resize x100 -gravity center -crop 100x100+0+0 +repage
xc:none -draw "circle 50,50 50,0" circle.png');
(line break added for clarity)
The only thing I'm unsure about is the xc:none as I don't know what that does. Other than that, it should be easy to combine these.
Maybe you can chain them with the && operand:
exec('convert original.jpg -resize x100 -gravity center -crop 100x100+0+0 +repage thumbnail.jpg && convert -size 100x100 xc:none -fill thumbnail.jpg -draw "circle 50,50 50,0" circle.png');
Even though that's two calls to convert, it's a single PHP exec call.
Regards