Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:08:19+00:00 2026-06-11T07:08:19+00:00

i have this function that transforms image to trapezoid using PHP GD: function perspective($i,$gradient=0.9,$rightdown=true,$background=0xFFFFFF)

  • 0

i have this function that transforms image to trapezoid using 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;
 }

But i cant modify it to produce isosceles trapezoid, i think there needs just one small modification, but i cant figure it outh (i tried lot of things).

Can someone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:08:21+00:00Added an answer on June 11, 2026 at 7:08 am

    Right, basically that code should generate the right values, but due to a bug has a lot of cludges in place to get a trapezium shape. The bug is that the copy of each line has the destination-y and the source-y values transposed. The source-y should always be 0, the destination-y should change.

    There were also a few other small numerical bugs and double rounding at unnecessary points throwing off the results.

    Also, the variable naming was atrocious, so I have rewritten it so that the entire function is clear.

    Try the following:

    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;
    }
    

    The essential mechanism of the inner loop, as before, is to take each column of pixels, along the x-axis and resize them over a gradient. It naturally creates an isosceles trapezium. In order to create a non-isosceles trapezoid, another gradient would have to be specified. Alternatively, a set of start and end y-values could be specified and the gradients calculated from them.

    Whilst this example works along the x-axis, in either direction as before, it could just as easily work along the y-axis (or the image could be rotated 90 degrees, processed, then rotated back).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Javascript function that sends username and password to php file through
I have this function that I would like to condense into some iterator. How
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function that takes the input of a, runs a calculation and
I have this function that switches the HTML contents from one element on a
So I have this function that forks N number of child processes. However it
I have this jquery function that works, except I need to add something. I
I have this caption function that worlds great. If I fill in the alt
I have this function called ProperCase that takes a string, then converts the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.