PHP - FPDI -旋转图像

zengzsys  于 2023-06-21  发布在  PHP
关注(0)|答案(2)|浏览(95)

我正在使用FPDI导入图像顶部的PDF。一切都正常了,但现在我想旋转图像。下面是我的代码:

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();

这条线运行良好:

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

但是,如果我尝试以下操作:

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

我得到一个错误:FPDI:RotatedImage未定义。
我正在尝试让FPDF Rotate方法(http://www.fpdf.org/en/script/script2.php)与FPDI(https://www.setasign.com/products/fpdi/about/)一起工作
以前有人做过这个吗?

sgtfey8w

sgtfey8w1#

RotatedImage()函数放在类中。
它是未定义的,因为它试图在$pdf上执行函数,但它当前存在于类之外,所以它看不到它。

mwkjh3gx

mwkjh3gx2#

$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");

相关问题