【问题标题】:Place image in another image with PHP使用 PHP 将图像放置在另一个图像中
【发布时间】:2011-09-02 09:37:18
【问题描述】:

我有一个图像,其上的文本是通过 php 表单动态生成的。我将徽标图像的位置保存到 mysql 数据库中的变量中。有没有办法获取此图像并将其应用于图像中的固定位置?如果需要,必须将其调整为更小以适合此图像区域。

我已经有一个如下所示的脚本:

        $img = imagecreatefromjpeg('coupon/coupontemplate.jpg');
        $textColor = imagecolorallocate($img, 0, 0, 0); // black text

        //Write first the coupon title
        imagefttext($img, 16, 0, 20, 34, $textColor, 'coupon/arialbd.ttf', $title);

        //Then write the coupon description
        imagettftextbox($img, 13, 0, 20, 45, $textColor, 'coupon/arial.ttf', $description, 440);
        //If the checkbox to include logo is checked...
        if ($_POST['clogo'] == 'y') {

            $logo = $row['imagecompany'];
            $logo_file = "../directory/memberimages/$logo";
            $logo_file_type = getimagesize($logo_file);

            if ($logo_file_type['mime'] == 'image/jpg') {
                $logoImage = imagecreatefromjpeg($logo_file);
            } else if ($logo_file_type['mime'] == 'image/gif') {
                $logoImage = imagecreatefromgif($logo_file);
            } else if ($logo_file_type['mime'] == 'image/png') {
                $logoImage = imagecreatefrompng($logo_file);
            }

            }

        // Output image to the browser
        //header('Content-Type: image/jpeg');
        //imagejpeg($img);

        // Or save to file
        imagejpeg($img, 'my-text.jpg');
        imagedestroy($img);

    }
//}

任何人都知道如何做到这一点 - 从指定位置获取图像并将其放在另一个图像上?谢谢!

【问题讨论】:

  • 意味着你想在每张生成的图片上加上标志?图像上的图像。对吗??
  • 顺便说一下,对图像执行此操作的通用名称是“水印”——这可能会对您有所帮助 find more resources

标签: php image gd


【解决方案1】:

可以像这样合并图像:reference

<?php
 # If you don't know the type of image you are using as your originals.
 $image = imagecreatefromstring(file_get_contents($your_original_image);
 $frame = imagecreatefromstring(file_get_contents($your_frame_image));

 # If you know your originals are of type PNG.
 $image = imagecreatefrompng($your_original_image);
 $frame = imagecreatefrompng($your_frame_image);

 imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100);

 # Save the image to a file
 imagepng($image, '/path/to/save/image.png');

 # Output straight to the browser.
 imagepng($image);

【讨论】:

  • 您知道该帧是否可能是我已经生成的正在生成的图像吗? $img 变量?
  • 是的,如果它已经在你的文件系统中,你可以:如果你使用 imagecreatefromstring 函数传递这个 file_get_contents($src) ... $src 可以是你已经生成的图像的路径。否则,如果您已经知道已生成文件的图像类型,则可以使用 imagecreatefrompng/jpeg/gif 函数并再次将路径/url 传递给已生成的图像
【解决方案2】:

我认为您正在寻找 imagecopyresampled()imagecopyresized()

【讨论】:

    【解决方案3】:

    您可以使用imagecopy 的功能。快速搜索发现这篇文章可能会对您有所帮助。

    http://forums.bit-tech.net/showthread.php?t=117270

    【讨论】:

      【解决方案4】:

      你想要的功能是imagecopy:

      bool imagecopy ( 资源 $dst_im , 资源 $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

      http://php.net/manual/en/function.imagecopy.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        • 1970-01-01
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 2013-01-29
        相关资源
        最近更新 更多