Advertisement
acclivity

ImageMagick

Apr 29th, 2024
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.65 KB | None | 0 0
  1. <?php
  2.  
  3. // This PHP code utilizes ImageMagick to perform image manipulation tasks similar to your Python code.
  4. // Note that you'll need to have the Imagick extension installed and enabled in your PHP environment to run this code.
  5. // Additionally, make sure to adjust the file paths and any other specifics according to your setup.
  6.  
  7.  
  8. function map_creator($data) {
  9.     $img = new Imagick('map_image.jpg');
  10.     $draw = new ImagickDraw();
  11.     $draw->setStrokeWidth(1);
  12.    
  13.     $data = str_replace("\"", "", $data);
  14.     $data = str_replace("\'", "", $data);
  15.    
  16.     function compute_XY_from_GR($gr_east, $gr_north) {
  17.         $base_es = 50481;
  18.         $base_ns = 98019;
  19.         $base_x = 644;
  20.         $base_y = 534;
  21.        
  22.         $x = $base_x + (($gr_east - $base_es) * 0.3555);
  23.         $y = $base_y - (($gr_north - $base_ns) * 0.355);
  24.        
  25.         $x_corr = ($y - $base_y) * 0.04;
  26.         $x -= $x_corr;
  27.        
  28.         $y_corr = ($x - $base_x) * 0.025;
  29.         $y += $y_corr;
  30.        
  31.         return array($x, $y);
  32.     }
  33.    
  34.     function draw_cross($draw, $point, $pc, $age) {
  35.         if ($pc > 20) {
  36.             $sizer = 12;
  37.         } elseif ($pc > 10) {
  38.             $sizer = 9;
  39.         } else {
  40.             $sizer = 6;
  41.         }
  42.        
  43.         $colors = array("rgb(0,0,255)", "rgb(255,0,255)", "rgb(255,0,0)", "rgb(255,165,0)", "rgb(255,255,0)");
  44.         $idx = 0;
  45.         if ($age < 2) {
  46.             $idx = 4;
  47.         } elseif ($age < 5) {
  48.             $idx = 3;
  49.         } elseif ($age < 10) {
  50.             $idx = 2;
  51.         } elseif ($age < 20) {
  52.             $idx = 1;
  53.         }
  54.        
  55.         $color = $colors[$idx];
  56.        
  57.         $draw->setStrokeColor($color);
  58.         $draw->line($point[0] - $sizer, $point[1], $point[0] + $sizer, $point[1]);
  59.         $draw->line($point[0], $point[1] - $sizer, $point[0], $point[1] + $sizer);
  60.     }
  61.    
  62.     $now = time();
  63.     $days = $now / 86400;
  64.     $year_now = 1970 + intval($days / 365.25);
  65.    
  66.     $fsplit = explode("*", $data);
  67.     $group = trim(array_shift($fsplit));
  68.    
  69.     $taxon = ucfirst(trim(array_shift($fsplit)));
  70.     $vernac = ucfirst(trim(array_shift($fsplit)));   // Vernacular (Common) name
  71.     $total = intval(trim(array_shift($fsplit)));    // Total number of sightings of this species
  72.    
  73.     $sp_refs = [];
  74.     $griddata = $fsplit;                    // e.g. TV514989-1-2001/TV513989-7-1998/TV514990-1-2016/
  75.    
  76.     foreach ($griddata as $triple) {
  77.         if (!$triple) {
  78.             break;
  79.         }
  80.         $psplit = explode('~', $triple);
  81.         $gr = $psplit[0];                              // get grid ref
  82.         $gr = substr($gr, 0, 5) . "50" . substr($gr, 5) . "50"; // Adjust grid rwf to centre of 100m square
  83.         $ctr = $psplit[1];
  84.         $year = $psplit[2];
  85.         $sp_refs[] = array($gr, $ctr, $year);
  86.     }
  87.    
  88.     $more = $vernac;
  89.     if (strtolower($vernac) === strtolower($taxon)) {  // if common name is same
  90.         $more = $group;
  91.     }
  92.    
  93.     $more = ucfirst($more);
  94.    
  95.     $draw->setFontSize(12);
  96.     $draw->setFillColor("rgb(255,255,255)");
  97.     $draw->setFont('Arial');
  98.     $img->annotateImage($draw, 675, 40, 0, $taxon);
  99.     $img->annotateImage($draw, 675, 80, 0, $more);
  100.     $img->annotateImage($draw, 675, 120, 0, "$total sightings");
  101.     $img->annotateImage($draw, 1400, 240, 0, "< 2 years");
  102.     $img->annotateImage($draw, 1400, 270, 0, "< 5 years");
  103.     $img->annotateImage($draw, 1400, 300, 0, "< 10 years");
  104.     $img->annotateImage($draw, 1400, 330, 0, "< 20 years");
  105.     $img->annotateImage($draw, 1400, 360, 0, "20 years+");
  106.    
  107.     foreach ($sp_refs as $item) {
  108.         $gr = $item[0];                    // Grid Ref
  109.         $ctr = $item[1];                   // Qty seen at this Grid Ref
  110.         $year = $item[2];                  // Year of most recent sighting at this Grid Ref
  111.         $eastings = intval(substr($gr, 2, 5));         // split numeric part of Grid Ref into Eastings and Northings
  112.         $northings = intval(substr($gr, 7));
  113.        
  114.         $ctr = intval($ctr);                                  // Qty seen at this location
  115.         $pc = ($ctr * 100) / $total;                          // Percent qty seen at this location compared to all locations
  116.        
  117.         list($x, $y) = compute_XY_from_GR($eastings, $northings);      // Convert Grid Ref to pixel positions X, Y
  118.        
  119.         $aged = $year_now - intval($year);                     // Age of most recent sighting at this Grid Ref
  120.        
  121.         draw_cross($draw, array($x, $y), $pc, $aged );              // Draw a cross on the map at centre of this grid ref.
  122.     }
  123.    
  124.     $img->drawImage($draw);
  125.     return $img;
  126. }
  127. ?>
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement