$value) { $value[0] -= $center_x; //Translate coords to center for rotation $value[1] -= $center_y; $x = $value[0] * $cosangle + $value[1] * $sinangle; $y = $value[1] * $cosangle - $value[0] * $sinangle; $max_x = max($x, $max_x); $min_x = min($x, $min_x); $max_y = max($y, $max_y); $min_y = min($y, $min_y); } $rot_width = (int)$max_x - $min_x; $rot_height = (int)$max_y - $min_y; $rot_center_x = floor($rot_width/2); $rot_center_y = floor($rot_height/2); } $rotate = imagecreatetruecolor($rot_width, $rot_height); $bg = imagecolorallocatealpha($rotate, $r, $g, $b, $a); imagefilledrectangle($rotate, 0, 0, $rot_width, $rot_height, $bg); imagealphablending($rotate, false); imagesavealpha($rotate, true); switch ($angle) { case 90: $rot_width--; for ($y = 0; $y < $height; ++$y) for ($x = 0; $x < $width; ++$x) imagesetpixel($rotate, $rot_width - $y, $x, imagecolorat($im, $x, $y)); break; case 270: $rot_height--; for ($y = 0; $y < $height; ++$y) for ($x = 0; $x < $width; ++$x) imagesetpixel($rotate, $y, $rot_height - $x, imagecolorat($im, $x, $y)); break; case 180: $rot_width--; $rot_height--; for ($y = 0; $y < $height; ++$y) for ($x = 0; $x < $width; ++$x) imagesetpixel($rotate, $rot_width - $x, $rot_height - $y, imagecolorat($im, $x, $y)); break; default: for ($y = 0; $y < $rot_height; ++$y) { for ($x = 0; $x < $rot_width; ++$x) { $mod_y = $rot_center_y-$y; $mod_x = $rot_center_x-$x; $old_x = $mod_x * $cosangle + $mod_y * $sinangle + $center_x; $old_y = $mod_y * $cosangle - $mod_x * $sinangle + $center_y; if ($old_x >= 0 && $old_x < $width && $old_y >= 0 && $old_y < $height) { $color = imagecolorat($im, $old_x, $old_y); imagesetpixel($rotate, $x, $y, $color); } } } } return $rotate; }