Top, Left, Right, Bottom Image combination Imagemagick - php

see: http://i.stack.imgur.com/88XVW.jpg
I cut out images automatically and then want to add them in this manner, so they are basically forming the former photo but with borders in between. Can someone tell me how this works? I did use this:
convert image.jpg -frame 5 -background none \
( uploads/left-crop.jpg ) -gravity west -geometry +0+30 -composite \
( uploads/middle-crop.jpg ) -gravity center -geometry +0+30 -composite \
( uploads/top-crop.jpg ) -gravity north -geometry 0,0 -composite \
( uploads/bottom-crop.jpg ) -gravity south -geometry 0,0 -composite \
( uploads/right-crop.jpg ) -gravity east -geometry +0+30 -composite \
output.jpg
But this doesn't really get me there.
Has anyone an idea, how I can do this?

Related

ImageMagick center image group on canvas

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

imagemagick trim the bottom from transparent PNG

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.

IMagick check lightness image

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.

How to adjust watermark according to image?

I have done almost everything but i am facing one issue. I am creating multiple watermark at multiple locations. That is running fine but actually problem is when the image is having good resolution and watermark is looking very small. I want whatever the image resolution watermark should be zoom and visible here is my exec function and i am using laravel framework and i am using imagick library
$path = storage_path('app/images/TestImages/');
$mediumFileName = $path.str_random(4)."medium".str_random(4).".".$ext;
$watermarkImage = storage_path('watermark.png');
$saveWatermark = $path."image_watermark.jpg";
exec("convert $mediumFileName \( $watermarkImage -write MPR:wm \) \
-define compose:args=30,100 -compose dissolve \
-gravity NorthWest -geometry +3+3 -composite \
MPR:wm -gravity NorthEast -geometry +3+3 -composite \
MPR:wm -gravity SouthEast -geometry +3+3 -composite \
MPR:wm -gravity Center -geometry +3+3 -composite \
MPR:wm -gravity SouthWest -geometry +3+3 -composite $saveWatermark");
Here is a large watermark, with enough resolution for any picture as it is 1,000 pixels square.
Now, if we have a 1000x800 pixel image like this, we can resize the watermark to say 15% of that before compositing it (15% of 1000 is the 150 in the code):
convert image.jpg \( watermark.png -resize 150x -write MPR:wm \) \
-gravity northwest -geometry +10+10 -composite \
MPR:wm -gravity northeast -geometry +10+10 -composite \
MPR:wm -gravity southwest -geometry +10+10 -composite \
MPR:wm -gravity southeast -geometry +10+10 -composite result.png
But, if we have a smaller image like this 400x300 image:
when we apply the watermark, we first resize it to 15% of 400, or 60:
convert image.jpg \( watermark.png -resize 60x -write MPR:wm \) \
-gravity northwest -geometry +10+10 -composite \
MPR:wm -gravity northeast -geometry +10+10 -composite \
MPR:wm -gravity southwest -geometry +10+10 -composite \
MPR:wm -gravity southeast -geometry +10+10 -composite result.png
So, you need to get the size of your image how Andreas kindly showed you:
list($width, $height, $type, $attr) = getimagesize($mediumFileName);
and then multiply that by 0.15 (to get say 15%) and use that in your -resize parameter.
If the "aside processing" inside the parentheses above is upsetting or confusing, you can achieve the same result by loading up and resizing the watermark first, on its own, putting it into an MPR and then loading the main image and overlaying the MPR four times. It is just a different, maybe simpler, syntax:
convert watermark.png -resize 60x -write MPR:wm +delete image.jpg \
MPR:wm -gravity northwest -geometry +10+10 -composite \
MPR:wm -gravity northeast -geometry +10+10 -composite \
MPR:wm -gravity southwest -geometry +10+10 -composite \
MPR:wm -gravity southeast -geometry +10+10 -composite result.png
Use imagesize and get the image size.
Choose a correct sized watermark an add that to the picture.
$path = storage_path('app/images/TestImages/');
$mediumFileName = $path.str_random(4)."medium".str_random(4).".".$ext;
$watermarkImage = storage_path('watermark.png');
list($width, $height, $type, $attr) = getimagesize($mediumFileName);
if ($height * $width < some Mpx){
$watermarkImage = storage_path('watermarkSMALL.png');
} elseif($height*$width >some larger Mpx)
$watermarkImage = storage_path('watermarkLARGE.png');
}
exec("convert
$mediumFileName \(
$watermarkImage -write MPR:wm \) \
-define compose:args=30,100 -compose dissolve \
-gravity NorthWest -geometry +3+3 -composite \
MPR:wm -gravity NorthEast -geometry +3+3 -composite \
MPR:wm -gravity SouthEast -geometry +3+3 -composite \
MPR:wm -gravity Center -geometry +3+3 -composite \
MPR:wm -gravity SouthWest -geometry +3+3 -composite $saveWatermark");

Shadow text with ImageMagick

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

Categories