1

我有一个像 Quickmeme.com 这样的图片网站,我想在图片上添加文字。我目前让用户手动上传图片,在 Windows 中使用 Paint 程序编写评论,这是一个坏主意,我需要帮助......

这是我用于上传图像的代码,我正在考虑在上传字段下方添加一个文本字段,以便用户在该框中写入的内容将作为图像文本打印在图像上......那么我该怎么做用php?

<?php if(isset($_POST["upload"])){

        $tmp_name = $_FILES["file"]["tmp_name"];
        $file_name = basename($_FILES["file"]["name"]);
        $random = rand(1, 9999999999);
        $directory = "Uploads/" . $random . $file_name;
        $time = strftime("%H:%M:%S", time());
        $date = strftime("%Y-%m-%d", time());

            if(move_uploaded_file($tmp_name, $directory)){
                if(mysql_query("")){
                    $query = mysql_query(""); $fetch = mysql_fetch_array($query);
                    if(mysql_query("")){
                        header("Location: index.php");
                        exit;
                    }
                }
            }


      } ?>

这是我的网站http://www.picturepunches.net

4

2 回答 2

4

你可以试试

if (isset($_POST["upload"])) {

    $tmp_name = $_FILES["file"]["tmp_name"];
    $file_name = basename($_FILES["file"]["name"]);
    $random = rand(1, 9999999999);
    $directory = "Uploads/" . $random . $file_name;
    $time = strftime("%H:%M:%S", time());
    $date = strftime("%Y-%m-%d", time());

    switch (strtolower(pathinfo($file_name, PATHINFO_EXTENSION))) {
        case "jpg" :
            $im = imagecreatefromjpeg($_FILES["file"]["tmp_name"]);
            break;
        case "gif" :
            $im = imagecreatefromgif($_FILES["file"]["tmp_name"]);
            break;
        case "png" :
            $im = imagecreatefrompng($_FILES["file"]["tmp_name"]);
            break;

        default :
            trigger_error("Error Bad Extention");
            exit();
            break;
    }

    $font = 'verdana.ttf';
    $grey = imagecolorallocate($im, 128, 128, 128);
    $red = imagecolorallocate($im, 255, 0, 0);
    // Add some shadow to the text
    imagettftext($im, 10, 0, 11, 20, $grey, $font, $date);
    imagettftext($im, 10, 0, 10, 35, $grey, $font, $time);
    imagettftext($im, 10, 0, 10, 50, $red, $font, $random);

    // imagepng($im);
    imagedestroy($im);

    if (move_uploaded_file($tmp_name, $directory)) {
        if (mysql_query("")) {
            $query = mysql_query("");
            $fetch = mysql_fetch_array($query);
            if (mysql_query("")) {
                header("Location: index.php");
                exit();
            }
        }
    }
}

输出

在此处输入图像描述在此处输入图像描述

           Uploaded                               Final
于 2012-10-22T16:51:48.720 回答
2

首先,检查您是否安装了 GD 扩展,接下来,

使用 GD 函数

//Loading the file
$rImg = ImageCreateFromJPEG("MyPicture.jpg");

//Font Color (black in this case)
$color = imagecolorallocate($rImg, 0, 0, 0);

//x-coordinate of the upper left corner. 
$xPos = 100;
//y-coordinate of the upper left corner. 
$yPos = 30;

//Writting the picture
imagestring($rImg,5,$xPos,$yPos,"My text in the picture",$color);

//The new file with the text
header('Content-type: image/jpeg');
imagejpeg($rImg, NULL, 100);

你可以使用本教程

http://blog.doh.ms/2008/02/12/adding-text-to-images-in-real-time-with-php/

于 2012-10-22T16:30:48.230 回答