我有像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;
}
}
}
} ?>
答案 0 :(得分: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
答案 1 :(得分: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/