在图片上加文字是论坛,博客,新闻网站上最喜欢用的功能,防止盗图。这里看看代码是如何实现的。

首先还是upload_image.php这个文件,注意这里的caption文本框中输入的内容最终会写到图片上面

<?php
//修改图片效果
$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');
mysql_select_db('moviesite',$db) or die(mysql_error($db));
//上传文件的路径
$dir = 'D:\Serious\phpdev\test\images';
//设置环境变量
//putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "C:\Windows\Fonts\arial.ttf"; //upload_image.php页面传递过来的参数,如果是上传图片
if($_POST['submit'] == 'Upload')
{
if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_INI_SIZE:
die('The uploaded file exceeds the upload_max_filesize directive');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
break;
case UPLOAD_ERR_PARTIAL:
die('The uploaded file was only partially uploaded');
break;
case UPLOAD_ERR_NO_FILE:
die('No file was uploaded');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('The server is missing a temporary folder');
break;
case UPLOAD_ERR_CANT_WRITE:
die('The server fail to write the uploaded file to the disk');
break;
case UPLOAD_ERR_EXTENSION:
die('The upload stopped by extension');
break;
}
}
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-d');
list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
$error = 'The file you upload is not a supported filetype';
switch($type)
{
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error);
break;
default:
break;
}
$query = 'insert into images(image_caption,image_username,image_date) values("'.$image_caption.'" , "'.$image_username.'","'.$image_date.'")';
$result = mysql_query($query,$db) or die(mysql_error($db));
$last_id = mysql_insert_id(); // $imagename = $last_id.'.jpg';
// imagejpeg($image,$dir.'/'.$imagename);
// imagedestroy($image); $image_id = $last_id;
imagejpeg($image , $dir.'/'.$image_id.'.jpg');
imagedestroy($image);
}
else //如果图片已经上传,则从数据库中取图片名字
{
$query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
$result = mysql_query($query,$db) or die(mysql_error($db));
extract(mysql_fetch_assoc($result));
list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.'.jpg');
} //如果是保存图片
if($_POST['submit'] == 'Save')
{
if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].'.jpg'))
{
$image = imagecreatefromjpeg($dir.'/'.$_POST['id'].'.jpg');
}
else
{
die('invalid image specified');
}
$effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
switch($effect)
{
case IMG_FILTER_NEGATE:
imagefilter($image , IMG_FILTER_NEGATE); //将图像中所有颜色反转
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image , IMG_FILTER_GRAYSCALE); //将图像转换为灰度的
break;
case IMG_FILTER_EMBOSS:
imagefilter($image , IMG_FILTER_EMBOSS); //使图像浮雕化
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像
break;
} if(isset($_POST['emb_caption']))
{
imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
} imagejpeg($image , $dir.'/'.$_POST['id'].'.jpg' , 100);
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>Your image has been saved!</h1>
<img src="data:images/<?php echo $_POST['id'];?>.jpg" alt="" />
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1>
<p>Here is the picture you just uploaded to your servers:</p>
<!--<img src="data:images/<?php echo $imagename;?>" alt="" style="float:left;" />-->
</body>
</html>
<?php
if($_POST['submit'] == 'Upload')
{
$imagename = 'images/'.$image_id.'.jpg';
}
else
{
$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];
if(isset($_POST['emb_caption']))
{
$imagename .= '&capt='.urlencode($image_caption);
}
}
?>
<img src="<?php echo $imagename;?>" style="float:left;" alt="" />
<table>
<tr>
<td>Image save as:</td>
<td><?php $image_id?></td>
</tr>
<tr>
<td>Height:</td>
<td><?php echo $height;?></td>
</tr>
<tr>
<td>Widht:</td>
<td><?php echo $width;?></td>
</tr>
<tr>
<td>Upload date:</td>
<td><?php echo $image_date;?></td>
</tr>
</table>
<p>You may apply a special effect to your image from the list of option below.
Note:saving an image with any of the filters applied <em>can be undone</em>
</p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $image_id;?>"/>
Filter:<select name="effect" id="">
<option value="-1">None</option>
<?php
echo '<option value="'.IMG_FILTER_GRAYSCALE.'" ';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GRAYSCALE)
{
echo 'selected="selected"';
}
echo ' >Black and white</option>'; echo '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GAUSSIAN_BLUR)
{
echo ' selected="selected"';
}
echo '>Blur</option>'; echo '<option value="'.IMG_FILTER_EMBOSS.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_EMBOSS)
{
echo 'selected="selected"';
}
echo '>Emboss</option>'; echo '<option value="'.IMG_FILTER_NEGATE.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_NEGATE)
{
echo 'selected="selected"';
}
echo '>Negative</option>';
?>
</select><br />
<?php
echo '<input type="checkbox" name="emb_caption"';
if(isset($_POST['emb_caption']))
{
echo ' checked="checked"';
}
echo ' />Embed caption in image?';
?>
<input type="submit" value="Preview" name="submit" /><br /><br />
<input type="submit" value="Save" name="submit" /> </div>
</form>
<?php
}
?>

