首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP中上传图像时在图像上添加文本

问在PHP中上传图像时在图像上添加文本
EN

Stack Overflow用户
提问于 2012-10-23 00:26:29
回答 2查看 4K关注 0票数 1

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

这是我用来上传图片的代码,我正在考虑在上传字段下面添加一个文本字段,这样用户在该框中写的任何内容都会作为imagetext打印在图像上……那么我怎么用php做到这一点呢?

代码语言:javascript
复制
<?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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-23 00:51:49

你可以试试

代码语言:javascript
复制
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();
            }
        }
    }
}

输出

​

​

代码语言:javascript
复制
           Uploaded                               Final
票数 4
EN

Stack Overflow用户

发布于 2012-10-23 00:30:48

首先,检查是否安装了GD扩展,然后,

使用GD函数

代码语言:javascript
复制
//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/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13015656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档