php教程

PDF_open_image_file

(PHP 4, PECL pdflib >= 1.0.0)

PDF_open_image_fileRead image from file [deprecated]

说明

PDF_open_image_file ( resource $p , string $imagetype , string $filename , string $stringparam , int $intparam ) : int

Opens an image file.

This function is deprecated since PDFlib version 5, use PDF_load_image() with the colorize, ignoremask, invert, mask, masked, and page options instead.

User Contributed Notes

John Middleton 13-Mar-2007 05:07
This short script will create a scaled PDF from a multi-page tiff file (tested and implemented in PHP 5):

<?php
  
// Declare pdf File
  
$pdf = pdf_new();
  
pdf_open_file($pdf, "test.pdf");

  
// Convert and add pages until you reach end of the tiff
  
for($page = 1; $image = @pdf_open_image_file($pdf, "tiff", "test.tif", "page", $page); $page++)
   {
     
// Set page scale to 72 dpi using the width of the image (72 * 8.5 inches = 612 dpi)
     
$scale = 612 / pdf_get_value($pdf, "imagewidth", $image);

     
// Create a new page using the scaled height and width of the image
     
pdf_begin_page($pdf, $scale * pdf_get_value($pdf, "imagewidth", $image), $scale * pdf_get_value($pdf, "imageheight", $image));

     
// Place the scaled image in the top left corner of the page and end this page of the pdf
     
pdf_place_image($pdf, $image, 0, 0, $scale);
     
pdf_end_page($pdf);
   }

  
// Finish the pdf File
  
pdf_close($pdf);
?>

Please note that this script disregards whether the height of the image is unable to scale comparably to an 8.5x11 inch document.  Depending on the images you are working with, you may want to add some logic to set your scale value based on the true height and width of the image.
Max / Poland 01-Oct-2005 03:58
another example : PHP 5 & PDFLIB 6.0.2 
<?
$pdf = pdf_new();

 if (pdf_begin_document($pdf,"", "") == 0)
    {die("Error: " . pdf_get_errmsg());}

   pdf_set_parameter($pdf, "SearchPath", "./");
   pdf_set_parameter($pdf, "hypertextencoding", "winansi");
   pdf_set_parameter($pdf, "imagewarning", "false");
   pdf_set_info($pdf, "Creator", "some INformations");
   pdf_set_info($pdf, "Author", "author");
   pdf_set_info($pdf, "Title", "Raport");

$logo = pdf_open_image_file($pdf, "jpeg", "foto.jpg", "", 0);

pdf_begin_page($pdf, 595, 842);
pdf_place_image($pdf, $logo, 28, 324, 0.125);
pdf_close_image($pdf, $logo);
pdf_end_page($pdf);
pdf_close($pdf);

  $buf = pdf_get_buffer($pdf);
  $len = strlen($buf);
  header("Content-type: application/pdf");
  header("Content-Length: $len");
  header("Content-Disposition: attachment; filename=raport.pdf");
  echo $buf;

?>
ws at esc-ag dot de 07-Apr-2005 03:25
when using pdf_open_image_file() with multipage tiffs in php 5.x.x I had to use the following

$pdf = pdf_new();
pdf_open_file($pdf, "test.pdf");
$file = "mutlipg.tif";

// Important to switch off the exceptions   
pdf_set_parameter($pdf, "imagewarning", "false");

for($i=1; ; $i++)
{
    $image=@pdf_open_image_file($pdf, "tiff", $file, "page", $i);
    if($image != 0)
    {
        $w = pdf_get_value($pdf, "imagewidth", $image);
        $h = pdf_get_value($pdf, "imageheight", $image);

    // use only half of the size
        pdf_begin_page($pdf, $w/2, $h/2);
        pdf_place_image($pdf, $image, 0, 0, 0.5);
        pdf_close_image($pdf, $image);
        pdf_end_page($pdf);
    }
    else
        break;
}
// Send the pdf to the Browser
pdf_close($pdf);
$fp = fopen("test.pdf", "r");
header("Content-type: application/pdf");
fpassthru($fp);
fclose($fp);
alfred dot zingg at freesurf dot ch 16-Dec-2004 07:27
...
$logo = pdf_open_image_file($pdf, "jpeg", "logo.jpg");
pdf_place_image($pdf, $logo, 28, 324, 0.125);
pdf_close_im($pdf, $logo);
...
does not work under PHP5, use
...
$logo = pdf_open_image_file($pdf, "jpeg", "logo.jpg", "", 0);
pdf_place_image($pdf, $logo, 28, 324, 0.125);
pdf_close_im($pdf, $logo);
...
j tse 05-Jun-2003 06:22
If the image file is sitting at somewhere (outside the subdirectory), an absolute path must be given instead.
darren at zoe dot co dot nz dot deleteme 16-Dec-2001 09:51
when using pdf_open_image_file() with multipage tiffs, the example in the manual won't work (i don't think the function returns -1 like it says)
you can use this instead;

   for ($page=1; ; $page++)
    {
      // our image
$image=pdf_open_image_file($pdf,"tiff",$file,"page",$page);
      // a bad image - page cannot be 0
$bad=pdf_open_image_file($pdf,"tiff",$file,"page",0);<br>
      if ($image == $bad)
    {
      // done adding pages
      break;
    }
      pdf_begin_page($pdf,$width,$height);
      pdf_place_image($pdf,$image,0,0,1);
      pdf_close_image($pdf,$image);
      pdf_end_page($pdf);
    }
bob at nijman dot de 02-Aug-2001 05:44
Try this.
(don't forget to put a "test.jpg" in the same dir)

<?php

//Create & Open PDF-Object
$pdf = pdf_new();
pdf_open_file($pdf);
pdf_set_info($pdf, "Author","Bob Nijman");
pdf_set_info($pdf, "Title","www.nijman.de");
pdf_set_info($pdf, "Creator", "bob@nijman.de");
pdf_set_info($pdf, "Subject", "pdf_open_image_file");
pdf_begin_page($pdf, 200, 200);

$pdfimage = pdf_open_image_file($pdf, "jpeg", "test.jpg");
pdf_place_image($pdf, $pdfimage, 10, 10, 0.6);

//close it up
pdf_end_page($pdf);
pdf_close($pdf);
$data = pdf_get_buffer($pdf);
header('Content-type: application/pdf');
header('Content-disposition: inline; filename=image.pdf');
header('Content-length: ' . strlen($data));
echo
$data;

?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788