Page 1 of 1

Rounded corner border on rounded corner image

Posted: 2010-06-10T11:07:17-07:00
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

Re: Rounded corner border on rounded corner image

Posted: 2010-06-10T11:30:45-07:00
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.

Re: Rounded corner border on rounded corner image

Posted: 2010-06-10T11:37:04-07:00
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 - 

Re: Rounded corner border on rounded corner image

Posted: 2010-06-10T12:35:23-07:00
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

Re: Rounded corner border on rounded corner image

Posted: 2010-06-10T12:58:57-07:00
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 

Re: Rounded corner border on rounded corner image

Posted: 2010-06-10T13:25:10-07:00
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".

Re: Rounded corner border on rounded corner image

Posted: 2010-06-10T14:02:18-07:00
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

Re: Rounded corner border on rounded corner image

Posted: 2010-06-13T09:55:08-07:00
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 `(' "

Re: Rounded corner border on rounded corner image

Posted: 2010-06-13T10:31:07-07:00
by Likao
scratch that, all i needed was one more escape :)

Re: Rounded corner border on rounded corner image

Posted: 2010-06-13T13:23:02-07:00
by Bonzo
Sorry the code I posted was using XAMPP on Windows; on Linux you need to escape the ( & ) with a \