<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Image extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'image {src_file : 输入路径} {dst_file : 输出路径}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$src_file = $this->argument('src_file');
$dst_file = $this->argument('dst_file');
if(empty($src_file) || empty($dst_file)){
cmslog(0,['src_file' => $src_file, 'dst_file' => $dst_file, 'error_data' => 0]);
echo 'save fail: 源文件或目标文件为空';
exit;
//return $this->error(__('lang.error_data'));
}
//var_dump("睡眠10秒,已处理完成".$this->argument('src_file').'--'.$this->argument('dst_file'));
try {
$img_info = getimagesize($src_file);
$pageWidth = $img_info[0];
$pageHeight = $img_info[1];
$pageWidth = $pageWidth / 72 * 25.4;
$pageHeight = $pageHeight / 72 * 25.4;
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8',"tempDir" => storage_path('tempdir'), 'format' => [$pageWidth, $pageHeight]]);
$mpdf->WriteHTML("<html><body style='background-image:url(\"". $src_file ."\"); background-image-resize: 5; background-position: top center;'></body></html>");
//$mpdf->Output();
$mpdf->Output($dst_file, \Mpdf\Output\Destination::FILE);
} catch (\Exception $exception) {
cmslog(0,['src_file' => json_encode($exception->getMessage(),true), 'error_data' => 'Exception']);
echo "save fail:" . $exception->getMessage();
exit;
//return $this->error(__('lang.error_data'));
}
//cmslog(0,['src_file' => $src_file, 'dst_file' => $dst_file, 'error_data' => 'success']);
echo 'save ok';
exit;
}
}
/usr/local/php/bin/php /usr/local/apache/htdocs/cms/artisan image '/usr/local/apache/htdocs/webserver/ftp/htmlbanner/108/003.png' '/usr/local/apache/htdocs/webserver/ftp/htmlbanner/108/49.pdf'
PHP laravel artisan将图片转换成PDF
于 2022-12-02 14:18:18 首次发布