In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to convert php html into pictures, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Php converts html into pictures: 1, create a new html2pdf folder; 2, enter "include ('. / html2pdf/mpdf');"; 3, use the "function pdf2png () {...}" method to convert html into pictures.
This article operating environment: Windows7 system, PHP5.6 version, DELL G3 computer.
How to convert php html into pictures?
The realization method of converting html into Picture by php
Parse on the server side to convert the compiled html into pictures.
Because html is generally parsed by client browsers, the server cannot parse html code directly. So we need to complete this requirement with the help of php class library and extension.
The file conversion process is html-> pdf-> png.
The class libraries that need help are mPDF, imagick.
The official download address of pdf is: http://www.mpdf1.com/mpdf/index.php (although recommended at 6.0,0) this is a class library that can be downloaded and uploaded to the server directly. There are a lot of things in it. Create a new html2pdf folder and introduce it.
Include ('. / html2pdf/mpdf')
The whole function
/ * convert name html to pdf image function converts html page to pdf image (some css styles are not recognized) the number of parameters is 2 1. Must html code can get 2. 0 with file_get_contenth. Must generate pdf storage location path 3. Optional pdf width 4. Optional pdf high return value picture name instance code ($html,'img/1.pdf'); * * / function html2pdf ($html, $PATH, $walled 414, $html 736) {/ / set the Chinese font (it is important that it affects the image generation in step 2) $mpdf=new mPDF ('utf-8'); $mpdf- > autoScriptToLang = true;$mpdf- > autoLangToFont = true;// sets the size of pdf $mpdf- > WriteHTML ('); / / sets the pdf display mode $mpdf- > SetDisplayMode ('fullpage') / / Delete the first page of pdf (an extra page due to setting the pdf size) $mpdf- > DeletePages (1Power1); $mpdf- > WriteHTML ($html); $pdf_name = md5 (time ()). '.pdf'; $mpdf- > Output ($PATH.$pdf_name); return $pdf_name;}
Using this function can basically solve the problem of HTML to pdf, it should be noted that mpdf can not effectively identify all css styles in html, such as position border-radius and so on. The position can be solved with margin, and if you need to display a rounded picture, you need to crop the picture into a circle.
Next, you need to convert pdf to png images. This step requires you to install the ImageMagick component on the server and run 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
At this point, pay attention to running.
Yum list | grep imagick
According to the query results, I chose to install 5.6.3 according to my own 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 see if the installation is successful
Then use the function to convert the generated pdf to png.
/ * convert the name pdf to png picture function to convert pdf image to png picture parameter number 2 1. Must html code can get 2. 0 with file_get_contenth. You must generate an instance of the pdf storage location path code ($html,'img/1.pdf'); * * / function pdf2png ($PDF, $PNG, $wicked 50, $html 50) {if (! extension_loaded ('imagick')) {return false;} if (! file_exists ($PDF)) {return false;} $im = new Imagick (); $im- > setResolution ($wCinh); / / 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, basically completed the image of the simple page. The size of the picture is about 1m. I don't know if I'm too young.
The above content is how to convert php html into pictures. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.