php图片转pdf&图片转pdf上传七牛云(使用laravel框架)
安装fpdf依赖
composer require setasign/fpdf
代码实现
<?php
namespace App\Utils;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Tools
{
public static function imageToPdf()
{
$imagePath = public_path() . '/label.png';
list($imageWidth, $imageHeight) = getimagesize($imagePath);
$pdf = new \FPDF();
$pdf->AddPage();
$pdf->SetPageSize(array($imageWidth, $imageHeight));
$pdf->Image($imagePath, 0, 0, $imageWidth, $imageHeight);
$pdf->Output(public_path() . '/label.pdf', 'F');
}
public static function uploadAsPdf()
{
$imagePath = public_path() . '/label.png';
$pdf = new \FPDF();
$pdf->AddPage();
$pdf->Image($imagePath, 0, 0, 210, 346);
ob_start();
$pdf->Output();
$pdfContent = ob_get_clean();
$config = config('filesystems.disks');
$accessKey = $config['qiniu']['access_key'];
$secretKey = $config['qiniu']['secret_key'];
$bucket = $config['qiniu']['bucket'];
$domain = $config['qiniu']['domains']['custom'];
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
$key = 'label.pdf';
list($ret, $err) = $uploadMgr->put($token, $key, $pdfContent);
if ($err !== null) {
throw new \Exception($err);
return false;
}
return $ret;
}
}