Skip to content

Instantly share code, notes, and snippets.

@gonzam88
Last active June 28, 2018 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzam88/2b0e8f850c469b48f4ea50fec29a4b1f to your computer and use it in GitHub Desktop.
Save gonzam88/2b0e8f850c469b48f4ea50fec29a4b1f to your computer and use it in GitHub Desktop.
Processwire module to allow tiff image upload
<?php
// Processwire module to allow tiff image upload
//
// You must create a 'imagen_tiff' file upload field, and another 'imagen_web' image field.
// This module hooks on InputfieldFile::fileAdded and creates a temporary .png image through Imagick
// It then adds this temp .png to the 'imagen_web' field
class AjaxTiffToPng extends WireData implements Module {
/**
* Basic information about module
*/
public static function getModuleInfo() {
return array(
'title' => 'Ajax Tiff a PNG',
'summary' => 'Converts tiff image from imagen_tiff field to png and saves in the imagen_web field',
'href' => '',
'version' => 001,
'permanent' => false,
'autoload' => true,
'singular' => true,
);
}
public function ready(){
$this->addHookBefore('InputfieldFile::fileAdded', $this, 'AjaxConvertTiff', array('priority'=>10));
}
function AjaxConvertTiff(HookEvent $event){
$field = $event->object;
if($field->name != "imagen_tiff") return; // only for this specific field
$pageid = null;
$process = $this->wire('process');
$pagefile = $event->argumentsByName("pagefile");
if($pagefile) {
$action = 'upload';
$pageid = $pagefile->pagefiles->getPage()->id;
$field = $this->wire('fields')->get($field->name);
$fieldid = $field->id;
}else {
$action = 'save';
$pageid = $event->arguments(0)->id;
}
if(!$pageid) return; //avoids interactions with other modules
if(method_exists($this->wire('pages')->get($pageid), 'getForPage')) {
$editedPage = $this->wire('pages')->get($pageid)->getForPage();
}
elseif($this->wire('input')->get->context == 'PageTable') {
$editedPage = $this->wire('pages')->get("FieldtypePageTable=".$process->getPage().", include=all");
}
else {
$editedPage = $this->wire('pages')->get($pageid);
}
// Tiff image conversion
$tiffImgPath = $pagefile->filename();
$tiffImgName = $pagefile->basename(false);
// Save the temp PNG
$maxWidth = 1024;
$maxHeight = 768;
$img = new Imagick($tiffImgPath);
// Size calculation
$d = $img->getImageGeometry();
$w = min($d['width'], $maxWidth);
$h = min($d['height'], $maxHeight);
// Image conversion
$img->setImageFormat('png');
$img->setCompressionQuality(97);
$img->setImageResolution(72, 72);
$img->resizeImage($w,$h, Imagick::FILTER_LANCZOS, 1, true);
// Save image temp
$tempDir = wire()->files->tempDir('tiffpng')->get() . $tiffImgName .'.png';
$img->writeImage($tempDir);
// Delete existing images
$editedPage->of(false);
$editedPage->imagen_web->deleteAll();
// Save new image in imagen_web field
$editedPage->imagen_web->add($tempDir);
$editedPage->save("imagen_web");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment