php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18459 Crashes When using pdf_open_image_file() with a tiff
Submitted: 2002-07-21 13:22 UTC Modified: 2002-09-11 11:54 UTC
From: sramage at fatpipeinternet dot com Assigned:
Status: Closed Package: PDF related
PHP Version: 4.2.1 OS: FreeBSD
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sramage at fatpipeinternet dot com
New email:
PHP Version: OS:

 

 [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;
}

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-21 13:26 UTC] sramage at fatpipeinternet dot com
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;
	}
}
 [2002-07-21 13:40 UTC] derick@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a backtrace to see what is happening behind the scenes. To
find out how to generate a backtrace, please read
http://bugs.php.net/bugs-generating-backtrace.php

Once you have generated a backtrace, please submit it to this bug
report and change the status back to "Open". Thank you for helping
us make PHP better.
 [2002-07-21 13:54 UTC] sramage at fatpipeinternet dot com
I'm at a shared hosting facility without shell access, is there any other way to get the needed info?
all I get access to is my HTTP access and error logs.
 [2002-07-21 14:04 UTC] derick@php.net
Then provide a short selfcontained script that reproduces the problem so that we can try it.

Derick
 [2002-07-21 17:02 UTC] sramage at fatpipeinternet dot com
self contained code below

here is link to a standard tiff file
http://fatpipeinternet.com/test/before1.tif

<?php
@define ("DOCUMENT_TIFF", "tiff");
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)) {
			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");
			$count=1;
			while($image=pdf_open_image_file($pdf,DOCUMENT_TIFF,$input_file,"page",$count)){
				if($count>2){
					break;
				}
				$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_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);
			$return=$output_file;
		}else{
			$return=false;
		}
	}else{
		$return=false;
	}
}
$passs_or_fail="";
if(is_array($_FILES)){
	while(list($k,$file_array)=each($_FILES)){
		if(file_exists($file_array["tmp"])){
			if(TiffToPDF($file_array["tmp"],"tiff_to_pdf_test.pdf")){
				$pass_or_fail="PASSED<iframe width=\"500\" height=\"200\" src=\"tiff_to_pdf_test.pdf\"></iframe>";
			}else{
				$pass_or_fail="couldn't Convert TIFF to PDF";
			}
			
		}else{
			$pass_or_fail="File didn't upload";
		}
	}
}
$output="$pass_or_fail<BR><form enctype=\"multipart/form-data\" action=\"\" method=\"post\">Send this file: <input name=\"userfile\" type=\"file\"><input type=\"submit\" value=\"Send File\"></form>";
echo $output;
?>
 [2002-07-22 21:16 UTC] sniper@php.net
Please provide a SHORT script...and by short we mean under 10 lines..

 [2002-07-22 22:20 UTC] sramage at fatpipeinternet dot com
$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);
}
 [2002-07-22 22:24 UTC] sramage at fatpipeinternet dot com
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);
}
 [2002-07-23 15:24 UTC] sniper@php.net
There was some problem with PDFlib and TIFFs..
The patch to fix that doesn't seem to be available
anymore (there is 4.0.3 release already).

Please update your pdflib and try again, I can not reproduce the crash. 

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 12:01:27 2024 UTC