Okay, probeer dan dit eens
<?php
$image = '027b.png'; // zelf aanpassen
$degrees = 8; // zelf aanpassen
$randDikte = 3; // zelf aanpassen
$vervang_zwart_niet = true;
$transparant = false;
switch ($_GET['p']) // voel je vrij om op een andere manier de funtie aan te roepen.
{
default:
if( file_exists($image) )
{
list($width, $height, $type, $attr) = getimagesize($image);
echo htmlHeading('<img src="index.php?p=i" />', 'Geroteerde png' ) ; // eventueel voeg je dit toe // width="'. $width .'" height="'. $height .'"
}
else
echo htmlHeading('
<div>File "'. $image .'" bestaat niet</div>', 'File "'. $image .'" bestaat niet');
break;
case 'i':
$img = LoadPNG($image, $degrees, $vervang_zwart_niet, $randDikte, $transparant);
imagepng($img);
imagedestroy($img);
break;
}
// deze functie laadt png bestand. Het bestand wordt geroteerd. De randen blijven transparant
function LoadPNG($imgname, $degrees = 0, $vervang_zwart_niet = 0, $randDikte = 0, $transparant = true)
{
$background = 0x000000; // zwart
try
{
if( file_exists($imgname) )
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);
list($width, $height, $type, $attr) = getimagesize($imgname);
if ( !empty($vervang_zwart_niet) )
vervangZwart($im, $width, $height);
vervangRand($im, $width, $height, $randDikte, $background );
if ($degrees )
{
$rotate = imagerotate($im, $degrees, 0);
if ($transparant)
{
$cBackground = imagecolorallocate($rotate, 0, 0, 0); // Zwarte kleur wordt als transparant gezet
imagecolortransparent($rotate, $cBackground);
}
}
header('Content-Type: image/png');
return ( $degrees ? $rotate : $im);
}
}
catch (Exception $e)
{
return null;
}
}
function vervangZwart($im, $width, $height)
{
for ($x = 0; $x < $width; $x++)
{
for ($y = 0; $y < $height; $y++)
{
$pixel = imagecolorat ( $im , $x , $y );
if ($pixel == 0)
imagesetpixel ( $im , $x , $y , 0x010101 );
}
}
}
function vervangRand($im, $width, $height, $randDikte, $background = 0x000000)
{
for ($x = 0; $x < $width; $x++)
{
for ($y = 0; $y < $height; $y++)
{
if ( ! isRand($x, $y, $width, $height, $randDikte) )
continue;
$laag = randLaag($x, $y, $width, $height, $randDikte); // aantal pixel vanaf de rand
$pixel = imagecolorat ( $im , $x , $y );
imagesetpixel ( $im , $x , $y , antiAlias($pixel, $background, $laag, $randDikte) );
}
}
}
function antiAlias($pixel, $background, $laag, $randDikte)
{
if ($background == 0)
{
$rgb = int2rgba($pixel);
$rgb['r'] = (int) $rgb['r'] * $laag / $randDikte;
$rgb['g'] =(int) $rgb['g'] * $laag / $randDikte;
$rgb['b'] = (int) $rgb['b'] * $laag / $randDikte;
return rgba2int($rgb['r'], $rgb['g'], $rgb['b']);
}
// TO DO: herberekenen voor andere achtergronden
}
function isRand($x, $y, $width, $height, $randDikte)
{
if ($x < $randDikte)
return true;
elseif ($y < $randDikte)
return true;
elseif ( ($width - $x) < $randDikte)
return true;
elseif ( ($height - $y) < $randDikte)
return true;
return false;
}
function randLaag($x, $y, $width, $height, $randDikte)
{
$infinity = 1000;
$pixelsVanDeRand = array();
$pixelsVanDeRand['left'] = ($x < $randDikte ? $x : $infinity);
$pixelsVanDeRand['top'] = ($y < $randDikte ? $y : $infinity);
$pixelsVanDeRand['right'] = ( ($width - $x) < $randDikte ? ($width - $x - 1) : $infinity);
$pixelsVanDeRand['bottom'] = ( ($height - $y) < $randDikte ? ($height - $y - 1) : $infinity);
$result = min($pixelsVanDeRand);
if ($result < $randDikte)
return (int) $result;
return 0;
}
function htmlHeading($body, $title)
{
return
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//NL" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>'. $title .'</title>
<style>
body {padding: 10px; background-color: #ffffff}
</style>
</head>
<body>
'. $body .'
</body>
</html>';
}
//
http://us.php.net/manual/en/function.imagecolorat.php
function rgba2int($r, $g, $b, $a=1) {
/*
This function builds a 32 bit integer from 4 values which must be 0-255 (8 bits)
Example 32 bit integer: 00100000010001000000100000010000
The first 8 bits define the alpha
The next 8 bits define the blue
The next 8 bits define the green
The next 8 bits define the red
*/
return ($a << 24) + ($b << 16) + ($g << 8) + $r;
}
function int2rgba($int) {
$a = ($int >> 24) & 0xFF;
$b = ($int >> 16) & 0xFF;
$g = ($int >> 8) & 0xFF;
$r = $int & 0xFF;
return array('r'=>$r, 'g'=>$g, 'b'=>$b, 'a'=>$a);
}
?>
(EDIT: aangepast)
De laatste veranderingen zijn vlug vlug in mekaar gestoken. De functies zijn aan verfijning toe. Voorlopig kan je er wel mee voort, denk ik.
Beschouw dit dus als een
quick and dirty versie