有没有办法对具有PHP-GD的图像进行鱼眼(或枪管转换)效果?我发现了一些代码,但是我很难将其移植到PHP。

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

有帮助吗?

解决方案

但 - GD和快速的可能! 与ImageMagick相比
enter image description here创建一个大小的新图像 (2*sourceWidth)/pi.
在新图像的每个像素上行走槽,并找到距中心的距离。d资源= hypot(x-centerx,y-Centery)
通过 d命运。= 2*r*asin(D资源/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);
     }

其他提示

带有GD的PHP不能以可接受的方式做这样的事情,处理图像像素像素真的很慢。

Imagick确实支持一个使您能够编写自己的表达式的函数(fximage),之后,一切都将在Imagick内部进行内部处理。

因此,我找到了一种方法来完成您在Imagick中要求的事情,我从 “ Scott构建软件”博客 - Imagick中的鱼眼效应. 。您可以在他的博客中阅读该表达式的完整解释。该功能的进一步文档可在官方中获得 成像 网站,您可以在那里学习如何构建自己的表达方式。

请注意,有关退货值的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;
?>

无论如何,请记住,这仍然很慢,请小心您所做的一切...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top