PHP implements the method of converting HTML to pictures

Source: Internet
Author: User
Tags imagemagick php class
This article mainly introduces the PHP implementation of the HTML into the image of the method, interested in the reference of friends, I hope to be helpful to everyone.

The server-side parsing transforms the compiled HTML into a picture.

Because HTML is typically parsed by the client browser, the server side cannot parse the HTML code directly. So we need to do this with PHP class libraries and extensions.

The file conversion process is html-> pdf->png.

The class library to use is Mpdf, Imagick

PDF official Download address is: http://www.mpdf1.com/mpdf/index.php (recommended under 6.0 although Big Point) this is a class library direct download upload to the server can, there are a lot of things, a new Html2pdf folder introduced


Include ('./html2pdf/mpdf ');


The whole of a function


/* Name  HTML conversion to PDF picture feature converts an  HTML page to a PDF image (some CSS styles are not recognized) number of Parameters 2 1. The HTML code is required to get 2 with File_get_contenth. Generate PDF Storage location path 3. Non-required PDF width 4. Non-mandatory PDF high return value picture name Instance  code ($html, ' img/1.pdf '); * */function html2pdf ($html, $PATH, $w =414, $h = 736) {//Set the Chinese font (it is important that it affects the image generation in the second step) $mpdf =new mpdf (' utf-8 '); $mpdf->autoscripttolang = true; $mpdf->autolangtofont = true;//Sets the size of the PDF $mpdf->writehtml (' <pagebreak sheet-size= '. $w. ' mm '. $h. ' mm '/> ');//Set PDF display mode $mpdf-> Setdisplaymode (' fullpage ');//delete the first page of the PDF (due to the size of the PDF, which results in one more page) $mpdf->deletepages, $mpdf->writehtml ($html); Pdf_name = MD5 (Time ()). PDF '; $mpdf->output ($PATH. $pdf _name); return $pdf _name;}


With this function basically can solve the problem of HTML to PDF, it is important to note that Mpdf does not effectively recognize all CSS styles in HTML, such as position Border-radius. Position can be resolved with margin, you need to display the fillet picture, you need to cut the picture into a circle.

The next step is to convert the PDF to a PNG image. You need to install the ImageMagick component on the server once to run the command


Yum install-y imagemagickyum install-y imagemagick-develyum install-y gccyum install-y php-pearyum install-y GHOSTSCR Iptyum install-y ghostscript-devel.x86_64


Take note of this step to run


Yum List |grep Imagick


Choose to install based on your server version according to the query results I'm 5.6.3.


Yum install-y Php56w-pecl-imagick.x86_64yum install-y php56w-pecl-imagick-devel.x86_64


Restarting the server


Service Nginx Restartservice php-fpm restart


Use Phpinfo () or run Php-m | grep Imagick to see if the installation was successful

You can then use the function to convert the generated PDF to PNG.


/* Name  PDF convert to PNG picture function  convert pdf picture to png picture parameter number 2 1. The HTML code must be available with File_get_contenth for 2. You must generate a PDF storage location path Instance  code ($ HTML, ' img/1.pdf '); * */function pdf2png ($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 quality of picture compression $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 complete the simple picture of the page. The image size is about 1M. It's too small to be clear.

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.