Rounded corner border on rounded corner image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Rounded corner border on rounded corner image

Post by Bonzo »

I am trying to add a red border around an image similar to Anthonys example all in one line:
Image

The two parts work OK but I can not add them together:

Code: Select all

$image = "small.jpg";
$size = getimagesize( $image );
$width = $size[0]+10;
$height = $size[1]+10;
// Creates the red filled rounded rectangle
$cmd = " -size {$width}x{$height} xc:none -fill red -draw \"roundRectangle 5,5 {$width},{$height} 20,20\" -compose dstout -matte ";
// Creates the image with the rounded rectangle
$cmd = " -size {$size[0]}x{$size[1]} xc:none -fill white -draw \"roundRectangle 0,0 {$size[0]},{$size[1]} 15,15\" $image -compose SrcIn -gravity center -composite ";
// All on one line
$cmd = " -size {$width}x{$height} xc:none -fill red -draw \"roundRectangle 5,5 {$width},{$height} 20,20\" -compose dstout -matte miff:- ".
 " ( -size {$size[0]}x{$size[1]} xc:none -fill white -draw \"roundRectangle 0,0 {$size[0]},{$size[1]} 15,15\" $image -compose SrcIn ) -gravity center -composite ";
echo "<pre>";
exec("convert $cmd rounded_corners4.png 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";
Image from this code: Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rounded corner border on rounded corner image

Post by snibgo »

A debug printing of $cmd might help.

The last "-composite" will have the previous "-compose" setting, which is "SrcIn", which might not be what you want. But the previous bracket has a "-compose" with no "-composite", which is suspicious.

$cmd seems to write to stdout and read from stdin. This is fine, but it looks as if you want the stdin to be read from the stdout, which won't work in a single command.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Rounded corner border on rounded corner image

Post by Bonzo »

This is what cmd =

Code: Select all

-size 646x340 xc:none -fill red -draw "roundRectangle 5,5 646,340 20,20" -compose dstout -matte miff:- ( -size 636x330 xc:none -fill white -draw "roundRectangle 0,0 636,330 15,15" small.jpg -compose SrcIn ) -gravity center -composite 
Just noticed why I had a problem with and without the extra - as the | was missing. But still not working.
cmd =

Code: Select all

-size 646x340 xc:none -fill red -draw "roundRectangle 5,5 646,340 20,20" -compose dstout -matte miff:- | ( -size 636x330 xc:none -fill white -draw "roundRectangle 0,0 636,330 15,15" small.jpg -compose SrcIn ) -gravity center -composite over - 
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rounded corner border on rounded corner image

Post by snibgo »

For "-composite over", you probably meant "-compose over -composite".

Here's a complete Windows script.

Code: Select all

rem Add a rounded corner by two different methods.

set SRC=logo:

set InRad=15
set OutRad=20
set BORDCOL=red


FOR /F "usebackq" %%L IN (`identify -format "WW=%%w\nHH=%%h" %SRC%`) DO set %%L

set /A wwOutRad=%WW%+OutRad
set /A hhOutRad=%HH%+OutRad


rem Method 1: three convert commands.

rem Create the red filled rounded rectangle
convert ^
  -size %wwOutRad%x%hhOutRad% ^
  xc:none ^
  -fill %BORDCOL% -draw "roundRectangle 0,0 %wwOutRad%,%hhOutRad% %OutRad%,%OutRad%" ^
  x.png

rem Create the image with the rounded rectangle
convert ^
  -size %WW%x%HH% ^
  xc:none ^
  -fill white -draw "roundRectangle 0,0 %WW%,%HH% %InRad%,%InRad%" ^
  %SRC% ^
  -compose SrcIn -gravity center -composite ^
  y.png

rem Combine the two
convert ^
  x.png ^
  y.png ^
  -gravity center -compose Over -composite ^
  border1.png


rem Method 2: Do it in one convert

convert ^
  ( ^
    -size %wwOutRad%x%hhOutRad% ^
    xc:none ^
    -fill %BORDCOL% -draw "roundRectangle 0,0 %wwOutRad%,%hhOutRad% %OutRad%,%OutRad%" ^
  ) ^
  ( ^
    -size %WW%x%HH% ^
    xc:none ^
    -fill white -draw "roundRectangle 0,0 %WW%,%HH% %InRad%,%InRad%" ^
    %SRC% ^
    -compose SrcIn -gravity center -composite ^
  ) ^
  -gravity center -compose Over -composite ^
  border2.png
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Rounded corner border on rounded corner image

Post by Bonzo »

Strange my reply dissapered !

Thanks for your example snibgo I will take a look at it later.

I have found I can get the same incorrect :? result without the miff:-

Code: Select all

$cmd = " -size {$width}x{$height} xc:none -fill red -draw \"roundRectangle 5,5 {$width},{$height} 20,20\" -compose dstout -matte ".
 " ( -size {$size[0]}x{$size[1]} xc:none -fill white -draw \"roundRectangle 0,0 {$size[0]},{$size[1]} 15,15\" $image -compose SrcIn -matte ) -gravity center -compose over -composite ";
$cmd is now:

Code: Select all

 -size 646x340 xc:none -fill red -draw "roundRectangle 5,5 646,340 20,20" -compose dstout -matte ( -size 636x330 xc:none -fill white -draw "roundRectangle 0,0 636,330 15,15" small.jpg -compose SrcIn -matte ) -gravity center -compose over -composite 
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rounded corner border on rounded corner image

Post by snibgo »

So, your command is:

Code: Select all

convert
  -size 646x340 
  xc:none -fill red -draw "roundRectangle 5,5 646,340 20,20" 
  -compose dstout -matte 
  ( -size 636x330 xc:none -fill white -draw "roundRectangle 0,0 636,330 15,15"
    small.jpg 
    -compose SrcIn -matte 
  )
  -gravity center -compose over -composite 
  rounded_corners4.png
The first "-compose dstout -matte" is redundant. (It sets the compose method, but this setting is overwritten before it is used. "-matte" is implied by "xc:none".)

Likewise, -compose SrcIn -matte". But you need "-compose SrcIn -composite" here, to combine the rounded rectangle with small.jpg.

The only other difference to my version is the first roundRectangle. Your origin is "5,5" where mine is "0,0".
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Rounded corner border on rounded corner image

Post by Bonzo »

Well done snibgo this works a treat:

Code: Select all

$cmd = " -size {$width}x{$height} xc:none -fill red -draw \"roundRectangle 0,0 {$width},{$height} 20,20\" ".
 " ( -size {$size[0]}x{$size[1]} xc:none -fill white -draw \"roundRectangle 0,0 {$size[0]},{$size[1]} 15,15\" $image -compose SrcIn -composite ) -gravity center -compose over -composite ";
$cmd =

Code: Select all

-size 646x340 xc:none -fill red -draw "roundRectangle 0,0 646,340 20,20" ( -size 636x330 xc:none -fill white -draw "roundRectangle 0,0 636,330 15,15" small.jpg -compose SrcIn -composite ) -gravity center -compose over -composite 
Image
Likao

Re: Rounded corner border on rounded corner image

Post by Likao »

im trying to do the same thing, but your code doesnt work for me, im using:

Code: Select all

-size 301x169 xc:none -fill red -draw "roundRectangle 0,0 301,169 20,20" ( -size 291x159 xc:none -fill white -draw "roundRectangle 0,0 291,159 15,15" images/VVD0900041-301-final.jpg -compose SrcIn -composite ) -gravity center -compose over -composite png:-
and i get "syntax error near unexpected token `(' "
Likao

Re: Rounded corner border on rounded corner image

Post by Likao »

scratch that, all i needed was one more escape :)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Rounded corner border on rounded corner image

Post by Bonzo »

Sorry the code I posted was using XAMPP on Windows; on Linux you need to escape the ( & ) with a \
Post Reply