note 91973 added to function.imagecopy

From: Date: Fri, 03 Jul 2009 09:16:45 +0000
Subject: note 91973 added to function.imagecopy
Groups: php.notes 
Request: Send a blank email to php-notes+get-157868@lists.php.net to get a copy of this message
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'); ?> ---- Server IP: 216.235.13.26 Probable Submitter: 70.55.87.141 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopy.php Edit -- https://master.php.net/note/edit/91973 Del: integrated -- https://master.php.net/note/delete/91973/integrated Del: useless -- https://master.php.net/note/delete/91973/useless Del: bad code -- https://master.php.net/note/delete/91973/bad+code Del: spam -- https://master.php.net/note/delete/91973/spam Del: non-english -- https://master.php.net/note/delete/91973/non-english Del: in docs -- https://master.php.net/note/delete/91973/in+docs Del: other reasons-- https://master.php.net/note/delete/91973 Reject -- https://master.php.net/note/reject/91973 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157868) next »