|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-07-21 13:22 UTC] sramage at fatpipeinternet dot com
Stats
Apache 1.3.22
PDFlib GmbH Version 4.0.2 Revision 1.106
Here's the first tested piece of code, works with Both JPEG and PNG, but hangs with TIFF.
<?php
@define ("DOCUMENT_PNG", "png");
@define ("DOCUMENT_JPEG", "jpeg");
@define ("DOCUMENT_GIF", "gif");
@define ("DOCUMENT_PDF", "pdf");
@define ("DOCUMENT_TIFF", "tiff");
function CreateScaledPDF($input_file,$file_type=DOCUMENT_PDF,$width=421,$height=595,$output_image=false){
if(!$height){
$height=84;
}
if(!$width){
$width=59;
}
if(file_exists($input_file)){
if(!$output_file){
$ext=strrev(strtok(strrev($input_file),"."));
$core=strrev(strtok(""));
$output_file=$core."_thumb.pdf";
}
$pdf = pdf_new();
if (pdf_open_file($pdf)) {
pdf_begin_page($pdf,$width,$height);
switch($file_type){
case DOCUMENT_PDF:
$pdi=pdf_open_pdi ($pdf,$input_file,"",0);
$page=pdf_open_pdi_page($pdf,$pdi,1,"Thumbnail");
$x=pdf_get_pdi_value($pdf,"pagewidth",$pdi,$page,0);
$y=pdf_get_pdi_value($pdf,"pageheight",$pdi,$page,0);
break;
case DOCUMENT_TIFF:
$image=pdf_open_image_file($pdf,$file_type,$input_file,"page",1);
$x=pdf_get_value($pdf,"imagewidth",$image);
$y=pdf_get_value($pdf,"imageheight",$image);
break;
case DOCUMENT_JPEG:
case DOCUMENT_PNG:
$image=pdf_open_image_file($pdf,$file_type,$input_file);
$x=pdf_get_image_width($pdf,$image);
$y=pdf_get_image_height($pdf,$image);
break;
}
if(!$y){
$y=1;
}
if(!$x){
$x=1;
}
$scalex=$width/$x;
$scaley=$height/$y;
$scale=min($scalex,$scaley);
if($scale>1){
$x=$width-($x/2);
$y=$height-($y/2);
$scale=1;
}else{
if($y*$scale == $height && $x*$scale != $width){
$y=0;
$x=($width-($x*$scale))/2;
}elseif($x*$scale == $width && $y*$scale != $height){
$x=0;
$y=($height-($y*$scale))/2;
}else{
$x=0;
$y=0;
}
}
switch($file_type){
case DOCUMENT_PDF:
pdf_place_pdi_page($pdf,$page,$x,$y,$scale,$scale);
pdf_close_pdi_page($pdf,$page);
pdf_close_pdi($pdf,$pdi);
break;
case DOCUMENT_TIFF:
case DOCUMENT_JPEG:
case DOCUMENT_PNG:
pdf_place_image($pdf,$image,$x,$y,$scale);
pdf_close_image($pdf,$image);
break;
}
pdf_end_page($pdf);
pdf_close($pdf);
$pdf_output=pdf_get_buffer($pdf);
$fp=fopen($output_file,"w");
fwrite($fp,$pdf_output,strlen($pdf_output));
fclose($fp);
pdf_delete($pdf);
$return=true;
}else{
$this->error="couldn't create PDF file";
$return=false;
}
}else{
$this->error="no input file";
$return=false;
}
return $return;
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 20:00:01 2025 UTC |
Here's one piece of code that I couldn't test but its better for iteration testing and more compact: function TIFFtoPDF($input_file,$output_file=false){ if(file_exists($input_file)){ if(!$output_file){ $ext=strrev(strtok(strrev($input_file),".")); $core=strrev(strtok("")); $output_file=$core.".pdf"; } $pdf = pdf_new(); if (pdf_open_file($pdf,$output_file)) { pdf_set_parameter($pdf, "warning", "true"); pdf_set_info($pdf, "Creator", "Automatic"); pdf_set_info($pdf, "Author", "Automatic"); pdf_set_info($pdf, "Title", basename($input_file)); pdf_set_info($pdf, "Subject", "Generated PDF Document Thumbnail"); $count=1; while($image=@pdf_open_image_file($pdf,DOCUMENT_TIFF,$input_file,"page",$count)){ $x=pdf_get_value($pdf,"imagewidth",$image); $y=pdf_get_value($pdf,"imageheight",$image); pdf_begin_page($pdf,$x,$y); pdf_place_image($pdf,$image,0,0,1); pdf_close_image($pdf,$image); pdf_end_page($pdf); $count++; } pdf_close($pdf); $return=$output_file; }else{ $return=false; } }else{ $return=false; } }$pdf = pdf_new(); if (pdf_open_file($pdf)) { $image=pdf_open_image_file($pdf,"tiff","test.tif","page",1); $x=pdf_get_value($pdf,"imagewidth",$image); $y=pdf_get_value($pdf,"imageheight",$image); pdf_begin_page($pdf,$x,$y); pdf_place_image($pdf,$image,0,0,1); pdf_close_image($pdf,$image); pdf_end_page($pdf); pdf_save($pdf); $data=pdf_get_buffer($pdf); pdf_close($pdf); pdf_delete($pdf); $fp=fopen($output_file,"w"); fwrite($fp,$data,strlen($data)); fclose($fp); }sorry last one was wrong 13 lines short as I could do =) $pdf = pdf_new(); if (pdf_open_file($pdf,"output.pdf")) { $image=pdf_open_image_file($pdf,"tiff","test.tif","page",1); $x=pdf_get_value($pdf,"imagewidth",$image); $y=pdf_get_value($pdf,"imageheight",$image); pdf_begin_page($pdf,$x,$y); pdf_place_image($pdf,$image,0,0,1); pdf_close_image($pdf,$image); pdf_end_page($pdf); pdf_save($pdf); pdf_close($pdf); pdf_delete($pdf); }