Php 在图像上添加文本

Php 在图像上添加文本,php,Php,在图像上添加文本。我下面有两个代码分别工作。 第一个用于通过pdo连接将图像上载到服务器和数据库。 第二个用于对静止图像进行水印 我的问题是,我想结合这两个代码,以便在上传到服务器的过程中不借助辅助工具对图像进行文本水印 全局变量。 我一直在做这件事,但无法让它工作。有人能帮我整合一下吗。谢谢 <?php include('pdo.php'); if (!isset($_FILES['image']['tmp_name'])) { echo ""; }else{

在图像上添加文本。我下面有两个代码分别工作。 第一个用于通过pdo连接将图像上载到服务器和数据库。 第二个用于对静止图像进行水印

我的问题是,我想结合这两个代码,以便在上传到服务器的过程中不借助辅助工具对图像进行文本水印 全局变量。 我一直在做这件事,但无法让它工作。有人能帮我整合一下吗。谢谢

<?php
include('pdo.php');
    if (!isset($_FILES['image']['tmp_name'])) {
    echo "";
    }else{
    $file=$_FILES['image']['tmp_name'];
    $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name= addslashes($_FILES['image']['name']);
    $image_size= getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE) {
        echo "That's not an image!";
            }else{
            move_uploaded_file($_FILES["image"]["tmp_name"],"postphoto/" . $_FILES["image"]["name"]);
            $location="postphoto/" . $_FILES["image"]["name"];
            $from=$_POST['from'];
            $time=time();
            $photos='photos';
$statement = $db->prepare('INSERT INTO text_watermark (photo,from_send) values(:photo,:from_send)');
if(!$statement->execute(array( 
':photo' => $photos,
':from_send' => $from))){
                echo 'There is problem';    
            }
            else{
            header("location: lol.php");
            exit();
            }
            }
    }
?>


<?php
      //Set the Content Type
      header('Content-type: image/jpeg');
      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg('sunset.jpg');
      // Allocate A Color For The Text
      $white = imagecolorallocate($jpg_image, 255, 255, 255);
      // Set Path to Font File
      $font_path = 'font.TTF';
      // Set Text to Be Printed On Image
      $text = "This is a sunset!";
      // Print Text On Image
      imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
      // Send Image to Browser
      imagejpeg($jpg_image);
      // Clear Memory
      imagedestroy($jpg_image);
    ?> 

如果你的代码是正确的,那只是移动块的情况。您可能想考虑当某人上传PNG时会发生什么。虽然你可能会在别处发现

<?php
include('pdo.php');
if (!isset($_FILES['image']['tmp_name'])) 
{
    echo "";
} 
else 
{
    $file       = $_FILES['image']['tmp_name'];
    $image      = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);
    if ($image_size == FALSE) 
    {
        echo "That's not an image!";
    } 
    else 
    {
        move_uploaded_file($_FILES["image"]["tmp_name"], "postphoto/" . $_FILES["image"]["name"]);
        $location  = "postphoto/" . $_FILES["image"]["name"];

        //Watermark it!
        ################

        $jpg_image = imagecreatefromjpeg($location);
        // Allocate A Color For The Text
        $white     = imagecolorallocate($jpg_image, 255, 255, 255);
        // Set Path to Font File
        $font_path = 'font.TTF';
        // Set Text to Be Printed On Image
        $text      = "This is a sunset!";
        // Print Text On Image
        imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);

        #################   

        $from      = $_POST['from'];
        $time      = time();
        $photos    = 'photos';
        $statement = $db->prepare('INSERT INTO text_watermark (photo,from_send) values(:photo,:from_send)');
        if (!$statement->execute(
                                    array(
                                        ':photo' => $photos,
                                        ':from_send' => $from
                                            )
                                    )
        ) 
        {
            echo 'There is problem';
        } 
        else 
        {
            header("location: lol.php");
            exit();
        }
    }
}

if ($jpg_image)
{
    //Set the Content Type
    header('Content-type: image/jpeg');

    // Send Image to Browser
    imagejpeg($jpg_image);
    // Clear Memory
    imagedestroy($jpg_image);
}
?> 


发布后查看您的问题。然后修复代码中完全不必要的空白。所有空白都已删除,只留下适当的代码缩进。Posthots目录中的图像没有水印。有什么建议吗