Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: max(five, six)?
(Example: nine)

The Note You're Voting On

pilot at myupb dot com
17 years ago
Thanks to the people who contributed the code for the 90 180 and 270 rotations. I needed a full implementation however so I wrote one. By no mean do I think this is the best way of doing it, I just whipped this together for myself. Seems to work good for me.

Note: I didn't want the image to be shrunk when rotating so this implementation will keep the size of the original image but just rotate it.

<?php
if(!function_exists("imagerotate")) {
function
imagerotate(&$srcImg, $angle, $transparentColor = null) {
$srcw = imagesx($srcImg);
$srch = imagesy($srcImg);

if(
$angle == 0) return $srcImg;

// Convert the angle to radians
$pi = 3.141592654;
$theta = $angle * $pi / 180;

// Get the origin (center) of the image
$originx = $srcw / 2;
$originy = $srch / 2;

// The pixels array for the new image
$pixels = array();
$minx = 0;
$maxx = 0;
$miny = 0;
$maxy = 0;
$dstw = 0;
$dsth = 0;

// Loop through every pixel and transform it
for($x=0;$x<$srcw;$x++) {
for(
$y=0;$y<$srch;$y++) {
list(
$x1, $y1) = translateCoordinate($originx, $originy, $x, $y, false);

$theta1 = 0;
$noTranslate = false;

// Determine the angle of original point
if($x1 > 0 && $y1 > 0) {
// Quadrant 1
$theta1 = atan($y1/$x1);
} elseif(
$x1 < 0 && $y1 > 0) {
// Quadrant 2
$theta1 = $pi - atan($y1/abs($x1));
} elseif(
$x1 < 0 && $y1 < 0) {
// Quadrant 3
$theta1 = $pi + atan($y1/$x1);
} elseif(
$x1 > 0 && $y1 < 0) {
// Quadrant 4
$theta1 = 2 * $pi - atan(abs($y1)/$x1);
} elseif(
$x1 == 0 && $y1 > 0) {
$theta1 = $pi / 2;
} elseif(
$x1 == 0 && $y1 < 0) {
$theta1 = 3 * $pi / 2;
} elseif(
$x1 > 0 && $y1 == 0) {
$theta1 = 0;
} elseif(
$x1 < 0 && $y1 == 0) {
$theta1 = $pi;
} else {
// Only case left should be $x1 == 0 && $y1 == 0
$noTranslate = true;
}

// Translate the position
if(!$noTranslate) {
// Calculate the new angle
$theta2 = $theta1 + $theta;

// Make sure theta2 is in between 0 - 2pi
while($theta2 < 0) $theta2 += 2 * $pi;
while(
$theta2 > (2 * $pi)) $theta2 -= 2 * $pi;

$radius = sqrt($x1*$x1 + $y1*$y1);

$x2 = ($radius * cos($theta2));
$y2 = ($radius * sin($theta2));
} else {
$x2 = $x1;
$y2 = $y1;
}

// Store the pixel color
$pixels[] = array($x2, $y2, imagecolorat($srcImg, $x, $y));

// Check our boundaries
if($x2 > $maxx) $maxx = $x2;
if(
$x2 < $minx) $minx = $x2;
if(
$y2 > $maxy) $maxy = $y2;
if(
$y2 < $miny) $miny = $y2;
}
}

// Determine the new image size
$dstw = $maxx - $minx + 1;
$dsth = $maxy - $miny + 1;

// Create our new image
$dstImg = imagecreatetruecolor($dstw, $dsth);

// Fill the background with our transparent color
if($transparentColor == null) $transparentColor = imagecolorallocate($dstImg, 1, 2, 3);
imagecolortransparent($dstImg, $transparentColor);
imagefilledrectangle($dstImg, 0, 0, $dstw + 1, $dsth + 1, $transparentColor);

// Get the new origin
$neworiginx = -$minx;
$neworiginy = -$miny;

// Fill in the pixels
foreach($pixels as $data) {
list(
$x, $y, $color) = $data;
list(
$newx, $newy) = translateCoordinate($neworiginx, $neworiginy, $x, $y);
imagesetpixel($dstImg, $newx, $newy, $color);
}

return
$dstImg;
}

/**
* Translates from mathematical coordinate system to computer coordinate system using
* origin coordinates from the computer system or visa versa
*
* @param int $originx
* @param int $originy
* @param int $x
* @param int $y
* @param bool $toComp
* @return array(int $x, int $y)
*/
function translateCoordinate($originx, $originy, $x, $y, $toComp=true) {
if(
$toComp) {
$newx = $originx + $x;
$newy = $originy - $y;
} else {
$newx = $x - $originx;
$newy = $originy - $y;
}

return array(
$newx, $newy);
}
}
?>

<< Back to user notes page

To Top