Php converts html into an image,

Source: Internet
Author: User
Tags imagemagick

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 pdf to png images.Install the ImageMagick component 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

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.

The above php implementation method for converting html into images is to share all the content with you through small editors. I hope to give you a reference, and I hope you can also provide more support for the help house.

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.