【问题标题】:image processing in php and get PDFof processed imagephp中的图像处理并获取处理后图像的PDF
【发布时间】:2011-06-12 12:37:32
【问题描述】:

我正在编写一个脚本,我必须根据给定的文本创建一个图像。

基本上我必须将这些文本放在生成图像的不同位置。

到此为止,我的工作已经完成。下面是它的代码。

header ("Content-type: image/jpeg");
$handle = imagecreate(415, 588) or die ("Cannot Create image");

$inven_tory=imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].'/thumb/editor/text_letter.jpg');
imagecopymerge($handle,$inven_tory,0,0,0,0,415,588,100);

$bg_color = imagecolorallocate ($handle, 255, 255, 255); // 0 -255
$txt_color = imagecolorallocate ($handle, 0, 0, 0);


// The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text.
imagettftext($handle, 15, 0, 10, 25, $txt_color, "fonts/" . $font, wordwrap($text1, 35, "\n", true));

// The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text.
imagettftext($handle, 20, 0, 40, 120, $txt_color, "fonts/" . $font, wordwrap($text2, 13, "\n", true));

// The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text.
imagettftext($handle, 15, 0, 10, 500, $txt_color, "fonts/" . $font, wordwrap($text3, 40, "\n", true));

imagejpeg($handle);

图片正在从此页面正确创建。现在我将这个生成的图像放在 PDF 中并让用户保存该 PDF。我怎样才能做到这一点?如果您不清楚,请告诉我。

【问题讨论】:

    标签: php image image-processing gd


    【解决方案1】:

    我会尝试 wkhtmltodpf 和 exec()。

    【讨论】:

      【解决方案2】:

      如果您在安装了 ImageMagick 的 Linux 服务器上,您可以执行以下操作:

      exec("convert foo.jpg bar.pdf");
      

      【讨论】:

        【解决方案3】:

        如何将图像放入 PDF 取决于您将如何创建 PDF。有一些扩展和 PHP 类允许您直接创建 PDF(CPDF、FPDF、TCPDF)。还有一些类和程序允许您从 HTML 文档创建 PDF。 PDF 类为您提供更精确的控制,HTML 到 PDF 类使其更易于使用,因为大多数 PHP 开发人员已经在一定程度上了解 HTML。您的情况很简单,我倾向于使用 PDF 生成类。

        您没有指定如何获取 $text1、$text2 和 $text3 的值。推测它们是由用户提供的。因此,您需要的是一个中间 PHP,它收集用户输入并创建您的 PDF。然后,您使用完整的 URL 引用您的图像生成 PHP。像下面这样的东西会起作用(使用 EZPDF+CPDF):

        <?php
        include ('class.ezpdf.php');
        $pdf = new Cezpdf();
        $pdf->ezImage('http://example.com/image.php?text1='.urlencode($text1));
        $pdf->ezStream();
        ?>
        

        我通常使用 DOMPDF,它将 HTML 文档转换为 PDF 文档。使用这个库同样简单:

        <?php
        require_once('dompdf_config.inc.php');
        $dompdf = new DOMPDF();
        $dompdf->load_html('http://example.com/image.php?text1='.urlencode($text1));
        $dompdf->render();
        $dompdr->stream();
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-16
          • 1970-01-01
          • 2017-04-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多