general code question:
i just coded a snippet for generating a coverimage für the lingonberry template., based on a special watermark.
code:
<?php
// define default-image filename
$defaultimagefilename = 'traildefault.jpg';
// define gpx file object from panel-field
$gpxfile = $item->mapgpx()->toFile();
// check if gpx.map.png (overlay) file exists, if so, go on, if not, generate it via bash script
($gpxfile->root() . '.map.png') ?: NULL; // replace "NULL" with bash script
// check if mapcoverimage-field is set, if so, set it in variable $image as object, if not, set defaultimage in variable $image as object
($image = $item->mapcoverimage()->toFile()) ?: $image = page('defaultimages')->image($defaultimagefilename);
// check if image is watermarked - if not, check if watermarked version of $image exists in $pagefolder, if so, use it, if not, generate watermarked version and set this in variable $image as object
(substr($image->root(), -16) == '.watermarked.jpg') ?: ( ($page->image($image->filename() . '.watermarked.jpg')) ?: watermark($image, url($gpxfile->url() . ".map.png"), $page->root()) AND $image = $page->image($image->filename() . '.watermarked.jpg') );
function watermark($baseIMG, $overlayPNG, $outputPATH) {
$img = new abeautifulsite\SimpleImage($baseIMG->root());
$img = $img->overlay($overlayPNG, 'bottom right', 1, -10, -10);
$img->save($outputPATH . '/' . $baseIMG->filename() . '.watermarked.jpg');
}
// notes: eventuell dateinamen der watermarked dateien inkl. gpx datei. damit unterschiedliche maps pro coverimage gehen könnten
?>
<div class="featured-media">
<a href="<?= $item->url() ?>">
<img src="<?= $image->resize(766, null, 85)->url() ?>" rel="bookmark" title="<?= $item->title()->html() ?>"<?php e($image->alt()->isNotEmpty(), ' alt="' . $image->alt()->html() . '"') ?>>
<?php if($image->caption()->isNotEmpty()) : ?>
<div class="media-caption-container">
<p class="media-caption">
<?= $image->caption()->html() ?>
</p>
</div>
<?php endif ?>
</a>
</div>
Problem/question:
when i open page in browser the first time (and no corresponding watermarked image is insite page folder) the watermarked image will be generated (i checked at filesystem) but not shown, instead the un-watermarked image is shown.
When i reload page in browser, the watermarked version is shown.
i don’t understand why this happens. as far as i know, php is single-threaded/batched. so every command should start AFTER the other. so, generating the watermarked image has to be finished, before the php script goes on. but the script does not wait for this to finish.
how does this happen?
how to solve?
thx in advance