本地環境
系統: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") ;}