librenms-plugins / Weathermap

MIT License
64 stars 51 forks source link

php8 deprecationwarnings #89

Open wdoekes opened 1 year ago

wdoekes commented 1 year ago

Here's a bunch of changes I did to get rid of various warnings.

Not everything is fixed: strftime will need something better. And the $num_points is also a thing...

I also needed fix to #88.

diff --git a/lib/WeatherMap.functions.php b/lib/WeatherMap.functions.php
index 3c62b13..1967ca5 100644
--- a/lib/WeatherMap.functions.php
+++ b/lib/WeatherMap.functions.php
@@ -269,7 +269,7 @@ function myimagecolorallocate($image, $red, $green, $blue)
    // it's possible that we're being called early - just return straight away, in that case
    if(!isset($image)) return(-1);

-   $existing=imagecolorexact($image, $red, $green, $blue);
+   $existing=imagecolorexact($image, round($red), round($green), round($blue));

    if ($existing > -1)
        return $existing;
@@ -1875,7 +1875,7 @@ function wimagefilledrectangle( $image ,$x1, $y1, $x2, $y2, $color )
    $r = $r/255; $g=$g/255; $b=$b/255; $a=(127-$a)/127;

    metadump("FRECT $x1 $y1 $x2 $y2 $r $g $b $a");
-   return(imagefilledrectangle( $image ,$x1, $y1, $x2, $y2, $color ));
+   return(imagefilledrectangle( $image, round($x1), round($y1), round($x2), round($y2), $color ));
 }

 function wimagerectangle( $image ,$x1, $y1, $x2, $y2, $color )
@@ -1887,7 +1887,7 @@ function wimagerectangle( $image ,$x1, $y1, $x2, $y2, $color )
    $r = $r/255; $g=$g/255; $b=$b/255; $a=(127-$a)/127;

    metadump("RECT $x1 $y1 $x2 $y2 $r $g $b $a");
-   return(imagerectangle( $image ,$x1, $y1, $x2, $y2, $color ));
+   return(imagerectangle( $image, round($x1), round($y1), round($x2), round($y2), $color ));
 }

 function wimagepolygon($image, $points, $num_points, $color)
@@ -1907,7 +1907,13 @@ function wimagepolygon($image, $points, $num_points, $color)

    metadump("POLY $num_points ".$pts." $r $g $b $a");

-   return(imagepolygon($image, $points, $num_points, $color));
+   // Removing $num_points creates weird boxes. This needs a better fix.
+   // Likely count($points) is sometimes not equal to $num_points*2
+   //assert(count($points) == ($num_points*2));
+   if (count($points) != ($num_points*2)) {
+       return @imagepolygon($image, $points, $num_points, $color);
+   }
+   return imagepolygon($image, $points, $color);
 }

 function wimagefilledpolygon($image, $points, $num_points, $color)
@@ -1927,7 +1933,13 @@ function wimagefilledpolygon($image, $points, $num_points, $color)

    metadump("FPOLY $num_points ".$pts." $r $g $b $a");

-   return(imagefilledpolygon($image, $points, $num_points, $color));
+   // Removing $num_points creates weird boxes. This needs a better fix.
+   // Likely count($points) is sometimes not equal to $num_points*2
+   //assert(count($points) == ($num_points*2));
+   if (count($points) != ($num_points*2)) {
+       return @imagefilledpolygon($image, $points, $num_points, $color);
+   }
+   return imagefilledpolygon($image, $points, $color);
 }

 function wimagecreatetruecolor($width, $height)
diff --git a/lib/WeatherMapNode.class.php b/lib/WeatherMapNode.class.php
index 55fa40d..81fb15e 100644
--- a/lib/WeatherMapNode.class.php
+++ b/lib/WeatherMapNode.class.php
@@ -615,7 +615,7 @@ class WeatherMapNode extends WeatherMapItem
        // Draw the icon, if any
        if(isset($icon_im))
        {
-           imagecopy($node_im, $icon_im, $icon_x1, $icon_y1, 0, 0, imagesx($icon_im), imagesy($icon_im));
+           imagecopy($node_im, $icon_im, round($icon_x1), round($icon_y1), 0, 0, imagesx($icon_im), imagesy($icon_im));
            imagedestroy($icon_im);
        }

