0

I am using FPDI to import a PDF over the top of an image. I have everything working, but now I want to rotate the image. Here is my code:

  require('fpdf.php');
  require('fpdi.php');

  class PDF_Rotate extends FPDI {

      var $angle = 0;

      function Rotate($angle, $x = -1, $y = -1) {
          if ($x == -1)
              $x = $this->x;
          if ($y == -1)
              $y = $this->y;
          if ($this->angle != 0)
              $this->_out('Q');
          $this->angle = $angle;
          if ($angle != 0) {
              $angle*=M_PI / 180;
              $c = cos($angle);
              $s = sin($angle);
              $cx = $x * $this->k;
              $cy = ($this->h - $y) * $this->k;
              $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
          }
      }

      function _endpage() {
          if ($this->angle != 0) {
              $this->angle = 0;
              $this->_out('Q');
          }
          parent::_endpage();
      }

  }

  function RotatedImage($file,$x,$y,$w,$angle)
  {
    //Image rotated around its upper-left corner
    $this->Rotate($angle,$x,$y);
    $this->Image($file,$x,$y,$w);
    $this->Rotate(0);
  }

  $pdf = new FPDI();

  $pdf->AddPage();

  //$pdf->Image('template/test.jpg',14,26,150);
  $pdf->RotatedImage('template/test.jpg',14,26,150,4);


  //load template
  $pdf->setSourceFile('template/photo.pdf');
  $tplIdx = $pdf->importPage(1);
  $pdf->SetAutoPagebreak(false, 0);

  //use the imported page and place it at point 0,0; calculate width and height
  //automaticallay and ajust the page size to the size of the imported page
  $pdf->useTemplate($tplIdx, 0, 0, 210.058, 296.926, true);

  $pdf->Output();

This line works fine:

$pdf->Image('template/test.jpg',14,26,150);

However if I try the following:

$pdf->RotatedImage('template/test.jpg',14,26,150,4);

I get an error: FPDI: RotatedImage undefined.

I am trying to get the FPDF Rotate method (http://www.fpdf.org/en/script/script2.php) working with FPDI (https://www.setasign.com/products/fpdi/about/)

Has anyone managed this previously?

2 Answers 2

1

Place your RotatedImage() function inside your class.

It's undefined because it's trying to execute the function on $pdf, but it currently exists outside the class, so it doesn't see it.

Sign up to request clarification or add additional context in comments.

Comments

0
        $image = "ruta/imagen.JPG";
        $degrees = 90;
        $source = imagecreatefromjpeg($image);
        //Roto la imagen y le cambio el nombre comenzando con r.nombre
        $image_rotate = "ruta/r.imagen.JPG";
        //roto la imagen 90 grados
        $rotate = imagerotate($source, $degrees, 0);
        imagejpeg($rotate, $image_rotate);
        unlink("ruta/imagen.JPG"}.JPG");

4 Comments

despues la llamo esa imagen en la funcion de fpdf
$pdf->Image($image_rotate, 110.5, 2.5, 38, 100, "JPEG");
You should add explanation.
Hello, Excuseme I am from Colombia and my write English is not very good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.