注意这里有个问题,选择字体的时要加上绝对路径并且带上后缀.tff,否则的话是看不到图片的,下面的代码是image_effect.php

<?php
//修改图片效果
$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');
mysql_select_db('moviesite',$db) or die(mysql_error($db));
//上传文件的路径
$dir = 'D:\Serious\phpdev\test\images';
//设置环境变量
//putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "C:\Windows\Fonts\arial.ttf"; //upload_image.php页面传递过来的参数,如果是上传图片
if($_POST['submit'] == 'Upload')
{
if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_INI_SIZE:
die('The uploaded file exceeds the upload_max_filesize directive');
break;
case UPLOAD_ERR_FORM_SIZE:
die('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
break;
case UPLOAD_ERR_PARTIAL:
die('The uploaded file was only partially uploaded');
break;
case UPLOAD_ERR_NO_FILE:
die('No file was uploaded');
break;
case UPLOAD_ERR_NO_TMP_DIR:
die('The server is missing a temporary folder');
break;
case UPLOAD_ERR_CANT_WRITE:
die('The server fail to write the uploaded file to the disk');
break;
case UPLOAD_ERR_EXTENSION:
die('The upload stopped by extension');
break;
}
}
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-d');
list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
$error = 'The file you upload is not a supported filetype';
switch($type)
{
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error);
break;
default:
break;
}
$query = 'insert into images(image_caption,image_username,image_date) values("'.$image_caption.'" , "'.$image_username.'","'.$image_date.'")';
$result = mysql_query($query,$db) or die(mysql_error($db));
$last_id = mysql_insert_id(); // $imagename = $last_id.'.jpg';
// imagejpeg($image,$dir.'/'.$imagename);
// imagedestroy($image); $image_id = $last_id;
imagejpeg($image , $dir.'/'.$image_id.'.jpg');
imagedestroy($image);
}
else //如果图片已经上传,则从数据库中取图片名字
{
$query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
$result = mysql_query($query,$db) or die(mysql_error($db));
extract(mysql_fetch_assoc($result));
list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.'.jpg');
} //如果是保存图片
if($_POST['submit'] == 'Save')
{
if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].'.jpg'))
{
$image = imagecreatefromjpeg($dir.'/'.$_POST['id'].'.jpg');
}
else
{
die('invalid image specified');
}
$effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
switch($effect)
{
case IMG_FILTER_NEGATE:
imagefilter($image , IMG_FILTER_NEGATE); //将图像中所有颜色反转
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image , IMG_FILTER_GRAYSCALE); //将图像转换为灰度的
break;
case IMG_FILTER_EMBOSS:
imagefilter($image , IMG_FILTER_EMBOSS); //使图像浮雕化
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像
break;
} if(isset($_POST['emb_caption']))
{
imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
} imagejpeg($image , $dir.'/'.$_POST['id'].'.jpg' , 100);
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>Your image has been saved!</h1>
<img src="data:images/<?php echo $_POST['id'];?>.jpg" alt="" />
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1>
<p>Here is the picture you just uploaded to your servers:</p>
<!--<img src="data:images/<?php echo $imagename;?>" alt="" style="float:left;" />-->
</body>
</html>
<?php
if($_POST['submit'] == 'Upload')
{
$imagename = 'images/'.$image_id.'.jpg';
}
else
{
$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];
if(isset($_POST['emb_caption']))
{
$imagename .= '&capt='.urlencode($image_caption);
}
}
?>
<img src="<?php echo $imagename;?>" style="float:left;" alt="" />
<table>
<tr>
<td>Image save as:</td>
<td><?php $image_id?></td>
</tr>
<tr>
<td>Height:</td>
<td><?php echo $height;?></td>
</tr>
<tr>
<td>Widht:</td>
<td><?php echo $width;?></td>
</tr>
<tr>
<td>Upload date:</td>
<td><?php echo $image_date;?></td>
</tr>
</table>
<p>You may apply a special effect to your image from the list of option below.
Note:saving an image with any of the filters applied <em>can be undone</em>
</p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $image_id;?>"/>
Filter:<select name="effect" id="">
<option value="-1">None</option>
<?php
echo '<option value="'.IMG_FILTER_GRAYSCALE.'" ';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GRAYSCALE)
{
echo 'selected="selected"';
}
echo ' >Black and white</option>'; echo '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GAUSSIAN_BLUR)
{
echo ' selected="selected"';
}
echo '>Blur</option>'; echo '<option value="'.IMG_FILTER_EMBOSS.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_EMBOSS)
{
echo 'selected="selected"';
}
echo '>Emboss</option>'; echo '<option value="'.IMG_FILTER_NEGATE.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_NEGATE)
{
echo 'selected="selected"';
}
echo '>Negative</option>';
?>
</select><br />
<?php
echo '<input type="checkbox" name="emb_caption"';
if(isset($_POST['emb_caption']))
{
echo ' checked="checked"';
}
echo ' />Embed caption in image?';
?>
<input type="submit" value="Preview" name="submit" /><br /><br />
<input type="submit" value="Save" name="submit" /> </div>
</form>
<?php
}
?>

