2014-12-03 4 views
0

캔버스에서 이미지를 캡처하여 아약스 게시 메서드를 통해 내 PHP 파일로 보냈습니다.PHP dataURL to png 이미지

여기 파일 유형과 확장명을 가진 png에 다시 넣어야합니다. fpdf를 사용하여이 새로운 이미지를 pdf에 추가 할 예정입니다.

여기 내 코드입니다. 나는 방금 깨진 이미지 링크 아이콘이 남았습니다.

<?php 
require_once('fpdf/fpdf.php'); 
require_once('fpdi/fpdi.php'); 
$imgEmbed = $_POST['embed']; 

//convert base64 string into an image file by wrapping 
//it in the png container 

$parts = explode(',',$imgEmbed); 
$data = $parts[1]; 
$data = base64_decode($data); 

header('Content-Type:image/png'); 

$pdf =& new FPDI(); 
$pdf->AddPage(); 

//Set the source PDF file 
$pdf->setSourceFile("test2.pdf"); 

//Import the first page of the file 
$tppl = $pdf->importPage(1); 

//Use this page as template 
// use the imported page and place it at point 20,30 with a width of 170 mm 
$pdf->useTemplate($tppl, null, null, 215.9, 279.4, TRUE); 

//Select Arial italic 8 
$pdf->SetFont('Helvetica'); 
$pdf->SetTextColor(0,0,0); 
$pdf->SetXY(100, 100); 
$pdf->Write(0, 'This is just a simple text'); 
$pdf->Image($newIm,null,null,150,100);// x y h w 'png' 

$pdf->Output("custom.pdf", "I"); // d send to browser, F send to local 
// I auto open in browser window. 
?> 
+1

'explode (',', $ imgEmbed);'는 ('. ', $ imgEmbed)'explode'이어야합니다. '출력 '은 이미지로 렌더링합니까? 나는 의심한다. –

+0

출력은 FPDF 일입니다. 'header ('Content-Type : image/png ');를'header ('Content-Type : application/pdf '); 그것은 이미지를 만드는 데 아무런 영향을 미치지 않습니다. 나는 fpdf가 png 이미지로 헤더를 인식 할 필요가있다. 안타깝게도 귀하의 제안에 따라 코드가 더 복잡해집니다. ... ( – user61026

+0

PNG를 서버에 임시 위치로 저장 한 다음 PDF에 기록 할 수 있습니까? –

답변

0
FPDF에서

$newIm='test.png';<br> if(exif_imagetype($newIm)==IMAGETYPE_PNG) {<br> $file_type="PNG";<br> }elseif (exif_imagetype($newIm) ==IMAGETYPE_JPEG) {<br> $file_type="JPG";<br> }<br> $pdf->Image($newIm,null,null,150,100,$file_type);<br>

는 이미지의 synatax 픽셀에 의해 전체 이미지의 픽셀을 읽고 이미지 기반이 PNG에 우리는 test.jpg를 같은 확장자를 주면 그것은 그래서 여기에 오류를 줄 것이다 나는 이미지의 이미지 유형을 줄 exif_imagetype()을 사용했다. 그리고 나서이 $ file_type을 구문에 전달하여 결과를 얻는다. 그리고 나의 경우에는 효과가있다.

+1

그 멋진 남자 나는 이것을 몇 달 전에 알아 냈다. – user61026