如何用PHP GD创建鱼眼效果

Daa*_*tje 5 php gd fisheye

有没有办法用PHP-GD对图像进行FishEye(或Barrel转换)效果?我发现这有一些代码,但我很难将它移植到PHP.

如何在MATLAB中实现鱼眼镜头效果(桶形变换)?

dvb*_*dvb 6

带GD的PHP无法以可接受的方式做这样的事情,逐像素处理图像会非常慢......

Imagick确实支持一个函数,使您能够编写自己的表达式(fximage),然后在Imagick内部处理所有内容.

所以我找到了一种方法来做你在Imagick中所要求的,我从"Scott builds Software"博客中得到了表达- 在imagick中的鱼眼效果.您可以在他的博客中阅读该表达的完整说明.有关此功能的更多文档可在官方ImageMagick网站上获得,您可以在那里了解如何构建自己的表达式.

请注意,关于返回值的PHP文档是不正确的,我也在那里评论过.该函数返回实际的Imagick对象.

所以这是你的代码:

<?php
/* Create new object */
$im = new Imagick();
/* Create new checkerboard pattern */
$im->newPseudoImage(100, 100, "pattern:checkerboard");
/* Set the image format to png */
$im->setImageFormat('png');
/* Fill background area with transparent */
$trans = Imagick::VIRTUALPIXELMETHOD_TRANSPARENT;
$im->setImageVirtualPixelMethod($trans);
/* Activate matte */
$im->setImageMatte(true);

/* This is the expression that define how to do the fisheye effect */
$distort_expression = 
'kk=w*0.5;
ll=h*0.5;
dx=(i-kk);
dy=(j-ll);
aa=atan2(dy,dx);
rr=hypot(dy,dx);
rs=rr*rr/hypot(kk,ll);
px=kk+rs*cos(aa);
py=ll+rs*sin(aa);
p{px,py}';

/* Perform the distortion */ 
$im = $im->fxImage($distort_expression);

/* Ouput the image */   
header("Content-Type: image/png");
echo $im;
?>
Run Code Online (Sandbox Code Playgroud)

无论如何,请记住,这仍然很慢,小心你用它做的任何事......


B.F*_*.F. 2

但是 - GD 是可能的,而且速度很快!与 ImageMagick 相比
在此输入图像描述 创建一个大小为(2*SourceWidth)/PI的新图像。
遍历新图像的每个像素并找到距中心的距离。 d source =hypot(x-centerX,y-centerY)通过d
dest 求源图像中对应的距离=2*r*asin(d source /r)/2 r是目标图像的半宽。 请参阅带有基准标记的示例: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);
     }
Run Code Online (Sandbox Code Playgroud)