ব্যবহারকারী:আফতাবুজ্জামান/চিত্র-রেজ.js

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
$(document.getElementsByClassName("fileInfo")).ready(function() {
	// Filenamespace only
	if (mw.config.get("wgNamespaceNumber") !== 6) return;
	
	// Get file information: height and width সাহায্যে: নকীব সরকার
	const ref = "০১২৩৪৫৬৭৮৯";
	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,'').replace(/[০-৯]/g, e => ref.indexOf(e)));
	var height = parseInt(height_width[2].replace(/,/g,'').replace(/[০-৯]/g, e => ref.indexOf(e)));
	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;
	}
	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("পিক্সেল,",
		"পিক্সেল, মোট পিক্সেল: "+colour_start+number_format(total_pixels)+colour_end+", ");
	
	// 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("/bn/", "/bn/thumb/")+"/"+new_width+"px-"+image_link.substr(image_link.lastIndexOf("/")+1);
		
		var reuploadReason = "অ-মুক্ত চিত্রের আকার হ্রাসকরণ";
		var reuploadLink = document.getElementById('mw-imagepage-reupload-link').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>(সর্বোচ্চ আকার: "+colour_start+new_dimensions+colour_end+
			" পিক্সেল, মোট পিক্সেল: "+number_format(new_total_pixels)+", <a href='"+new_image_link+"'>হ্রাসকৃত চিত্র ডাউনলোড করুন</a>, <a href='"+reuploadLinkNew+"'>হ্রাসকৃত চিত্র আপলোড করুন</a>)";
	}

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