这里如果我们写成下面这样

putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "arial";

是看不到效果的,这里顺便提一下,火狐是最佳的开发工具,怎么讲呢,看看下面的对比吧。

火狐的提示是这样的:

图片没有截完整提示是:The image “http://localhost:81/test/image_effect.php?id=31&e=-1&capt=you+are+big+bitch” cannot be displayed because it contains errors.

谷歌浏览器的显示如下:

一个未能正确显示的图片,什么信息都没有。

再来看看大IE的,如下:

也能看到错误信息,但是一大堆乱码啊!

所以说开发人员好帮手还是火狐。

php给图片加文字的更多相关文章

  1. PHP给图片加文字水印

    <?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = ...

  2. Java图片加文字水印

    Java图片加文字水印 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.I ...

  3. C#给图片加文字水印

    public class TxtWaterMark { public enum WaterPositionMode { LeftTop,//左上 LeftBottom,//左下 RightTop,// ...

  4. ASP.NET(C#)图片加文字、图片水印,神啊,看看吧

    ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...

  5. C#给图片加文字和图片的水印

    /// <summary> /// WaterMark 的摘要说明 /// </summary> /// 图片加水印 /// <param name="strC ...

  6. Android给图片加文字和图片水印

    我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * 欢迎访问水寒的个人博客:ht ...

  7. 安卓使用TextView实现图片加文字说明

    背景:通讯录列表,每个单元格显示头像+名字,且头像显示圆形 方案一:ImageView + TextView 方案二:只用TextView + drawableLeft 属性 <TextView ...

  8. PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)

    $dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...

  9. PHP给图片加文字(水印)

    准备工作: 代码: <?php header("Content-type: image/jpeg"); //浏览器输出,如不需要可去掉此行 $im = @imagecreat ...

随机推荐

  1. archlinux安装gnome的一些坑随记

    问题1:网络设置无法查看,提示缺少NetworkManager 解决:安装networkmanager库,因为gnome调用的是networkmanager这个软件来管理网络的.然后要启动它:sudo ...

  2. *2.3.2_加入env

    在验证平台中加入reference model.scoreboard等之前,思考一个问题:假设这些组件已经定义好了,那么在验证平台的什么位置对它们进行实例化呢?在top_tb中使用run_test进行 ...

  3. 开源高性能网络库Libevent的简介

    Libevent是什么? Libevent 是一个用C语言编写的.轻量级的开源高性能网络库. 官网:http://libevent.org/ 优点: (1)事件驱动,高性能 (2)轻量级,专注于网络 ...

  4. eclipse下JAVA的搭建

    练手JAVA用eclipse比android studio快很多,android studio啥都好,就是太慢 参考资料:http://blog.csdn.net/21aspnet/article/d ...

  5. 【10】Quartz.net 定时服务实例

    一.安装nuget包 Install-Package Quartz Install-Package Common.Logging.Log4Net1211 Install-Package log4net ...

  6. 1.Java设计模式-工厂模式

    1.简单工厂模式(Factory Method) 常用的工厂模式是静态工厂模式,利用static修饰方法,作为一种类似于常见的工具类Utils等辅助效果,一般情况下工厂类不需要实例化. //1.定义一 ...

  7. jenkins在Linux 下安装部署

      这里介绍两种方法,一种方法将最新版jenkins加入到yum源,另外一种是下载指定版本的rpm包 系统centos6 自带jdk1.7 一 安装jenkins wget -O :下载并以不同的文件 ...

  8. 关于二进制和字符串及base64格式

    字符串转二进制可以直接转,而二进制转字符串不可以,其中间包含的各种特殊符号,转成字符串时会出现问题,需要将二进制进行base64编码,并且需要在结尾加上#0表示结尾,然后转成字符串.

  9. javaweb项目中绝对路径的写法理解

    Tomcat的默认访问路径为http://localhost:8080,后需添加项目路径. 请求转发,是转发到本项目中的其他文件,所以在默认访问路径中添加了本项目的项目路径,故可以省略项目名称: re ...

  10. C/C++:判断机器是32位还是64位

    要求是不使用sizeof,一开始写了个看似可以,但是有问题的方法: long* a = NULL; ; int n = (char*)b - (char*)a; 这个方法等价于sizeof(long) ...