2010-12-09 5 views
5

PHP-GD로 이미지에 FishEye (또는 Barrel 변환) 효과를 적용 할 수있는 방법이 있습니까? 이 코드는 일부 코드에서 발견되었지만 PHP로 이식하는 데 어려움이 있습니다. GD와PHP GD로 어안 효과를 만드는 방법

How can I implement a fisheye lens effect (barrel transformation) in MATLAB?

+0

당신은 SLOOOWWWW 될 것입니다, 이는 직접 자체 너 한테 어안를 구현해야 하나. 또는이 작업을 수행 할 다른 방법을 찾으십시오. exec() 등으로 포토샵에 매크로를 사용하는 경우 ...이 작업을 수행 한 사람을 알지 못합니다 ... – DampeS8N

+0

C 프로그래밍 지식이 있으면 * gd * 소스 코드를 다운로드하고 구현할 수 있습니다 새로운 기능 - 그것을 게시하십시오! 코드가 너무 오래되었습니다 ... 어쨌든 솔루션을 찾으면 게시하십시오! 나는 또한 궁금해서 ... Btw 당신의 OS는 무엇입니까? –

+0

그것은 할 수 있지만, MATLAB 코드보다 더 복잡합니다. PHP는 바이트 연산에 적합하지 않으므로 3 계층 배열 (R, G, B)이 필요할 것입니다. 그래서 나는'exec (imagemagick)'에 의지하는 것도 제안 할 것이다. – mario

답변

2

하지만 - 그것은 가능합니다 : 그래서 여기

은 당신의 코드 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); 
    } 
6

PHP는 ... 이미지 픽셀 단위 정말 느려집니다 처리, 수용 가능한 방법으로 그런 일을 할 수 없습니다

Imagick 활성화 기능을 지원하지 않습니다 자신 만의 표현식 (fximage)을 작성한 후에 모든 것이 Imagick에서 내부적으로 처리됩니다.

Imagick에서 요청한 것을 수행 할 방법을 찾았으므로 "Scott builds Software" blog - fisheye effect in 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; 
?> 

...

+0

대단히 감사합니다! 너는 나의 영웅이야! :디 – Daantje

관련 문제