Skip to content

Instantly share code, notes, and snippets.

@jacmaes
Created May 23, 2019 15:18
Show Gist options
  • Save jacmaes/22f03d9c3bc4aa837993f4a3b574ad14 to your computer and use it in GitHub Desktop.
Save jacmaes/22f03d9c3bc4aa837993f4a3b574ad14 to your computer and use it in GitHub Desktop.
Save external image and rename it in Processwire #pw
<?php
// 1. We generate a static image from Mapbox's static Maps API.
// 2. We save it to an image field
// 3. We rename it to a more comprehensible filename, and then we add an extension (png by default for Mapbox).
// View investigacioncontemporanea.com -> "university" template for example of implementation.
if (!$page->image) {
$p = $page;
$p->of(false);
$p->image->add("https://api.mapbox.com/styles/v1/mapbox/dark-v10/static/pin-s-r+d29b31($long,$lat)/$long,$lat,$zoom,0,0/629x250?access_token=API_KEY_HERE");
$p->image->first->rename("{$p->title}.png");
$p->save();
} else {
echo "<img src='{$page->image->url}' alt='$page->title'>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment