Issue
Is there a way to do a FishEye (or Barrel transformation) effect on a image with PHP-GD? I found this with some code, but I have a hard time porting it to PHP.
How can I implement a fisheye lens effect (barrel transformation) in MATLAB?
Solution
But - It is possible with GD and fast!! in comparison with ImageMagick
Create a new image with the size of (2*SourceWidth)/PI.
Walk trough each pixel of the new image and find the distance from the center.
dsource=hypot(x-centerX,y-centerY)
Find the corresponding distance in the source image by ddest.=2*r*asin(dsource/r)/2
r is the half width of the destination image.
See examples with bench mark: http://meindesign.net/picture2bubble/picture2bubble.php
function fisheye($infilename,$outfilename){
$im=imagecreatefrompng($infilename);
$ux=imagesx($im);//Source imgage width(x)
$uy=imagesy($im);//Source imgage height(y)
$umx=$ux/2;//Source middle
$umy=$uy/2;
if($ux>$uy)$ow=2*$uy/pi();//Width for the destionation image
else $ow=2*$ux/pi();
$out=imagecreatetruecolor($ow+1,$ow+1);
$trans=imagecolortransparent($out,ImageColorAllocate($out,0,0,0));
imagefill($im,1,1,$trans);
for($c=0;$c<imagecolorstotal($im);$c++){//Copy palette
$col=imagecolorsforindex($im,$c);
imagecolorset($out,$c,$col[red],$col[green],$col[blue]);
}
$om=$ow/2;//destination middle
for($x=0;$x<=$ow;++$x){//Loop X in destination image
for($y=0;$y<=$ow;++$y){//Loop y in destination image
$otx=$x-$om;//X in relation to the middle
$oty=$y-$om;//Y in relation to the middle
$oh=hypot($otx,$oty);//distance
$arc=(2*$om*asin($oh/$om))/(2);
$factor=$arc/$oh;
if($oh<=$om){//if pixle inside radius
$color=imagecolorat($im,round($otx*$factor+$umx),round($oty*$factor+$umy));
$r = ($color >> 16) & 0xFF;
$g = ($color >> 8) & 0xFF;
$b = $color & 0xFF;
$temp=imagecolorexact($out, $r, $g, $b);
imagesetpixel($out,$x,$y,$temp);
}
}
}
imagepng($out,$outfilename);
}
Answered By - B.F. Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.