__('Images', __FILE__), // Module Title 'summary' => __('One or more image uploads (sortable)', __FILE__), // Module Summary 'version' => 110, 'permanent' => true, ); } public function init() { parent::init(); $this->set('extensions', 'JPG JPEG GIF PNG'); $this->set('adminThumbs', false); $this->set('maxWidth', ''); $this->set('maxHeight', ''); $this->set('minWidth', ''); $this->set('minHeight', ''); $this->set('adminThumbHeight', 100); $this->set('itemClass', 'InputfieldFileItem InputfieldImage ui-widget'); $this->set('defaultGrid', 0); //$this->modules->get("InputfieldFile"); // force the JS and CSS from InputfieldFile to also be loaded } public function ___render() { $this->modules->getInstall("JqueryMagnific"); $this->config->scripts->add($this->config->urls->InputfieldFile . "InputfieldFile.js"); $this->config->styles->add($this->config->urls->InputfieldFile . "InputfieldFile.css"); $out = parent::___render(); if($this->defaultGrid) $out .= ""; return $out; } public function ___renderValue() { $out = ''; $value = $this->attr('value'); if(empty($value)) return ''; foreach($value as $pageimage) { $thumb = $pageimage; if($this->adminThumbs && $thumb->height > $this->adminThumbHeight) { // create a variation for display with this inputfield $thumb = $thumb->height($this->adminThumbHeight); } $alt = $this->wire('sanitizer')->entities($thumb->description); $img = "$alt"; if($thumb !== $pageimage) $img = "$img"; $out .= $img; } return $out; } /** * Resize images to max width/height if specified in field config and image is larger than max * */ protected function ___fileAdded(Pagefile $pagefile) { $filename = $pagefile->filename; $pagefile2 = null; if(!$pagefile->width) { $pagefile->unlink(); throw new WireException($this->_('Invalid image')); } if(($this->minWidth && $pagefile->width < $this->minWidth) || ($this->minHeight && $pagefile->height < $this->minHeight)) { $actualDimensions = $pagefile->width . 'x' . $pagefile->height; $requiredDimensions = $this->minWidth . 'x' . $this->minHeight; throw new WireException(sprintf($this->_('Image of %s does not meet minimum size requirements'), $actualDimensions) . " ($requiredDimensions)"); } if(($this->maxWidth && $pagefile->width > $this->maxWidth) || ($this->maxHeight && $pagefile->height > $this->maxHeight)) { $pagefile2 = $pagefile->size($this->maxWidth, $this->maxHeight, array('cropping' => false)); if($pagefile->filename != $pagefile2->filename) { unlink($pagefile->filename); rename($pagefile2->filename, $pagefile->filename); } $pagefile->getImageInfo(true); // force it to reload it's dimensions } if($pagefile2) { $this->message($this->_("Image resized to fit maximum allowed dimensions") . " ({$this->maxWidth}x{$this->maxHeight}"); } return parent::___fileAdded($pagefile); } protected function ___renderItem($pagefile, $id, $n) { $thumb = $pagefile; $error = ''; $imgAttr = ''; if($this->adminThumbs) { $thumbHeight = $thumb->height; if($thumbHeight > $this->adminThumbHeight) { // create a variation for display with this inputfield $thumb = $thumb->height($this->adminThumbHeight); if($thumb->error) $error = "$thumb->error"; $thumbHeight = $this->adminThumbHeight; } $imgAttr = " height='$thumbHeight'"; } $thumbUrl = $thumb->url; // ensure cached image doesn't get shown when replacing same filename if($this->overwrite) $thumbUrl .= "?m=" . filemtime($pagefile->pathname); $out = "\n\t\t

" . "\n\t\t\t" . "\n\t\t\t" . "\n\t\t\t{$pagefile->basename} " . "\n\t\t\t• {$pagefile->filesizeStr} • {$pagefile->width}x{$pagefile->height} " . "\n\t\t\t" . "\n\t\t\t " . "\n\t\t\t " . "\n\t\t

" . "\n\t\t
" . "\n\t\t\t{$pagefile->basename}" . "\n\t\t\t" . $error . $this->renderItemDescriptionField($pagefile, $id, $n) . "\n\t\t\t" . "\n\t\t
"; return $out; } public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'adminThumbs'); $field->attr('value', 1); $field->attr('checked', $this->adminThumbs ? 'checked' : ''); $field->label = $this->_('Display thumbnails in page editor?'); $field->description = $this->_('Thumbnails take up less space and make it easier to sort multiple images. If unchecked, the full (original) size image will be shown in the page editor.'); // Display thumbnails description $inputfields->add($field); $field = $this->modules->get("InputfieldRadios"); $field->attr('name', 'defaultGrid'); $field->label = $this->_('Default View'); $field->addOption(0, $this->_('Rows')); $field->addOption(1, $this->_('Grid')); $field->showIf = 'maxFiles!=1'; $field->optionColumns = 1; $field->description = $this->_('Either view can be toggled by clicking the grid/row icon in the image field. This just defines which one is the default. The rows view is the recommended default.'); $field->attr('value', (int) $this->defaultGrid); $inputfields->add($field); $fieldset = $this->modules->get('InputfieldFieldset'); $fieldset->label = $this->_("Max Image Dimensions"); $fieldset->collapsed = $this->maxWidth || $this->maxHeight ? Inputfield::collapsedNo : Inputfield::collapsedYes; $fieldset->description = $this->_("Optionally enter the max width and/or height of uploaded images. If specified, images will be resized at upload time when they exceed either the max width or height. The resize is performed at upload time, and thus does not affect any images in the system, or images added via the API."); // Max image dimensions description $description = $this->_("Enter the value in number of pixels or leave blank for no limit."); // Min/Max width/height description $field = $this->modules->get("InputfieldInteger"); $field->attr('name', 'maxWidth'); $field->attr('value', $this->maxWidth ? (int) $this->maxWidth : ''); $field->label = $this->_("Max width for uploaded images"); $field->description = $description; $field->columnWidth = 50; $fieldset->add($field); $field = $this->modules->get("InputfieldInteger"); $field->attr('name', 'maxHeight'); $field->attr('value', $this->maxHeight ? (int) $this->maxHeight : ''); $field->label = $this->_("Max height for uploaded images"); $field->description = $description; $field->columnWidth = 50; $fieldset->add($field); $inputfields->add($fieldset); // min image dimensions $fieldset = $this->modules->get('InputfieldFieldset'); $fieldset->label = $this->_("Min Image Dimensions"); $fieldset->collapsed = $this->minWidth || $this->minHeight ? Inputfield::collapsedNo : Inputfield::collapsedYes; $fieldset->description = $this->_("Optionally enter the minimum width and/or height of uploaded images. If specified, images that don't meet these minimums will be refused."); // Max image dimensions description $field = $this->modules->get("InputfieldInteger"); $field->attr('name', 'minWidth'); $field->attr('value', $this->minWidth ? (int) $this->minWidth : ''); $field->label = $this->_("Min width for uploaded images"); $field->description = $description; $field->columnWidth = 50; $fieldset->add($field); $field = $this->modules->get("InputfieldInteger"); $field->attr('name', 'minHeight'); $field->attr('value', $this->minHeight ? (int) $this->minHeight : ''); $field->label = $this->_("Min height for uploaded images"); $field->description = $description; $field->columnWidth = 50; $fieldset->add($field); $inputfields->add($fieldset); return $inputfields; } }