User:Alex 21/script-imageres.js

$(document.getElementsByClassName("fileInfo")).ready(function() {
	// Filenamespace only
	if (mw.config.get("wgNamespaceNumber") !== 6) return;
	
	// Get file information: height and width
	var fileInfo = document.getElementsByClassName("fileInfo")[0];
	if(!fileInfo) return;
	var height_width = fileInfo.innerHTML.match(/([\d,]+) × ([\d,]+)/);

	// With the height and width, calculate the total pixel count
	var width = parseInt(height_width[1].replace(/,/g,''));
	var height = parseInt(height_width[2].replace(/,/g,''));
	var total_pixels = height*width;

	// Maximum pixel count per IMAGERES
	var max_pixels = 100000;

	// Calculate the new width, height, dimensions and total pixel count with the
	// formula from IMAGERES, and confirm the new total pixel count is within range
	var new_width = Math.floor(Math.sqrt(max_pixels*width/height));
	var new_height = Math.round(height*new_width/width);
	var new_total_pixels = new_width*new_height;
	while (new_total_pixels > max_pixels) {
		new_width--;
		new_height = Math.round(height*new_width/width);
		new_total_pixels = new_width*new_height;
	}

	// Get available width per the preset thumbnail sizes
	var thumbnail_sizes = [20, 40, 60, 120, 250, 330, 500, 960, 1280, 1920, 3840];
	var new_width0 = thumbnail_sizes.filter(v => v < new_width).pop();
	new_width = new_width0;
	new_height = Math.round(height*new_width/width);
	new_total_pixels = new_width*new_height;
	var new_dimensions = new_width+" × "+new_height;

	// Colour coding the results; green = good, red = bad
	var colour_start = "";
	var colour_end = "";
	if (total_pixels > max_pixels) {
		colour_start = "<span style='color:red'><b>";
		colour_end = "</b></span>";
	} else {
		colour_start = "<span style='color:green'>";
		colour_end = "</span>";
	}

	// Add the total pixel count after the initial dimensions
	fileInfo.innerHTML = fileInfo.innerHTML.replace("pixels,",
		"pixels, "+colour_start+number_format(total_pixels)+colour_end+" total pixels, ");
	
	// If the image exceeds 0.1MP, then report the error
	if (total_pixels > max_pixels) {
		// Create a image link with the maximum reduced size
		var image_link = document.getElementsByClassName('internal')[0].href;
		var new_image_link = image_link.replace("/en/", "/en/thumb/")+"/"+new_width+"px-"+image_link.substr(image_link.lastIndexOf("/")+1);
		
		var reuploadReason = "Resize to less than 0.1 MP per [[WP:IMAGERES]] via [[User:Alex 21/script-imageres|script]].";
		var replaceLink = document.getElementById('mw-imagepage-reupload-link');
		if ( !replaceLink || !replaceLink.children) return;
		var reuploadLink = replaceLink.children[0];
		var reuploadLinkNew = reuploadLink.href+"&wpUploadDescription="+reuploadReason;
		
		// Add the maximum size of the image that's less than 0.1MP, the total pixel count, the new image link, and the reupload link summary
		fileInfo.innerHTML = fileInfo.innerHTML+"<br>(maximum size: "+colour_start+new_dimensions+colour_end+
			" pixels, "+number_format(new_total_pixels)+" total pixels, <a href='"+new_image_link+"'>resized file</a>, <a href='"+reuploadLinkNew+"'>upload resized file</a>)";
	}

	// Add commas to values over 1,000
	function number_format(x) {
		return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.