PHP GD image perspective

前端 未结 2 1286
粉色の甜心
粉色の甜心 2020-12-09 23:39

Hi is it possible to transform an images perspective. so it\'s new shape is an isosceles trapezoid? I saw a solution using Imagick, but that would involve possibly rewriting

相关标签:
2条回答
  • 2020-12-10 00:15

    GD does not support 3D image manipulations :( The solution using ImageMagick is not complex: http://valokuva.org/?p=112

    0 讨论(0)
  • 2020-12-10 00:24

    I have improved the function that James have posted.

    Added:

    • vertical / horizontal perspective support
    • transparency
    • it looks more like rotating objects in 3d now (closer ojects are bigger while further away are smaller)

    Here is my function:

    define("TOP",0);
    define("BOTTOM",1);
    define("LEFT",2);
    define("RIGHT",3);
    
    function perspective($i,$gradient=0.85,$rightdown=TOP,$background=0xFFFFFF, $alpha=0) {
            $w=imagesx($i);
            $h=imagesy($i);
            $col=imagecolorallocatealpha($i,($background>>16)&0xFF,($background>>8)&0xFF,$background&0xFF,$alpha);
    
            $mult=5;
            $li=imagecreatetruecolor($w*$mult,$h*$mult);
            imagealphablending($li,false);
            imagefilledrectangle($li,0,0,$w*$mult,$h*$mult,$col);
            imagesavealpha($li,true);
    
            imagecopyresized($li,$i,0,0,0,0,$w*$mult,$h*$mult,$w,$h);
            imagedestroy($i);
            $w*=$mult;
            $h*=$mult;
    
    
            $image=imagecreatetruecolor($w,$h);
            imagealphablending($image,false);
            imagefilledrectangle($image,0,0,$w,$h,$col);
            imagealphablending($image,true);
    
            imageantialias($image,true);
            $test=$h*$gradient;
    
            $rdmod=$rightdown%2;
            $min=1;
            if($rightdown<2){
                for($y=0;$y<$h;$y++){
                    $ny=$rdmod? $y : $h-$y;
                    $off=round((1-$gradient)*$w*($ny/$h));
                    $t=((1-pow(1-pow(($ny/$h),2),0.5))*(1-$gradient)+($ny/$h)*$gradient);
                    $nt=$rdmod? $t : 1-$t;
                    if(abs(0.5-$nt)<$min){
                        $min=abs(0.5-$nt);
                        $naty=$off;
                    }
                    imagecopyresampled($image,$li,
                                        round($off/2),$y,
                                        0,abs($nt*$h),
                                        $w-$off,1,
                                        $w,1);
                }
            } else {
                for($x=0;$x<$w;$x++){
                    $nx=$rdmod? $x : $w-$x;
                    $off=round((1-$gradient)*$h*($nx/$w));
                    $t=((1-pow(1-pow(($nx/$w),2),0.5))*(1-$gradient)+($nx/$w)*$gradient);
                    $nt=$rdmod? $t : 1-$t;
                    if(abs(0.5-$nt)<$min){
                        $min=abs(0.5-$nt);
                        $natx=$off;
                    }
                    imagecopyresampled($image,$li,
                                        $x,round($off/2),
                                        abs($nt*$w),0,
                                        1,$h-$off,
                                        1,$h);
                }
            }
            imagedestroy($li);
    
            imageantialias($image,false);
            imagealphablending($image,false);
            imagesavealpha($image,true);
    
            $i=imagecreatetruecolor(($w+$naty)/$mult,($h+$natx)/$mult);
            imagealphablending($i,false);
            imagefilledrectangle($i,0,0,($w+$naty)/$mult,($h+$natx)/$mult,$col);
            imagealphablending($i,true);
            imageantialias($i,true);
            imagecopyresampled($i,$image,0,0,0,0,($w+$naty)/$mult,($h+$natx)/$mult,$w,$h);
            imagedestroy($image);
            imagealphablending($i,false);
            imageantialias($i,false);
            imagesavealpha($i,true);
            return $i;
        }
    

    Examples:

    Original image: http://imgur.com/iX5Aski
    

    Using function:

    $img=perspective($img,0.5,TOP,0xFFFFFF,127);
    

    Result:

    $img=perspective($img,0.2,RIGHT,0xFFFFFF,127);
    

    Result:

    0 讨论(0)
提交回复
热议问题