But - It's possible with GD and fast !! compared to ImageMagick
Create a new image with size (2 * SourceWidth) / PI .
Go through each pixel of the new image and find the distance from the center. <i> d <sub> source sub> = hypot (x-CenterX, y-centerY)
Find the appropriate distance in the original image with d dest. = 2 * r * asin (d source / r) / 2
r is the half-width of the target image.
See Examples with a character label: 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);
}
source
share