#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") ;

    }
相关推荐
JaguarJack7 小时前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo7 小时前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack1 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
QQ5110082852 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php