Unexpectedly compressing JS files into PNG Image Storage Methods

Source: Internet
Author: User

Have you ever thought about: To Compress js files, convert js files into PNG images, and then use the getImageData () function in the canvas control to re-read images into js files. I mentioned this method in my post on rapid loading of JS files that I posted here yesterday. Some netizens are very interested in this method, so I will explain it in detail today.

In this way, a high compression ratio can be achieved. The following section describes how high the compression ratio is. This method uses the canvas control, which means that it is only effective in browsers that support the canvas control.

Now you can see that the above image is similar to a noisy image, but it is actually a 30 K 8-bit PNG image converted from the prototype Framework Code of 12 4 K. The compression ratio is good ).

In fact, to convert code into image format storage, you can convert it into GIF and PNG formats. PNG images have 24-bit and 8-bit RGB Images. Each pixel can store 3 bytes of data. If an 8-bit RGB image is used, each pixel can store 1 byte of data.

Tests in PHOTOSHOP show that a x 8-bit solid color noise image can be compressed to 5 K, and the same solid color noise image, for a x 24-bit image, it can only be compressed to 20 kb. For 8-bit GIF images with the same image, the compression effect is worse than that of PNG images. Therefore, we choose to use an 8-bit PNG image as the storage format for compression and decompression.

Now we need to start compressing the image. below is the compressed file address written in PHP.

 
 
  1. <?  
  2. $filename = "prototype-1.6.0.2.js";  
  3. if (file_exists($filename)) {  
  4.     $iFileSize = filesize($filename);  
  5.     $iWidth = ceil(sqrt($iFileSize / 1));  
  6.     $iHeight = $iWidth;  
  7.     $im = imagecreatetruecolor($iWidth, $iHeight);  
  8.     $fs = fopen($filename, "r");  
  9.     $data = fread($fs, $iFileSize);  
  10.     fclose($fs);  
  11.     $i = 0;  
  12.     for ($y=0;$y<$iHeight;$y++) {  
  13.         for ($x=0;$x<$iWidth;$x++) {  
  14.             $ord = ord($data[$i]);  
  15.             imagesetpixel($im,   
  16.                 $x, $y,  
  17.                 imagecolorallocate($im,  
  18.                     $ord,  
  19.                     $ord,  
  20.                     $ord 
  21.                 )  
  22.             );  
  23.             $i++;  
  24.         }  
  25.     }  
  26.     header("Content-Type: image/png");  
  27.     imagepng($im);  
  28.     imagedestroy($im);  
  29. }  
  30. ?> 

It reads the JS file and creates a PNG image. each pixel in the image is a value between 0 and, which corresponds to the ascII value of the JS character.

Of course, in addition to compression, we also need to extract the image, that is, the process of reading the image as a JS file. This function is written in JS and can be downloaded from the following location.

 
 
  1. function loadPNGData(strFilename, fncCallback) {  
  2.     // test for canvas and getImageData  
  3.     var bCanvas = false;  
  4.     var oCanvas = document.createElement("canvas");  
  5.     if (oCanvas.getContext) {  
  6.         var oCtx = oCanvas.getContext("2d");  
  7.         if (oCtx.getImageData) {  
  8.             bCanvas = true;  
  9.         }  
  10.     }  
  11.     if (bCanvas) {  
  12.         var oImg = new Image();  
  13.         oImg.style.position = "absolute";  
  14.         oImg.style.left = "-10000px";  
  15.         document.body.appendChild(oImg);  
  16.         oImg.onload = function() {  
  17.             var iWidth = this.offsetWidth;  
  18.             var iHeight = this.offsetHeight;  
  19.             oCanvas.width = iWidth;  
  20.             oCanvas.height = iHeight;  
  21.             oCanvas.style.width = iWidth+"px";  
  22.             oCanvas.style.height = iHeight+"px";  
  23.             var oText = document.getElementById("output");  
  24.             oCtx.drawImage(this,0,0);  
  25.             var oData = oCtx.getImageData(0,0,iWidth,iHeight).data;  
  26.             var a = [];  
  27.             var len = oData.length;  
  28.             var p = -1;  
  29.             for (var i=0;i<len;i+=4) {  
  30.                 if (oData[i] > 0)  
  31.                     a[++p] = String.fromCharCode(oData[i]);  
  32.             };  
  33.             var strData = a.join("");  
  34.             if (fncCallback) {  
  35.                 fncCallback(strData);  
  36.             }  
  37.             document.body.removeChild(oImg);  
  38.         }  
  39.         oImg.src = strFilename;  
  40.         return true;  
  41.     } else {  
  42.         return false;  
  43.     }  

Finally, the online test URL is provided. On this webpage, you can select a PNG image file in the list and click the load file button to view the image on the webpage, below the image is the code file read by the image. Http://www.nihilogic.dk/labs/canvascompress/

Link: http://www.cnblogs.com/ilian/archive/2012/06/21/js-to-png.html

Edit recommendations]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.