Set Image Border

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
pranav
Posts: 16
Joined: 2014-05-20T23:41:16-07:00
Authentication code: 6789

Set Image Border

Post by pranav »

Hi All,
I have tree sample images in landscape and portrait format, Now i want to set dynamically set border color on its edges, I am little bit slow in imagemagick , so not able to perform this.
Can any one look into this.

My Sample code and sample images can be downloaded from below path.

https://www.dropbox.com/sh/m2w7qczkfx0m ... F9wsRwAxPa


Thanks in Advance.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Set Image Border

Post by Bonzo »

I am not sure what you mean about "set dynamically set border color ".

Can you show what you want with some example images created in another program?
pranav
Posts: 16
Joined: 2014-05-20T23:41:16-07:00
Authentication code: 6789

Re: Set Image Border

Post by pranav »

Hi Bonzo, Thanks for your reply, I have uploaded output.zip onto same dropbox url,

I mean by dynamic border is when i am trying to create 3d image it leaves some gap between image and edges,

But when you put the images one by one in script $image variable, you will get the output images, same those i have uploaded in output.zip file
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Set Image Border

Post by snibgo »

I don't so PHP so can't test, but I notice that you don't "+repage" after the crops. That may be correct, or may cause problems.
snibgo's IM pages: im.snibgo.com
pranav
Posts: 16
Joined: 2014-05-20T23:41:16-07:00
Authentication code: 6789

Re: Set Image Border

Post by pranav »

Nop, +repage is not working with the same, have tried with the same before and now also........... :)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Set Image Border

Post by Bonzo »

An example of your expected result is always useful, try this:

Code: Select all

<?php  
// Input image 
$image = 'Chrysanthemum.jpg'; 

// Resize to a png to stop quality loss 
exec("convert $image -thumbnail 500x500 temp.png"); 

// Get the size of the original image 
$size = getimagesize('temp.png'); 
// The canvas frame edge 
$edge = $size[0]*.06; 
// New central portion width 
$width = $size[0]; 
// New central portion height 
$height = $size[1]; 
// Reduction of edge width 
$shrink = round(($edge * 0.6 ), 2); 
// Angle of the edge 
$angle = 30; 
// Change in edge height 
// Convert angle to radian > angle in degrees * Pi / 180  
$radian = ( $angle * Pi() ) / 180; 
$alpha = round((abs(tan($radian)) * $shrink), 2); 
// Edge short side 
$short_side = round( ($height - $alpha ), 2); 
// Top long side  
$top_long = round($width + $shrink, 2); 

// Resize the center
exec("convert $image -thumbnail 500x500 center.png"); 

// Crop for the edges 
$cmd = " temp.png ( -clone 0 -crop {$width}x{$edge}+0+0 -flip -fill #FFC0CB -colorize 100% -write top.png +delete )". 
" -gravity northeast -crop {$edge}x{$height}+0+0 -flop -fill #E75480 -colorize 100%  -write right.png +delete null: "; 
exec("convert $cmd "); 

// Perspective for the RHS 
$cmd = " right.png -virtual-pixel background -background none ". 
" +distort Perspective \"0,0 0,0  $edge,0 $shrink,-$alpha  $edge,$height $shrink,$short_side  0,$height 0,$height\" +repage -trim"; 
exec("convert $cmd right_edge.png"); 

// Perspective for the top 
$cmd = " top.png -virtual-pixel background -background none ". 
" +distort Perspective \"0,0 $shrink,0  $width,0 $top_long,0  $width,$edge $width,$alpha  0,$edge 0,$alpha\" +repage -trim"; 
exec("convert $cmd top_edge.png"); 

// There was a 1px gap between the RHS and main photo 
$tweek = $width-1; 

// Join the images 
$cmd = " ( -page -1,0 top_edge.png -page +0+$alpha center.png -page +$tweek+0 right_edge.png -background none -layers merge ) ". 
" ( +clone -background black -shadow 95x10+15+15 ) +swap -background none -layers merge +repage "; 
exec(" convert $cmd canvas1.png"); 

// Display the final image 
echo "<img src=\"canvas1.png\">";
?>
Notice the -fill and -colorize added to the // Crop for the edges section
pranav
Posts: 16
Joined: 2014-05-20T23:41:16-07:00
Authentication code: 6789

Re: Set Image Border

Post by pranav »

Hey Bonzo,
That was amazing, now I got where i was doing mistake, there was wrong calculation for image edge sizes, for height and width, and the alternative you have given for -fill command is very qualifies logic, we can use it in the alternate of border color, i am right i think.

You gave a amazing help and guidance. Thanks a lot and keep it up with the forum. See ya in next lesson and learning in imagick..... :D

Cheers..

Thank again for your time and guidance....
Post Reply