1

Quickmeme.com のような写真の Web サイトがあり、画像の上にテキストを追加したいと考えています。私は現在、ユーザーが写真を手動でアップロードできるようにしており、Windows のペイント プログラムを使用してコメントを書いていますが、これは悪い考えであり、助けが必要です ...

画像のアップロードに使用するコードは次のとおりです。アップロードフィールドの下にテキストフィールドを追加することを考えているので、ユーザーがそのボックスに書いたものはすべて画像テキストとして画像に印刷されます...どうすればそれを行うことができますか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 に答える