Skip to content

Instantly share code, notes, and snippets.

@Hemie143
Created November 6, 2019 22:38
Show Gist options
  • Save Hemie143/56554bbc2eaf31da70b219e2748534c5 to your computer and use it in GitHub Desktop.
Save Hemie143/56554bbc2eaf31da70b219e2748534c5 to your computer and use it in GitHub Desktop.
Geocache rotating image
<?php
// File and rotation
$overFilename = 'Map_Plain.png'; // this is the file that goes over top
$underFilename = 'Map_Gat.png'; // the underneath image
// calculate the rotation
//$degrees = $_GET["angle"];
if (date("G") < 5)
{$hr = (date("G") + 24);}
else {$hr = (date("G"));}
//$secs = (((date("G")-5) * 3600) + (date("i") * 60) + date("s")) / 240;
$secs = ((($hr -5) * 3600) + (date("i") * 60) + date("s")) / 120;
//$startColor = "0a053a";
//$endColor = "fff600";
//$step = "43200";
$overlayImg = imagecreatefrompng($overFilename);
$dest = imagecreatefrompng($underFilename);
$clockImg = createClock();
$timeTextImg = createTimeTextImage();
imagecopymerge($overlayImg, $clockImg, 105,275, 0, 0, 250, 70, 100);
imagecopymerge($overlayImg, $timeTextImg, 125,210, 0, 0, 150, 50, 100);
$rotatedImage = rotateImage($overlayImg, $secs);
$black = imagecolorallocate($rotatedImage, 0, 0, 0);
//$grey = imagecolorallocate($dest, 192, 192, 192);
imagecolortransparent($rotatedImage, $black);
//imagecolortransparent($dest, $grey);
imagecopymerge($dest, $rotatedImage, 100, 107, 0, 0, 410, 410,100);
imagealphablending( $dest, false );
imagesavealpha( $dest, true );
header('Content-Type: image/png');
imagepng($dest ) ;
function createTimeTextImage(){
// Create the image
$image = imagecreatetruecolor(180, 100);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 39, 9, 225);
$orange = imagecolorallocate($image, 225, 151, 9);
$grey = imagecolorallocate($image, 192, 192, 192);
imagefilledrectangle($image, 0, 0, 190, 100, $white);
$curTime = "TIME";
// Replace path by your own font path
//$font = 'ALGER.TTF';
$font = './ARLRDBD.TTF';
imagecolortransparent($image, $white); //change white to grey
// Add some shadow to the text
// Add the text
imagettftext($image, 25, 0, 30, 25, $orange,$font, $curTime);
// Using imagepng() results in clearer text compared with imagejpeg()
return $image;
}
function createClock(){
if (date("G") < 5)
{$hr = (date("G") + 24);}
else {$hr = (date("G"));}
// Create the image
$image = imagecreatetruecolor(350, 110);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 39, 9, 225);
$orange = imagecolorallocate($image, 225, 151, 9);
$grey = imagecolorallocate($image, 192, 192, 192);
imagefilledrectangle($image, 0, 0, 499,210, $white);
$hour = ($hr-5) ;
$min = (date("i")) ;
$curTime = $hour . ":" . $min;
// Replace path by your own font path
//$font = 'ALGER.TTF';
$font = './ARLRDBD.TTF';
imagecolortransparent($image, $white);
// Add some shadow to the text
// Add the text
imagettftext($image, 40, 0, 20, 70, $orange, $font, $curTime);
// Using imagepng() results in clearer text compared with imagejpeg()
return $image;
}
function rotateImage ($image, $angle)
{
while ($angle > 360){
$angle = $angle - 360;
}
while ($angle < 0 ){
$angle = $angle + 360;
}
if ( ($angle < 0) || ($angle > 360) )
{
exit ("Error, angle passed out of range: [0,360]");
}
$width = imagesx ($image);
$height = imagesy ($image);
$dstImage = imagecreatetruecolor ($width, $height);
if ( ($angle == 0) || ($angle == 360) )
{
// Just copy image to output:
imagecopy ($dstImage, $image, 0, 0, 0, 0, $width, $height);
}
else
{
$centerX = floor ($width / 2);
$centerY = floor ($height / 2);
// Run on all pixels of the destination image and fill them:
for ($dstImageX = 0; $dstImageX < $width; $dstImageX++)
{
for ($dstImageY = 0; $dstImageY < $height; $dstImageY++)
{
// Calculate pixel coordinate in coordinate system centered at the image center:
$x = $dstImageX - $centerX;
$y = $centerY - $dstImageY;
if ( ($x == 0) && ($y == 0) )
{
// We are in the image center, this pixel should be copied as is:
$srcImageX = $x;
$srcImageY = $y;
}
else
{
$r = sqrt ($x * $x + $y * $y); // radius - absolute distance of the current point from image center
$curAngle = asin ($y / $r); // angle of the current point [rad]
if ($x < 0)
{
$curAngle = pi () - $curAngle;
}
$newAngle = $curAngle + $angle * pi () / 180; // new angle [rad]
// Calculate new point coordinates (after rotation) in coordinate system at image center
$newXRel = floor ($r * cos ($newAngle));
$newYRel = floor ($r * sin ($newAngle));
// Convert to image absolute coordinates
$srcImageX = $newXRel + $centerX;
$srcImageY = $centerY - $newYRel;
}
if ( ($srcImageX >= 0) && ($srcImageX < $width) && ($srcImageY >= 0) && ($srcImageY < $height) )
{
$pixelColor = imagecolorat($image, $srcImageX, $srcImageY); // get source pixel color
imagesetpixel($dstImage, $dstImageX, $dstImageY, $pixelColor); // write destination pixel
}
}
}
}
return $dstImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment