gonzz Posted June 25, 2018 Posted June 25, 2018 I'm doing an artist site and they want to be able to upload a large .tiff to their portfolio. I need to keep to a copy of the original file in the server for the art distributers, and of course serve jpg in the web front end. I had the idea of extending the file or image upload input so that if the image is tiff, keep it, and also convert it to jpg, so I could access through something like $image->originalUrl or $image->size(100, 100)->url I was wondering if you thought this a good idea, or if i'm over-engineering something that could be done simply. Also, I've never extended an input, so any examples are welcome. Thanks!
elabx Posted June 25, 2018 Posted June 25, 2018 For starters it appear that you would need ImageMagick installed on your server to make the format conversion. If time is an issue, I'd use two fields paired up in repeaters, or pages themselves, so the user can upload the pairing of jpeg and the tiff file. Though I understand making the user convert/upload the image is a pain point ? You could add a method to the Pagefile class through a hook like this: https://github.com/fbg13/FieldtypeFileS3/blob/master/InputfieldFileS3.module#L21
gonzz Posted June 26, 2018 Author Posted June 26, 2018 thanks @elabx for the reply. Time is not an issue, and this is a core feature for the website. It's also key that this is easy to use, without the need to previously convert the file to png. Client is not computer savvy. Thanks for pointing me to the s3 module. I should create the png with imagemagick, inside the ___fileAdded function (line 63) right? ..Can i create an image object on the fly? Or should I set an image field (maybe hidden) and set that in the ___fileAdded function? Please let me know if i'm not clear ? thanks
Jonathan Lahijani Posted June 26, 2018 Posted June 26, 2018 @gonzz I think the Pageimage Assistant module might help here. At least it did for me when I wanted to convert PNG to JPG, which was helpful in my use-case for a particular site because I wanted to keep the lossless PNGs, but serve JPGs. Not sure what the support for TIFF is. https://modules.processwire.com/modules/pageimage-assistant/
Tom. Posted June 26, 2018 Posted June 26, 2018 Converting a high-res tiff into a jpg will likely bring your server to an halt or crash it. If you did still want to go down that route - you can do this using ImageMagick, hook on the field and convert it at that point. But I would strongly encourage that you have a repeater with two fields, one for the JPG image and the other for the TIFF and do an offline conversion. You could get your client to download a tool such as - http://www.easy2convert.com/tiff2jpg/ and let them convert it offline before upload.
jacmaes Posted June 26, 2018 Posted June 26, 2018 I think the Cloudinary image conversion service (they have a generous free tier) could take care of the conversion. They support TIFF. You can use their Fetch method to remotely transform your TIFFs. 1
LostKobrakai Posted June 26, 2018 Posted June 26, 2018 I'd suggest using a file field for the tiff and on upload trigger the conversion of the image to a jpg, which is then stored in a image field. For the actual conversion I'd look outside of processwire, so you can scale independently or even more the conversion to a beefier node if needed.
gonzz Posted June 27, 2018 Author Posted June 27, 2018 Thanks everyone for your responses, got me on track. I ended up making a module that hooks after saveReady. After some validation it does the following. Hope it helps anyone. This could be transformed into a more advanced module with options. Server needs Image Magick module installed. // Lets generate a png image from this tiff // imagen_tiff is a file field $tiffImgPath = $page->imagen_tiff->first()->filename; $tiffImgName = preg_replace('/\\.[^.\\s]{3,4}$/', '', $page->imagen_tiff->first()->name); // Create an Imagick from the TIFF $maxWidth = 1024; $maxHeight = 768; $img = new Imagick($tiffImgPath); // Size calc $d = $img->getImageGeometry(); $w = min($d['width'], $maxWidth); $h = min($d['height'], $maxHeight); // Convert image to PNG $img->setImageFormat('png'); $img->setCompressionQuality(97); $img->setImageResolution(72, 72); $img->resizeImage($w,$h, Imagick::FILTER_LANCZOS, 1, true); // Save the converted image to a cache folder $tempDir = wire()->files->tempDir('tiffpng')->get() . $tiffImgName .'.png'; $img->writeImage($tempDir); // I save the cached image in the image field imagen_web $page->imagen_web->add($tempDir); 1
adrian Posted June 27, 2018 Posted June 27, 2018 Obviously I don't have your complete code to check, but if you are doing on SaveReady, make sure it doesn't happen on every save - surely you only want it if the tiff has just been uploaded or replaced? Sorry if you have already taken care of this - just wanted to raise it in case. I would probably go with hooking into InputfieldFile::fileAdded 1
gonzz Posted June 27, 2018 Author Posted June 27, 2018 Hi @adrian thanks for the heads up. I do have a couple of checks so it's not fired on every save, but i'm not handling the situation where the TIFF is replaced ? My first attempt was using InputfieldFile::fileAdded but I wasn't sure how to get the uploaded file data and the page instance. Tried I couple of things googling around but nothing worked for me. Any working example to point me?
adrian Posted June 27, 2018 Posted June 27, 2018 3 hours ago, gonzz said: Any working example to point me? These are two modules that use it: https://github.com/adrianbj/CustomUploadNames/blob/master/ProcessCustomUploadNames.modulehttps://github.com/matjazpotocnik/AutoSmush/blob/master/AutoSmush.module Let me know if you can't figure out what you need from those and I can try to answer more specific questions. 2 1
gonzz Posted June 28, 2018 Author Posted June 28, 2018 Thanks @adrian for the links. I have carefully read ProcessCustomUploadNames.module and put this together. Here's a gist with the module I wrote: https://gist.github.com/gonzam88/2b0e8f850c469b48f4ea50fec29a4b1f It's working for me but can't guarantee anything. Anyone is free to grab this code and continue developing into a full featured module. Thanks for everyone's input 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now