Skip to content

Instantly share code, notes, and snippets.

@ogabrielguerra
Last active May 1, 2020 21:04
Show Gist options
  • Save ogabrielguerra/f9d8057577d98ee601fdb55a23e2fa04 to your computer and use it in GitHub Desktop.
Save ogabrielguerra/f9d8057577d98ee601fdb55a23e2fa04 to your computer and use it in GitHub Desktop.
Get images from a dir and return HTML accordingly with passed template.
<?php
class SiteImages{
public string $path = '';
public string $template = '';
public function __construct(string $path, string $template){
$this->setPath($path);
$this->setTemplate($template);
}
public function setPath(string $path) : void{
if(empty($this->path)){
if(is_dir($path)){
$this->path = $path;
}
}
}
public function setTemplate($template){
$this->template = $template;
}
public function getFiles() : array{
if(!empty($this->path)){
$files = scandir($this->path);
$items = array_diff($files, array('.','..','.DS_Store'));
return array_values($items);
}else{
return [];
}
}
public function getImageRowsHtml(int $numColumns, array $items, string $vSpace='') : string{
$output = '<div class="row">';
$numItems = count($items);
if($numItems>0){
for ($i=0; $i<$numItems; $i++) {
$image = $this->path.$items[$i];
if($i > 0 && $i % $numColumns == 0){
$output .= '</div><div class="row '.$vSpace.'">';
}
$output .= str_replace('#image#', $image, $this->template);
}
$output .= '</div>';
return $output;
}else{
return 'Empty items list provided. Cannot proceed.';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment