Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // This PHP code utilizes ImageMagick to perform image manipulation tasks similar to your Python code.
- // Note that you'll need to have the Imagick extension installed and enabled in your PHP environment to run this code.
- // Additionally, make sure to adjust the file paths and any other specifics according to your setup.
- function map_creator($data) {
- $img = new Imagick('map_image.jpg');
- $draw = new ImagickDraw();
- $draw->setStrokeWidth(1);
- $data = str_replace("\"", "", $data);
- $data = str_replace("\'", "", $data);
- function compute_XY_from_GR($gr_east, $gr_north) {
- $base_es = 50481;
- $base_ns = 98019;
- $base_x = 644;
- $base_y = 534;
- $x = $base_x + (($gr_east - $base_es) * 0.3555);
- $y = $base_y - (($gr_north - $base_ns) * 0.355);
- $x_corr = ($y - $base_y) * 0.04;
- $x -= $x_corr;
- $y_corr = ($x - $base_x) * 0.025;
- $y += $y_corr;
- return array($x, $y);
- }
- function draw_cross($draw, $point, $pc, $age) {
- if ($pc > 20) {
- $sizer = 12;
- } elseif ($pc > 10) {
- $sizer = 9;
- } else {
- $sizer = 6;
- }
- $colors = array("rgb(0,0,255)", "rgb(255,0,255)", "rgb(255,0,0)", "rgb(255,165,0)", "rgb(255,255,0)");
- $idx = 0;
- if ($age < 2) {
- $idx = 4;
- } elseif ($age < 5) {
- $idx = 3;
- } elseif ($age < 10) {
- $idx = 2;
- } elseif ($age < 20) {
- $idx = 1;
- }
- $color = $colors[$idx];
- $draw->setStrokeColor($color);
- $draw->line($point[0] - $sizer, $point[1], $point[0] + $sizer, $point[1]);
- $draw->line($point[0], $point[1] - $sizer, $point[0], $point[1] + $sizer);
- }
- $now = time();
- $days = $now / 86400;
- $year_now = 1970 + intval($days / 365.25);
- $fsplit = explode("*", $data);
- $group = trim(array_shift($fsplit));
- $taxon = ucfirst(trim(array_shift($fsplit)));
- $vernac = ucfirst(trim(array_shift($fsplit))); // Vernacular (Common) name
- $total = intval(trim(array_shift($fsplit))); // Total number of sightings of this species
- $sp_refs = [];
- $griddata = $fsplit; // e.g. TV514989-1-2001/TV513989-7-1998/TV514990-1-2016/
- foreach ($griddata as $triple) {
- if (!$triple) {
- break;
- }
- $psplit = explode('~', $triple);
- $gr = $psplit[0]; // get grid ref
- $gr = substr($gr, 0, 5) . "50" . substr($gr, 5) . "50"; // Adjust grid rwf to centre of 100m square
- $ctr = $psplit[1];
- $year = $psplit[2];
- $sp_refs[] = array($gr, $ctr, $year);
- }
- $more = $vernac;
- if (strtolower($vernac) === strtolower($taxon)) { // if common name is same
- $more = $group;
- }
- $more = ucfirst($more);
- $draw->setFontSize(12);
- $draw->setFillColor("rgb(255,255,255)");
- $draw->setFont('Arial');
- $img->annotateImage($draw, 675, 40, 0, $taxon);
- $img->annotateImage($draw, 675, 80, 0, $more);
- $img->annotateImage($draw, 675, 120, 0, "$total sightings");
- $img->annotateImage($draw, 1400, 240, 0, "< 2 years");
- $img->annotateImage($draw, 1400, 270, 0, "< 5 years");
- $img->annotateImage($draw, 1400, 300, 0, "< 10 years");
- $img->annotateImage($draw, 1400, 330, 0, "< 20 years");
- $img->annotateImage($draw, 1400, 360, 0, "20 years+");
- foreach ($sp_refs as $item) {
- $gr = $item[0]; // Grid Ref
- $ctr = $item[1]; // Qty seen at this Grid Ref
- $year = $item[2]; // Year of most recent sighting at this Grid Ref
- $eastings = intval(substr($gr, 2, 5)); // split numeric part of Grid Ref into Eastings and Northings
- $northings = intval(substr($gr, 7));
- $ctr = intval($ctr); // Qty seen at this location
- $pc = ($ctr * 100) / $total; // Percent qty seen at this location compared to all locations
- list($x, $y) = compute_XY_from_GR($eastings, $northings); // Convert Grid Ref to pixel positions X, Y
- $aged = $year_now - intval($year); // Age of most recent sighting at this Grid Ref
- draw_cross($draw, array($x, $y), $pc, $aged ); // Draw a cross on the map at centre of this grid ref.
- }
- $img->drawImage($draw);
- return $img;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement