Php converts html into an image,
Parse the Compiled html into an image on the server side.
Because html is generally parsed by a client browser, the server cannot parse html code directly. Therefore, we need to use php class libraries and extensions to meet this requirement.
The file conversion process is html-> pdf-> Png.
The required class libraries are mPDF and imagick.
Pdf official is: http://www.mpdf1.com/mpdf/index.php (recommended in 6.0 although the big point) This is a class library directly download and upload to the server, there are a lot of things, create a folder to introduce html2pdf
include('./html2pdf/mpdf');
Whole Function
/* Convert the name html to a pdf image function to convert the html page to a pdf image (some css styles cannot be identified). The number of parameters is 2. you can use file_get_contenth to obtain the html code. the pdf storage path must be generated. 3. non-mandatory pdf width 4. non-mandatory pdf high-return image name instance code ($ html, 'img/1.pdf '); **/function html2pdf ($ html, $ PATH, $ w = 414, $ h = 736) {// set the Chinese font (it is important that it will affect the image generation in step 2) $ mpdf = new mPDF ('utf-8 '); $ mpdf-> autoScriptToLang = true; $ mpdf-> autoLangToFont = true; // set the pdf size $ mpdf-> WriteHTML ('<pagebreak sheet-size = "'. $ w. 'mm '. $ h. 'mm "/>'); // sets the pdf display mode $ mpdf-> SetDisplayMode ('fullpage '); // Delete the first page of the pdf file (one more page due to the setting of the pdf size) $ mpdf-> DeletePages (); $ mpdf-> WriteHTML ($ html ); $ export _name = md5(time({}.'}'; $ mpdf-> Output ($ PATH. $ partition _name); return $ partition _name ;}
Using this function can basically solve the problem of HTML to pdf. Note that mpdf cannot effectively identify all css styles in html, such as position border-radius. The position can be solved by using margin. to display the rounded image, you need to crop the image into a circle.
The next step is to convert the pdf file to a png image. This step needs to be installed on the server.The ImageMagick component runs the command once.
yum install -y ImageMagickyum install -y ImageMagick-develyum install -y gccyum install -y php-pearyum install -y ghostscriptyum install -y ghostscript-devel.x86_64
Pay attention to running
yum list |grep imagick
Based on the query results, select 5.6.3 for installation based on your server version.
yum install -y php56w-pecl-imagick.x86_64yum install -y php56w-pecl-imagick-devel.x86_64
Restart the server
service nginx restartservice php-fpm restart
Use phpinfo () or run php-m | grep imagick to check whether the installation is successful.
Then use the function to convert the generated pdf file to png.
/* Convert the name pdf to a png Image function to convert a pdf image to a png image. The number of parameters is 2. you can use file_get_contenth to obtain the html code. the code ($ html, 'img/1.pdf ') of the pdf storage path must be generated. **/function hour 2png ($ PDF, $ PNG, $ w = 50, $ h = 50) {if (! Extension_loaded ('imagick') {return false;} if (! File_exists ($ PDF) {return false ;}$ im = new Imagick (); $ im-> setResolution ($ w, $ h ); // set the resolution $ im-> setCompressionQuality (15); // set the image compression quality $ im-> readImage ($ PDF); $ im-> resetIterator (); $ imgs = $ im-> appendImages (true); $ imgs-> setImageFormat ("png"); $ img_name = $ PNG; $ imgs-> writeImage ($ img_name ); $ imgs-> clear (); $ imgs-> destroy (); $ im-> clear (); $ im-> destroy (); return $ img_name ;}
OK. The image of the simple page is basically completed. The image size is about 1 MB. I am not clear about it.