I have this code. It should crate a transparent PNG, but its output is black backgrounded PNG instead. On CentOS.
<?php
header( 'Content-Type: image/png' );
passthru("convert -background transparent -fill red -pointsize 72 -font TR.Matisse.ITC.TTF -gravity Center label:'Font Test' png:-");
?>
this works flawlessly on ubuntu terminal:
convert -background transparent -fill red -pointsize 72 -font TR.Matisse.ITC.TTF -gravity Center label:'Font Test' png.png
So how do I get it to be transparent (and not have a black matte) by calling the system command from PHP?
Isn't the proper transparent command for ImageMagick this:
convert -transparent <SomeColor> -fill red -pointsize 72 -font TR.Matisse.ITC.TTF -gravity Center label:'Font Test' png.png
Or else use an Alpha channel:
convert -alpha transparent -fill red -pointsize 72 -font TR.Matisse.ITC.TTF -gravity Center label:'Font Test' png.png
Try using exec() and saving the file to disk then see if it is the correct image. You may find the issue is with php and the passthru.
Related
I am outputting a banner with an image to the left and some text to the right. This is working well, however the positioning is very manual. I'd like to be able to group the $robot_image and the $robot_name text into one image, and then center that image group on the background 1400x500 canvas. Any ideas how I'd achieve this? The attached image is what I'd like to achieve: The robot + text centered as a group on the canvas
$banner = exec("convert {$robot_image} -resize 500x500 -gravity west -geometry +100+100 -background '#{$hex}' -extent 1400x500 -font 'SignPainter.ttc' -pointsize 300 -fill 'white' -strokewidth 20 -fill white \
-stroke 'rgba(0,0,0,0.3)' -annotate +550+40 '{$robot_name}' \
-stroke none -annotate +550+40 '{$robot_name}' output.jpg");
For anyone interested, this is how I achieved it:
convert robot.png -resize 500x500 -background "#31BEFF" -size 600x -font 'SignPainter.ttf' -gravity center label:"RobotName" -alpha off +smush -50 -gravity center -background "#31BEFF" -extent 1400x500 result.jpg
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 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 have a 424x318 image that I "draw" a circle into and leave the rest transparent. I want to then take that circle and crop it out. How can I do this?
My function (the bash variables are just the normal stuff, $SCALEFILE is the file, $NEWFILE is what it saves it as and $SIZE is just the normal size string x0,y0 x1,y1)
convert -size 416x318 xc:none -fill $SCALEFILE -draw "circle $SIZE" $NEWFILE
ps. my circle dimensions change.
Thanks!
You can use -crop WxH+X+Y +repage to crop to the circle.
For example:
convert -size 300x300 xc:transparent -fill "image.png" -draw "circle 240,90 290,90" -crop 100x100+190+40 +repage circle1.png
convert -size 300x300 xc:transparent -fill "image.png" -draw "circle 70,90 110,90" -crop 100x100+20+40 +repage circle2.png
This command add the text "flower" to the image:
convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 'Flower' flower_annotate1.jpg
I'm using ImageMagick 2.2.0. I'm running it from PHP using:
system('convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 'Flower' flower_annotate1.jpg');
but I'm not getting the result
Escape the single-quotes in the parameters:
system('convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 \'Flower\' flower_annotate1.jpg');
Or just wrap the thing in double-quotes so you don't have to escape them.