Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将文本覆盖在图像上并上载到服务器_Php_Html_Css - Fatal编程技术网

Php 将文本覆盖在图像上并上载到服务器

Php 将文本覆盖在图像上并上载到服务器,php,html,css,Php,Html,Css,我是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']; $tar

我是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>




您的代码中缺少几个步骤。查看下面的注释代码,看看它是否符合您的要求:

    <?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>