Home  >  Article  >  Backend Development  >  How to convert word to picture in php

How to convert word to picture in php

藏色散人
藏色散人Original
2020-10-19 11:01:296039browse

How to convert word to picture in php: first install libreoffice on the server; then use the exec function to call the command line operation; finally use the "xec("soffice --headless --invisible..."" method to Just convert pictures in word.

How to convert word to picture in php

Recommended: "PHP Video Tutorial"

linux, centos, php, word Method of converting pictures (without formatting, garbled characters, etc.)

At the beginning, it was the client's job. I really didn't expect that I was stuck by such a method. I was really drunk. After a long process After a week, I finally solved this problem. Here I will introduce the following method to you. It can be convenient for everyone.

[Note] This method can also convert ppt to pictures (I haven’t tried it with execl. If you have time, you can You can try it)

(There is no way to convert directly, so I found a compromise method, first convert it to a pdf file, and then convert the pdf to an image)

Let’s talk about the installation of the environment first (I also experienced several failures before summarizing the installation of the environment. * indicates necessary operations. If you are a master, please avoid it)

Server environment: centos7

*Integrated environment: Pagoda

After installing Pagoda, choose to install the integrated environment of LNMP (it is best to install php version 5.6 and above)

What needs to be noted here is that you must use the nginx environment (do not use apache, There are too many pitfalls), and there is the integrated environment of Pagoda. Don’t ask me why, because there are too many pitfalls. As long as you have enough time and there is no time limit for the work, you can also try to install various environments yourself (for the sake of speed, just Follow my environment installation steps)

Wait...

After the environment installation is completed, we enter the Pagoda software store, click on the settings behind php, and then install the imagemagick extension for php

Wait for the installation of this php extension to be completed

After the installation is completed, we start to install libreoffice on the server

Just execute the following code directly (the first one is installed and it will be ok, Errors may be reported during the installation of the latter two, but as long as the first one is installed, it actually has no big impact)

1 yum install libreoffice
2 yum install libreoffice-headless
3 yum -y install  libreoffice-langpack-zh-Han*

After installing libreoffice, we use the following code to test whether the installation is successful:

soffice --headless --invisible --convert-to pdf 1.docx

(If you are converting ppt to a picture, just replace the following docx file with a pptx file)

If a pdf file with the same name as word is generated in the directory where the command is executed, This means that libreioffice has been successfully installed.

You can use Baidu for libreoffice on Linux

In php, we can use the exec() function to call command line operations, as well as shell_exec() and other functions (Function usage is on Baidu)

If the exec() method cannot be executed in the php method, it means that php has disabled these methods. At this time, we can modify the configuration in the php.ini file

Find the line starting with disable_functions = in the php.ini file, then delete the functions you want to use later, and then restart php. Then the php we wrote can call exec() and other functions

At this point, we have successfully installed all the environments needed to convert word to pictures

How to convert word to pdf in php:

$result = exec("soffice --headless --invisible --convert-to pdf 1.docx");
echo json_encode($result);

You can generate a pdf file with the same name in word in the same directory

How to convert pdf to pictures in php:

$result = pdf2png("/www/wwwroot/wwj.wangwenjie.club/1.pdf","/www/wwwroot/wwj.wangwenjie.club/");
function pdf2png($pdf, $path)
    {
        if (!extension_loaded('imagick')) {
            return false;
        }
        if (!file_exists($pdf)) {
            return false;
        }
        $im = new \Imagick();
        $im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
        $im->setCompressionQuality(100);
        $im->readImage($pdf);
        foreach ($im as $k => $v) {
            $v->setImageFormat('png');
            $fileName = $path . md5($k . time()) . '.png';
            if ($v->writeImage($fileName) == true) {
                $return[] = $fileName;
            }
        }
        return $return;
    }

You can generate png pictures in the same directory (there are several pictures in the pdf page)

About converting pdf to pictures You can refer to the method:

https://blog.csdn.net/jeff_love_marina/article/details/80838055 (The environment and other above steps have been installed, so just look at the method of this link)

At this point, the method of converting word to image in php under Linux is finished. Come on

The above is the detailed content of How to convert word to picture in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn