在图像上覆盖文本并上传到服务器


Overlaying text on an image and uploading to server

我是PHP新手,在将图像上传到服务器之前,正在努力添加文本覆盖。这是我当前的功能,但它不添加文本到图像。

function image_file_upload($conn) {
$target_dir = "../users/images/";
$caption = $_POST['comment'];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$target_file = $target_dir . basename($imgFile);
$uploadOk = 1;
$font_color = bcca;
$background_color = abbc;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$ext = explode('.',$imgFile);
$file = $target_dir . uniqid() . "." . $ext[1];
$uid = 4;
// Check if image file is a actual image or fake image
$check = getimagesize($tmp_dir);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($tmp_dir, $file)) {
                $jpg_image = imagecreatefromjpeg($file);
                $white = imagecolorallocate($jpg_image, 255, 255, 255);
                $font_path = 'simple.ttf';
                $text = "This is a Sunset!";
                imagettftext($jpg_image, 10, 0, 0, 0, $white, $font_path, $text);
                imagejpeg($jpg_image,$file);
       $date = date('Y-m-d H:i:s');
       insert_into_post($conn, $uid, 1, $date, $file, $background_color, $font_color, 0 , 0, "image", $caption);
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

}

您的代码中遗漏了几个步骤。检查下面的注释代码,看看它是否符合你的要求:

    <?php
        function image_file_upload($conn) {
            // IF WE THERE WAS NO FILE UPLOADED, WE JUST REPORT THAT AND TERMINATE THE FUNCTION IMMEDIATELY:
            if(!isset($_FILES['user_image']) && empty($_FILES['user_image'])){
                echo "Please, upload a File using the Form....";
                return null;                    
            }
            $target_dir         = "../users/images/";
            $caption            = $_POST['comment'];
            $imgFile            = $_FILES['user_image']['name'];
            $tmp_dir            = $_FILES['user_image']['tmp_name'];
            $target_file        = $target_dir . basename($imgFile);
            $font_color         = "#bcca00";      //<== YOUR COLOR SHOULD BE A VALID HEX OR RGB OR HSLA COLOR VALUE
            $background_color   = "#abbc00";      //<== YOUR COLOR SHOULD BE A VALID HEX OR RGB OR HSLA COLOR VALUE
            $fileInfo           = new SplFileInfo($imgFile);
            $ext                = $fileInfo->getExtension();
            $file               = $target_dir . uniqid() . "." . $ext;
            $uid                = 4;
            $check              = getimagesize($tmp_dir);
            // CHECK THAT getimagesize RETURNS AN ARRAY OF DATA
            if(!empty($check)){                 
                // CHECK THAT THIS WAS AN IMAGE FILE USING THE MIME PROPERTY OF THE $check ARRAY...
                if (stristr($check['mime'], "image/")) {
                    if (move_uploaded_file($tmp_dir, $file)) {
                        // GRAB A COPY OF THE UPLOADED IMAGE - SO THAT WE CAN ADD TEXT TO IT...
                        $jpg_image  = imagecreatefromjpeg($file);
                        $white      = imagecolorallocate($jpg_image, 255, 255, 255);
                        $red        = imagecolorallocate($jpg_image, 255, 0, 0); //<== RED: JUST FOR TESTING PURPOSES...
                        $font_path  = 'simple.ttf';
                        $text       = "This is a Sunset!";
                        $date       = date('Y-m-d H:i:s');
                        // ADD TEXT TO THE IMAGE... BE CAREFUL WITH THE X, Y  LOCATION...
                        // YOU MAY NEED TO PLAY AROUND WITH IT A BIT ESPECIALLY CONSIDERING THE FONT-SIZE...
                        imagettftext($jpg_image, 50, 0, 50, 50, $red, $font_path, $text);
                        // SAVE BACK THE IMAGE WITH SOME TEXT SUPERIMPOSED ON IT...
                        imagejpeg($jpg_image, $file);
                        // INSERT SOME DATA RELATED TO THE UPLOADED IMAGE TO THE DATABASE...
                        insert_into_post($conn, $uid, 1, $date, $file, $background_color, $font_color, 0, 0, "image", $caption);
                    }else {
                        echo "Sorry, there was an error uploading your file.";
                    }
                }else {
                    echo "File is not an image.";
                }
            }
        }
        // CHECK THAT THE FORM WAS SUBMITTED BEFORE RUNNING THE UPLOAD PROCESS...
        if(isset($_POST["upload"])){
            image_file_upload($connectionHandle);
        }
        ?>
    <html>
    <form method="post" enctype="multipart/form-data" action="">
        <input type="file" name="user_image" value="" id="user_image" /><br /><br />
        <input type="submit" name="upload" value="upload" id="upload" />
    </form>
    </html>