2012-09-12 3 views
0

나는이 PHP의 GD 사용하여 사다리꼴 이미지 변환이 기능 :이미지 등변 사다리꼴 PHP GD

function perspective($i,$gradient=0.9,$rightdown=true,$background=0xFFFFFF) { 
    $mult=3; 
    $w=imagesx($i); 
    $h=imagesy($i); 
    $image=imagecreatetruecolor($w*$mult,$h*$mult); 
    imagecopyresized($image,$i,0,0,0,0,$w*$mult,$h*$mult,$w,$h); 
    imagedestroy($i); 
    $w*=$mult; 
    $h*=$mult; 
    $im=imagecreatetruecolor($w,$h); 
    $background=imagecolorallocate($im,($background>>16)&0xFF,($background>>8)&0xFF,$background&0xFF); 
    imagefill($im,0,0,$background); 
    imageantialias($im,true); 
    $nh=$h-($h*$gradient); 
    for ($x=0; $x<$w; $x++) { 
     $ni=(($rightdown) ? $x : $w-$x); 
     $p=intval($h-(($ni/$w)*$nh)); 
     if (($p%2)<>0) 
     $p-=1; 
     $nx=intval(($p-$h)/2); 
     imagecopyresampled($im,$image,$x,0,$x,$nx,1,$p,1,$h-1); 
     imageline($im,$x,$h-1,$x,$h+$nx,$background); 
     imageline($im,$x,0,$x,-$nx-1,$background); 
    } 
    imagedestroy($image); 
    imagefilter($im,IMG_FILTER_SMOOTH,10); 
    $i=imagecreatetruecolor($w/$mult,$h/$mult); 
    imageantialias($i,true); 
    imagecopyresampled($i,$im,0,0,0,0,$w,$h,$w*$mult,$h*$mult); 
    imagedestroy($im); 
    return $i; 
} 

하지만 난이 등변 사다리꼴을 생산하기 위해 수정 캔트, 난 그냥 하나의 작은 수정이 필요하다고 생각을하지만 난 캔트는 그것을 outh (나는 많은 것을 시도했다) 계산한다.

누군가 도움을 줄 수 있습니까?

+1

아마도 현재 코드에서 사다리꼴이 어떻게 생성되었는지 알 수 없을 것입니다. – Orbling

+0

글쎄, 어떻게 만들어 졌는지 이해할 수 있지만, 다른 방법으로 작동시키기에 충분한 php (gd) 지식이 없다. – SomeoneS

답변

1

오른쪽 코드는 기본적으로 올바른 값을 생성해야하지만, 버그로 인해 사다리꼴 모양을 얻는 데 많은 cludges가 있습니다. 문제는 각 줄의 복사본이 destination-y이고 source-y 값이 바뀐 것입니다. source-y은 항상 0이어야하며 destination-y은 변경해야합니다.

작은 수치 버그가 몇 개 있었으며 불필요한 점에서 결과가 두 번 반올림되었습니다.

또한 변수 명명은 잔인했기 때문에 전체 함수가 명확하도록 변수 이름을 다시 작성했습니다.

는 다음을 시도

function makeTrapeziumImage($image, $gradient, $rightToLeft = false, $background = 0xFFFFFF, $supersampleScale = 3) { 
    $originalWidth = imagesx($image); 
    $originalHeight = imagesy($image); 

    $supersampledWidth = $originalWidth * $supersampleScale; 
    $supersampledHeight = $originalHeight * $supersampleScale; 

    $supersampledImage = imagecreatetruecolor($supersampledWidth, $supersampledHeight); 

    imagecopyresized($supersampledImage, $image, 
        0, 0, 0, 0, 
        $supersampledWidth, $supersampledHeight, $originalWidth, $originalHeight); 

    $workingImage = imagecreatetruecolor($supersampledWidth, $supersampledHeight); 

    $backgroundColour = imagecolorallocate($workingImage, ($background >> 16) & 0xFF, ($background >> 8) & 0xFF, $background & 0xFF); 
    imagefill($workingImage, 0, 0, $backgroundColour); 

    imageantialias($workingImage,true); 

    $endHeight = $supersampledHeight - ($supersampledHeight * $gradient); 

    for ($x = 0; $x < $supersampledWidth; $x++) { 
    $cX = ($rightToLeft ? $supersampledWidth - $x : $x); 

    $dstHeight = $supersampledHeight - ((($cX + 1)/$supersampledWidth) * $endHeight); 

    $dstY = intval(($supersampledHeight - $dstHeight)/2) - 1; // -1 required as zero-indexed 
    $dstY = ($dstY < 0 ? 0 : $dstY); // Rounding can make $dstY = -1 

    $dstHeight = intval($dstHeight); // Round the height after calculating $dstY 

    imagecopyresampled($workingImage, $supersampledImage, 
         $cX, $dstY, $cX, 0, 
         1, $dstHeight, 1, $supersampledHeight); 
    } 

    imagedestroy($supersampledImage); 
    imagefilter($workingImage, IMG_FILTER_SMOOTH, 10); 

    $resizedImage = imagecreatetruecolor($originalWidth, $originalHeight); 
    imageantialias($resizedImage, true); 

    imagecopyresampled($resizedImage, $workingImage, 
        0, 0, 0, 0, 
        $originalWidth, $originalHeight, $supersampledWidth, $supersampledHeight); 

    imagedestroy($workingImage); 

    return $resizedImage; 
} 

내부 루프의 본질적인기구는, 이전과 같이, 상기 X 축 방향으로, 각 픽셀 칼럼을 가지고 구배 위에 크기를 조정하는 것이다. 자연스럽게 이등변 사다리꼴을 만듭니다. 사다리꼴이 아닌 것을 만들려면 다른 그라디언트를 지정해야합니다. 또는 시작과 끝 집합 y-values을 지정하고 그라데이션을 계산할 수 있습니다.

이 예제는 이전과 같이 어느 방향 으로든 x 축을 따라 작동하지만 y 축을 따라 쉽게 작업 할 수도 있습니다 (또는 이미지를 90도 회전하고 처리 한 다음 다시 회전시킬 수 있음).

+0

와우 맨, 환상적입니다, 충분히 감사드립니다. PHP GD에서 이등변 사다리꼴을 만들려면 적절한 함수가 없습니다. 이제는 더 쉽게 이해할 수 있습니다. 다시 Txx : – SomeoneS

+1

괜찮아. 이미지를 전문적으로 변형 한 것이므로 슬프게 내장 될 가능성은 거의 없습니다. Image Magick과 같은보다 강력한 이미지 라이브러리를 사용했다면 아마도. – Orbling

+1

원래 이미지가 수신되는 이미지를 파괴했음을 유의하십시오. 그래서 더 이상 원본 이미지가 필요 없다면'imagedestroy() '해야합니다. – Orbling