Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pjdietz/ca815c0788ad4dfcccdc to your computer and use it in GitHub Desktop.
Save pjdietz/ca815c0788ad4dfcccdc to your computer and use it in GitHub Desktop.
Imagick::distrortImage control points array function
/**
* Returns an array of control points for use with Imagick::distortImage
*
* Offset floats describe the proportions between 0.0 and 1.0 to move the
* given anchor point toward the opposite corner of the image. For example,
* an image of size 100x100. $topLeftOffsetX = .2, $topLeftOffsetY = .5
* will move the top left corner down and right from (0,0) to (20,50).
* $bottomRightOffsetX = .2, $bottomRightOffsetY will move the bottom right
* corner up and left from (100,100) to (80,50).
*
* Providing 1.0 for all values will flip the image horizontally and
* vertically.
*
* @param int $width Width of the image to distort
* @param int $height Height of the image to distort
* @param float $topLeftOffsetX
* @param float $topLeftOffsetY
* @param float $topRightOffsetX
* @param float $topRightOffsetY
* @param float $bottomRightOffsetX
* @param float $bottomRightOffsetY
* @param float $bottomLeftOffsetX
* @param float $bottomLeftOffsetY
* @return float[] Array of control points for Imagick::distortImage
*/
function getControlPointsForPerspectiveDistortion(
$width,
$height,
$topLeftOffsetX = 0.0,
$topLeftOffsetY = 0.0,
$topRightOffsetX = 0.0,
$topRightOffsetY = 0.0,
$bottomRightOffsetX = 0.0,
$bottomRightOffsetY = 0.0,
$bottomLeftOffsetX = 0.0,
$bottomLeftOffsetY = 0.0
) {
return array(
// Top left source
0, 0,
// Top left destination
$width * $topLeftOffsetX, $height * $topLeftOffsetY,
// Top right source
$width, 0,
// Top right destination
$width - ($width * $topRightOffsetX), $height * $topRightOffsetY,
// Bottom right source
$width, $height,
// Bottom right destination
$width - $width * $bottomRightOffsetX,
// Bottomleft source
$height - ($height * $bottomRightOffsetY),
// Bottom left destination
0, $height, $width * $bottomLeftOffsetX, $height - $height * $bottomLeftOffsetY
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment