r79867 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79866‎ | r79867 | r79868 >
Date:16:24, 8 January 2011
Author:btongminh
Status:ok
Tags:
Comment:
Follow-up r79845: Add rotation support to the upload preview using the <canvas> element. The actual rotation angle still needs to be extracted from the file using a library such as jsjpegmeta <http://benno.id.au/jpegmetaexample/jpegmeta.js&gt;
Modified paths:
  • /trunk/phase3/resources/mediawiki.special/mediawiki.special.upload.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.special/mediawiki.special.upload.js
@@ -39,28 +39,74 @@
4040 * @param {File} file
4141 */
4242 function showPreview(file) {
 43+ var previewSize = 180;
 44+
4345 var thumb = $("<div id='mw-upload-thumbnail' class='thumb tright'>" +
4446 "<div class='thumbinner'>" +
45 - "<img style='max-width: 180px; max-height: 180px' />" +
 47+ "<canvas width=" + previewSize + " height=" + previewSize + " ></canvas>" +
4648 "<div class='thumbcaption'><div class='filename'></div><div class='fileinfo'></div></div>" +
4749 "</div>" +
4850 "</div>");
4951 thumb.find('.filename').text(file.name).end()
50 - .find('.fileinfo').text(prettySize(file.size)).end()
51 - .find('img').attr('src', wgScriptPath + '/skins/common/images/spinner.gif');
 52+ .find('.fileinfo').text(prettySize(file.size)).end();
 53+
 54+ var ctx = thumb.find('canvas')[0].getContext('2d');
 55+ var spinner = new Image();
 56+ spinner.onload = function () {
 57+ ctx.drawImage( spinner, (previewSize - spinner.width) / 2,
 58+ (previewSize - spinner.height) / 2 );
 59+ };
 60+ spinner.src = wgScriptPath + '/skins/common/images/spinner.gif';
5261 $('#mw-htmlform-source').parent().prepend(thumb);
5362
54 - fetchPreview(file, function(dataURL) {
55 - var img = $('#mw-upload-thumbnail img');
56 - img.load(function() {
57 - if ('naturalWidth' in img[0]) {
58 - var w = img[0].naturalWidth, h = img[0].naturalHeight;
59 - var info = mediaWiki.msg('widthheight', w, h) +
60 - ', ' + prettySize(file.size);
61 - $('#mw-upload-thumbnail .fileinfo').text(info);
 63+ fetchPreview(file, function(dataURL) {
 64+ var img = new Image();
 65+ var rotation = 0;
 66+ img.onload = function() {
 67+ // Fit the image within the previewSizexpreviewSize box
 68+ if ( img.width > img.height ) {
 69+ width = previewSize;
 70+ height = img.height / img.width * previewSize;
 71+ } else {
 72+ height = previewSize;
 73+ width = img.width / img.height * previewSize;
6274 }
63 - });
64 - img.attr('src', dataURL);
 75+ // Determine the offset required to center the image
 76+ dx = (180 - width) / 2;
 77+ dy = (180 - height) / 2;
 78+ switch (rotation) {
 79+ // If a rotation is applied, the direction of the axis
 80+ // changes as well. You can derive the values below by
 81+ // drawing on paper an axis system, rotate it and see
 82+ // where the positive axis direction is
 83+ case 0:
 84+ x = dx;
 85+ y = dy;
 86+ break;
 87+ case 90:
 88+ x = dx;
 89+ y = dy - previewSize;
 90+ break;
 91+ case 180:
 92+ x = dx - previewSize;
 93+ y = dy - previewSize;
 94+ break;
 95+ case 270:
 96+ x = dx - previewSize;
 97+ y = dy;
 98+ break;
 99+ }
 100+
 101+ ctx.clearRect( 0, 0, 180, 180 );
 102+ ctx.rotate( rotation / 180 * Math.PI );
 103+ ctx.drawImage( img, x, y, width, height );
 104+
 105+ // Image size
 106+ var info = mediaWiki.msg('widthheight', img.width, img.height) +
 107+ ', ' + prettySize(file.size);
 108+ $('#mw-upload-thumbnail .fileinfo').text(info);
 109+ }
 110+ img.src = dataURL;
65111 });
66112 }
67113
@@ -101,6 +147,7 @@
102148 function clearPreview() {
103149 $('#mw-upload-thumbnail').remove();
104150 }
 151+
105152
106153 if (hasFileAPI()) {
107154 // Update thumbnail when the file selection control is updated.

Sign-offs

UserFlagDate
Krinkletested18:48, 7 March 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r79868Clean up mediawiki.special.upload.js...krinkle16:43, 8 January 2011
r80775Follow-up r79867: Read out EXIF orientation in JavaScript and rotate accordin...btongminh22:34, 22 January 2011
r97672* (bug 31048) Fix for width/height reported on Special:Upload thumbnail for E...brion22:25, 20 September 2011
r99913Followup r79867: re-animate the spinner during Special:Upload's thumbnail ini...brion20:56, 15 October 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r79845$wgMaxUploadSize may now be set to an array to specify the upload size limit ...btongminh22:12, 7 January 2011

Status & tagging log