@@ -632,7 +632,7 @@ class WeatherMapNode extends WeatherMapItem
            // if there's an icon, then you can choose to have no background
            if(! $col->is_none() )
            {
-               imagefilledrectangle($node_im, $label_x1, $label_y1, $label_x2, $label_y2, $col->gdallocate($node_im));
+               imagefilledrectangle($node_im, round($label_x1), round($label_y1), round($label_x2), round($label_y2), $col->gdallocate($node_im));
            }

            if ($this->selected)
@@ -646,7 +646,7 @@ class WeatherMapNode extends WeatherMapItem
                $olcol = new Colour($this->labeloutlinecolour);
                if ($olcol->is_real())
                {
-                   imagerectangle($node_im, $label_x1, $label_y1, $label_x2, $label_y2, $olcol->gdallocate($node_im));
+                   imagerectangle($node_im, round($label_x1), round($label_y1), round($label_x2), round($label_y2), $olcol->gdallocate($node_im));
                }
            }
            #}
@@ -716,7 +716,7 @@ class WeatherMapNode extends WeatherMapItem
        if(isset($this->image))
        {
            imagealphablending($im, true);
-           imagecopy ( $im, $this->image, $this->x - $this->centre_x, $this->y - $this->centre_y, 0, 0, imagesx($this->image), imagesy($this->image) );
+           imagecopy ( $im, $this->image, round($this->x - $this->centre_x), round($this->y - $this->centre_y), 0, 0, imagesx($this->image), imagesy($this->image) );
        }

    }
diff --git a/lib/Weathermap.class.php b/lib/Weathermap.class.php
index f575fd2..4c17948 100644
--- a/lib/Weathermap.class.php
+++ b/lib/Weathermap.class.php
@@ -503,7 +503,7 @@ class WeatherMap extends WeatherMapBase
         }

         if (($fontnumber > 0) && ($fontnumber < 6)) {
-            imagestring($image, $fontnumber, $x, $y - imagefontheight($fontnumber), $string, $colour);
+            imagestring($image, $fontnumber, round($x), round($y - imagefontheight($fontnumber)), $string, $colour);
             if ($angle != 0) {
                 wm_warn("Angled text doesn't work with non-FreeType fonts [WMWARN02]\n");
             }
@@ -1609,7 +1609,7 @@ class WeatherMap extends WeatherMapBase
                 $boxy += $this->height;
             }

-            $scale_im = imagecreatetruecolor($boxwidth + 1, $boxheight + 1);
+            $scale_im = imagecreatetruecolor(round($boxwidth + 1), round($boxheight + 1));
             $scale_ref = 'gdref_legend_' . $scalename;

             // Start with a transparent box, in case the fill or outline colour is 'none'
@@ -1717,12 +1717,12 @@ class WeatherMap extends WeatherMapBase

         switch ($which) {
             case "MIN":
-                $stamp = strftime($this->minstamptext, $this->min_data_time);
+                $stamp = @strftime($this->minstamptext, $this->min_data_time);
                 $pos_x = $this->mintimex;
                 $pos_y = $this->mintimey;
                 break;
             case "MAX":
-                $stamp = strftime($this->maxstamptext, $this->max_data_time);
+                $stamp = @strftime($this->maxstamptext, $this->max_data_time);
                 $pos_x = $this->maxtimex;
                 $pos_y = $this->maxtimey;
                 break;
@@ -3284,7 +3284,7 @@ class WeatherMap extends WeatherMapBase
         } else {
             $maptime = time();
         }
-        $this->datestamp = strftime($this->stamptext, $maptime);
+        $this->datestamp = @strftime($this->stamptext, $maptime);

         // do the basic prep work
         if ($this->background != '') {
vhuk commented 7 months ago

I didn't notice this issue existed when I create PR#100 to fix these issues; it also has a solution for weird boxes that come when fixing imagefilledpolygon() calls.