Voting

: eight minus two?
(Example: nine)

The Note You're Voting On

etienne at escott dot info
15 years ago
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
// $img: handle of an image
// $skew_val: level of skew to apply (0 being none, 1 being 45°)
function imageskewantialiased($img, $skew_val)
{
$width = imagesx($img);
$height = imagesy($img);
$height2 = $height + ($width * $skew_val);

// See below for definition of imagecreatealpha
$imgdest = imagecreatealpha($width, $height2);

// Process the image
for($x = 0, $level = 0; $x < $width - 1; $x++)
{
$floor = floor($level);

// To go faster, some lines are being copied at once
if ($level == $floor)
imagecopy($imgdest, $img, $x, $level, $x, 0, 1, $height - 1);
else
{
$temp = $level - $floor;

// The first pixel of the line
// We get the color then apply a fade on it depending on the level
$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);
}

// The rest of the line
for($y = 1; $y < $height - 1; $y++)
{
// Merge this pixel and the upper one
$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;
}

// The last pixel of the line
$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);
}
}

// The line is finished, the next line will be lower
$level += $skew_val;
}

// Finished processing, return the skewed image
return $imgdest;
}

// Creates a new image of the size specified with a blank background (transparent)
function imagecreatealpha($width, $height)
{
// Create a normal image and apply required settings
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);

// Apply the transparent background
$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;
}

// Here's an example of how to use it
imagepng(imageskewantialiased(imagecreatefrompng('test.png'), 0.15), 'skew.png');
?>

<< Back to user notes page

To Top