| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image Shortcode |
| 4 |
* |
| 5 |
* Displays map with [leaflet-image src="path/to/image.jpg"] |
| 6 |
* |
| 7 |
* JavaScript equivalent : L.imageOverlay('path/to/image.jpg'); |
| 8 |
* |
| 9 |
* @category Shortcode |
| 10 |
* @author Benjamin J DeLong <ben@bozdoz.com> |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly |
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.map-shortcode.php'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Leaflet Image Shortcode Class |
| 22 |
*/ |
| 23 |
class Leaflet_Image_Shortcode extends Leaflet_Map_Shortcode |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Get HTML for shortcode |
| 27 |
* |
| 28 |
* @param string $atts or Array |
| 29 |
* @param string $content produced by adding atts to JavaScript |
| 30 |
* |
| 31 |
* @return string HTML script |
| 32 |
*/ |
| 33 |
protected function getHTML($atts='', $content=null) |
| 34 |
{ |
| 35 |
extract($this->getAtts($atts)); |
| 36 |
|
| 37 |
/* only required field for image map (src/source) */ |
| 38 |
$src = empty($src) ? '' : $src; |
| 39 |
$source = empty($source) ? 'https://picsum.photos/1000/1000/' : $source; |
| 40 |
$source = empty($src) ? $source : $src; |
| 41 |
|
| 42 |
ob_start(); ?>/*<script>*/ |
| 43 |
var options = L.Util.extend({}, { |
| 44 |
attributionControl: false |
| 45 |
}, <?php echo $map_options; ?>, { |
| 46 |
crs: L.CRS.Simple |
| 47 |
}); |
| 48 |
var image_src = '<?php echo htmlspecialchars($source, ENT_QUOTES); ?>'; |
| 49 |
var img = new Image(); |
| 50 |
var zoom = <?php echo $zoom; ?>; |
| 51 |
var map = window.WPLeafletMapPlugin.createImageMap(options).setView([0, 0], zoom); |
| 52 |
img.onload = function() { |
| 53 |
var h = img.height, |
| 54 |
w = img.width, |
| 55 |
projected_zoom = zoom + 1, |
| 56 |
southWest = map.unproject([-w, h], projected_zoom), |
| 57 |
northEast = map.unproject([w, -h], projected_zoom), |
| 58 |
bounds = new L.LatLngBounds(southWest, northEast); |
| 59 |
L.imageOverlay( image_src, bounds ).addTo( map ); |
| 60 |
map.setMaxBounds(bounds); |
| 61 |
}; |
| 62 |
img.src = image_src;<?php |
| 63 |
|
| 64 |
$script = ob_get_clean(); |
| 65 |
|
| 66 |
return $this->getDiv($height, $width) . $this->wrap_script($script, 'WPLeafletImageShortcode'); |
| 67 |
} |
| 68 |
} |
| 69 |
|