#php把pdf文件转成图片#

本地环境

系统:win11 64位

环境:phpStudy

PHP版本:8.0.2

矿建:laravel

配置扩展

一、安装imageMagick

下载地址:https://imagemagick.org/script/download.php

安装版本:ImageMagick-最新版本-Q16-HDRI-x64-dll

配置环境变量:我的电脑-属性-高级系统设置-环境变量-系统变量-path-编辑-新建-引入安装路径

二、安装ghostscript

下载地址:https://www.ghostscript.com/releases/gsdnld.html

安装版本:gs926w64.exe

配置环境变量:我的电脑-属性-高级系统设置-环境变量-系统变量-path-编辑-新建-引入安装路径\bin

三、下载php_imagick.dll扩展

下载地址:https://pecl.php.net/package/imagick/3.4.4/windows

安装版本:php_imagick-3.4.4-{当前使用的php版本}-nts-vc15-x64

意事项:选择与自身PHP版本相同的下载,如果phpinfo的Thread Safety是disabied选择nts版本,否则选ts版本

四、开启php_imagick.dll扩展

1.把php_imagick.dll文件复制到 ext 目录下(phpstudy-属性-打开 文件所在的位置 -> 返回上一层 -Extensions- php-php8.0.2nts ->ext)

2.把其他.dll文件复制在php根目录下(phpstudy-属性-打开 文件所在的位置 -> 返回上一层 -Extensions- php-php8.0.2nts

3.开启扩展(phpStudy-网站-选择开发的项目-管理-php扩展-勾选imagick)

五、实现代码

1:前端代码

复制代码
<!DOCTYPE html>
<html>
   <head>
      <title>注册画面</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <!-- 引入 Bootstrap -->

 
      <!-- HTML5 Shiv 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
      <!-- 注意: 如果通过 file://  引入 Respond.js 文件,则该文件无法起效果 -->
      <!--[if lt IE 9]>

      <![endif]-->
      <style>

      </style>
   </head>
   <body>
      <div id="main">
      	<form class="form-horizontal" role="form"  enctype="multipart/form-data"  action="/test/savepdf"  method="POST">
            <div class="form-group">
                <div class="col-sm-offset-3 col-sm-9">
                    <input type="file" name="pdffile">
                </div>
            </div>
					<div class="form-group">
						<div class="col-sm-offset-3 col-sm-9">
                            <input type="submit"  value="保存"  class="btn btn-primary btn-group-justified">

						</div>
					</div>
				</form>
      </div>

      <script>
      	$(function() {


      	})
      </script>
   </body>
</html>
<script>

</script>

2:后端代码

复制代码
Route::any('test/savepdf', 'Teacher\TestController@savePdf');

public function  savePdf(Request $request ){


        $rootPath = storage_path('app/public');

        if ($request->hasFile('pdffile')) {
            $file = $request->file('pdffile');

            $extension = $file ->getClientOriginalExtension() ;

            if(strtolower($extension) != "pdf"){
                return 0;
            }

            $filename =  date("YmdHis").'.'.$extension;
            $path = $file->storeAs('pdfs', $filename, 'public');
            $pdfFilePath =$rootPath."/".$path;

            try{
                if(!extension_loaded('imagick')){
                    return 1;
                }
                if(!file_exists($pdfFilePath)){
                    return 2;
                }


//                $outputImage = $rootPath.'/output.png'; // 输出图片文件名
//                // 创建Imagick对象
//                $imagick = new \Imagick();
//                // 从PDF文件读取数据
//                $imagick->readImage($pdfFilePath . '[0]'); // 读取第一页,索引从0开始
//                //$imagick->readImage($pdfFilePath); // 读取第一页,索引从0开始
//
//                // 设置图片格式和质量
//                $imagick->setImageFormat('png');
//                $imagick->setImageCompressionQuality(100);
//                // 写入图片文件
//                $imagick->writeImage($outputImage);
//                 // 清理资源
//                $imagick->clear();
//                $imagick->destroy();


                $IM = new \imagick();
                $IM->setResolution(120,120);
                $IM->setCompressionQuality(100);
                $IM->readImage($pdfFilePath);

                foreach ($IM as $Key => $Var){
                    $Var->setImageFormat('png');
                    $Filename = $rootPath.'/'.$filename.'_pdfpng_'.$Key.'.png';
                    if($Var->writeImage($Filename) == true){
                        $Return[] = $Filename;
                    }
                }
            }catch(\Exception $e){

                die($e->getMessage());
                return $e->getMessage();
            }


        }



        foreach ($Return as $k=>$v){
            $item = str_replace($rootPath,"",$v) ;
            $Return[$k] = asset("storage".$item);
        }


        print_r($Return);

        die("error") ;

    }
相关推荐
Never_Satisfied7 小时前
在JavaScript / HTML中,div容器在内容过多时不显示超出的部分
开发语言·javascript·html
艾莉丝努力练剑7 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
胡萝卜3.08 小时前
深入理解string底层:手写高效字符串类
开发语言·c++·学习·学习笔记·string类·string模拟实现
西柚小萌新8 小时前
【Python从入门到精通】--Pycharm增加内存
开发语言·python·pycharm
不爱编程的小九九8 小时前
小九源码-springboot082-java旅游攻略平台
java·开发语言·旅游
只是懒得想了8 小时前
用C++实现一个高效可扩展的行为树(Behavior Tree)框架
java·开发语言·c++·design-patterns
yan8626592468 小时前
于 C++ 的虚函数多态 和 模板方法模式 的结合
java·开发语言·算法
Small___ming8 小时前
【Python基础】Python路径操作全解析:os.path、glob与pathlib从入门到精通
开发语言·python
_poplar_9 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
lly2024069 小时前
Ruby Socket 编程
开发语言