This is based on the Skew function from designerkamal at gmail dot com.
This is a function for skewing images in PHP with anti-aliasing. It works with alpha PNG images.
Warning: the bigger the image you skew, the longer it will take to process. It's about 3 times longer than without anti-aliasing.
<?php
function imageskewantialiased($img, $skew_val)
{
$width = imagesx($img);
$height = imagesy($img);
$height2 = $height + ($width * $skew_val);
$imgdest = imagecreatealpha($width, $height2);
for($x = 0, $level = 0; $x < $width - 1; $x++)
{
$floor = floor($level);
if ($level == $floor)
imagecopy($imgdest, $img, $x, $level, $x, 0, 1, $height - 1);
else
{
$temp = $level - $floor;
$color1 = imagecolorsforindex($img, imagecolorat($img, $x, 0));
$alpha = $color1['alpha'] + ($temp * 127);
if ($alpha < 127)
{
$color = imagecolorallocatealpha($imgdest, $color1['red'], $color1['green'], $color1['blue'], $alpha);
imagesetpixel($imgdest, $x, $floor, $color);
}
for($y = 1; $y < $height - 1; $y++)
{
$color2 = imagecolorsforindex($img, imagecolorat($img, $x, $y));
$alpha = ($color1['alpha'] * $temp) + ($color2['alpha'] * (1 - $temp));
if ($alpha < 127)
{
$red = ($color1['red'] * $temp) + ($color2['red'] * (1 - $temp));
$green = ($color1['green'] * $temp) + ($color2['green'] * (1 - $temp));
$blue = ($color1['blue'] * $temp) + ($color2['blue'] * (1 - $temp));
$color = imagecolorallocatealpha($imgdest, $red, $green, $blue, $alpha);
imagesetpixel($imgdest, $x, $floor + $y, $color);
}
$color1 = $color2;
}
$color1 = imagecolorsforindex($img, imagecolorat($img, $x, $height - 1));
$alpha = $color1['alpha'] + ((1 - $temp) * 127);
if ($alpha < 127)
{
$color = imagecolorallocatealpha($imgdest, $color1['red'], $color1['green'], $color1['blue'], $alpha);
imagesetpixel($imgdest, $x, $floor + $height - 1, $color);
}
}
$level += $skew_val;
}
return $imgdest;
}
function imagecreatealpha($width, $height)
{
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
for ($x = 0; $x < $width; $x++)
{
for ($y = 0; $y < $height; $y++)
{
imagesetpixel($img, $x, $y, $trans);
}
}
return $img;
}
imagepng(imageskewantialiased(imagecreatefrompng('test.png'), 0.15), 'skew.png');
?>