6

I am ideally after resizing and setting a maxWidth and maxHeight on all my image uploads to mediawiki.

Having looked through the documentation of various different pages in mediawiki I am unable to find anything that says you can edit in anyway images uploaded to a site built on mediaWiki

I have no problem in writing some custom PHP but truth is I have no idea where to start looking in mediaWiki.

My thoughts are: imagemagick similarly to this:

I think you need the > flag on the resize:

convert -size 300x200 xc:red   small.png
convert -size 1000x500 xc:blue large.png
Now convert them both to 800x600 with no flags:

convert small.png -resize 800x600 a.png   # 800x533
convert large.png -resize 800x600 b.png   # 800x400
Now with flags:

convert small.png -resize 800x600\> a.png # 300x200
convert large.png -resize 800x600\> b.png # 800x400

But again, I cannot see where you would run this after an image upload to change the files dimensions.

Any help would be fantastic.

Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
  • 1
    This should help... https://stackoverflow.com/a/40008440/2836621 – Mark Setchell Feb 12 '19 at 18:45
  • Thanks mark, any ideas how to add this into mediawiki code? – Jamie Hutber Feb 12 '19 at 19:06
  • I'm really sorry, I know zero about `mediawiki`. I see there are some **ImageMagick** type of things in the page you linked to, but I have no idea how/where you would add the commands I suggested. Sorry! – Mark Setchell Feb 12 '19 at 19:10
  • Apparently i am running convert in mediawiki... https://www.mediawiki.org/wiki/Manual:$wgImageMagickConvertCommand Now I just need to work out how to run a custom command and get the file name to convert that! – Jamie Hutber Feb 13 '19 at 00:44
  • 2
    Why do you want that? MediaWiki already resizes (when [correctly configured](https://www.mediawiki.org/wiki/Manual:Image_administration#Image_thumbnailing)) the original images for display (while keeping the originals). If you don't want people to upload big files, you can set [$wgMaxImageArea](https://www.mediawiki.org/wiki/Manual:$wgMaxImageArea) or [$wgMaxUploadSize](https://www.mediawiki.org/wiki/Manual:$wgMaxUploadSize) – Ángel Feb 17 '19 at 00:06
  • 1
    Thanks @Ángel. These are good examples. The reason is simple really. There are around 22,000 images that need to be uploaded and by multiple users. We don't believe that many users will actually upload if they then have to come out, resize and resave the image. So normally with programming I make it as easy as possible for the users :D – Jamie Hutber Feb 17 '19 at 18:17
  • OK, and I assume some of the images are so big that you can't afford to store the originals at full size (like MediaWiki does by default) and just make resized versions to show on pages as needed, right? – Ilmari Karonen Feb 22 '19 at 06:41
  • 1
    Correct @IlmariKaronen. Though the need for smaller images is to stop downloads of said images and then being printed and sold. – Jamie Hutber Feb 22 '19 at 13:16
  • Hmm, that suggests an alternative solution: make the original full-size images inaccessible over the web and prevent MediaWiki from serving rescaled versions above a certain size. Would that be acceptable? – Ilmari Karonen Feb 22 '19 at 13:35
  • That certainly would be an option for sure. ultimately this is the most important thing, though I had even considered adding watermarks to images, but yes. I think your second solution works just as well. It automatically is creating images of around 1000px I believe? – Jamie Hutber Feb 22 '19 at 14:46
  • I am not sure about `mediawiki` but You can surely use custom PHP `imagemagick` library for this purpose which can help you to resize of image. But instead of resizing them in a fix length height, you should resize those in uploaded ratio else it will stretch you image. – Rohit Mittal Feb 23 '19 at 04:27
  • A radio would of course the choice :) Thank you It was would be a maximum on either height or width. So you are thinking of a Cron job to automatically resize all images each day or similar? – Jamie Hutber Feb 23 '19 at 11:26
  • 1
    Yes, I can help you to create script in PHP to convert an image size while maintaining its quality. To resize all your images, we can do that by cron job for existing and can implement in your `MediaWiki` to convert new uploads. – Rohit Mittal Feb 23 '19 at 16:58
  • Amazing, lets try and get this done before the Bounty runs out – Jamie Hutber Feb 23 '19 at 21:26

1 Answers1

0

You can try with an extension properly hooked : https://www.mediawiki.org/wiki/Manual:Hooks/UploadForm:BeforeProcessing or https://www.mediawiki.org/wiki/Manual:Hooks/UploadVerifyFile

Edit :

this example to put at the end of LocalSettings.php add a logo in the upper right corner of uploaded files :

$wgHooks['UploadForm:BeforeProcessing'][]=function(&$upload) { 
    $fictmp = $upload->mUpload->getTempPath();
    $newtmp = tempnam("/tmp", "tmp");
    $mylogo = "/path/to/my/logo/220px-SNice.svg.png";
    exec("composite -gravity NorthEast $mylogo $fictmp $newtmp" );
    copy($newtmp, $fictmp);
    unlink($newtmp);
    return true;
};
Community
  • 1
  • 1
Tuckbros
  • 417
  • 3
  • 13
  • Thanks Tuckbros. Could you please provide an example of usage? Find that a little bit confusing. – Jamie Hutber Feb 27 '19 at 12:21
  • I am not familiar with it but for inspiration : https://www.mediawiki.org/wiki/Extension:UploadedFileHasher (using UploadComplete hook) – Tuckbros Feb 27 '19 at 21:55
  • Adding this to the LocalSettings.php puts in a file the name of the tmp file being uploaded. I guess you can deal with it to perform your operations. $wgHooks['UploadForm:BeforeProcessing'][]=function(&$upload) { $fictmp = $upload->mUpload->getTempPath(); file_put_contents("/tmp/UploadForm",$fictmp . "\n"); return true; }; – Tuckbros Feb 28 '19 at 07:51