1、引言
数据库应用程序,特别是基于WEB的数据库应用程序,常会涉及到图片信息的存储和显示。通常我们使用的方法是将所要显示的图片存在特定的目录下,在数据库中保存相应的图片的名称,在JSP中建立相应的数据源,利用数据库访问技术处理图片信息。但是,如果我们想动态的显示图片,上述方法就不能满足需要了。我们必须把图片存入数据库,然后通过编程动态地显示我们需要的图片。实际操作中,可以利用JSP的编程模式来实现图片的数据库存储和显示。
2、建立后台数据库
假定处理的是图片新闻,那么我们可以建立相应的数据库及数据表对象。我们要存取的数据表结构的SQL脚本如下所示:
if ...
Imaging_ToWeb.jsp
<%@ page autoFlush="false" import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%
String ImageStr = request.getParameter("ID_Text");
if(ImageStr==null || ImageStr.equals(""))
{
response.setContentType("text/html; charset=gb2312");
%>
<HTML>
<HEAD>
<...
后台代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo imagesfile = new DirectoryInfo(Server.MapPath("./images"...
// 涉及命名空间using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.Web;using System.Configuration;
// 方法public void AddUser(string PersonName, string PersonEmail, string PersonSex, string PersonDOB, string PersonImage, string PersonImageType){ string strImageType = userImage.PostedFile.ContentType; Stream ImageStream = userImage.PostedFile.InputStream;
// 加水印-----...
$img_array = array();
$content1 = stripslashes($content1);
if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);
//echo $content1;
preg_match_all("/(src│SRC)="(http://(.+).(gif│jpg│jpeg│bmp│png))/isU",$content1,$img_array);
$img_array = array_unique(dhtmlspecialchars($img_array[2]));
set_time_limit(0);
foreach ($img_array as $key => $value) {
$get_file = file_get_contents($value);
$filetime = time();
$filepath = "../uploadfile/".date("Y",$filetime)."/".date("m",$filetime)."/";
!is_dir($filepath)...
其实,也就是采集啦。。。主要函数:
以下是PHP代码:【复制代码】 function GrabImage($url,$filename="") {
if($url=="") return false;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
$filename=date("YmdHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
} 获取一张图片的代码...
将index.html和upload.php文件保存到apache工作目录,例子使用安装目录D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/
AJAX 客户端页面代码: index.html
<html>
<body>
<h1>Ajax file upload sample</h1><br/><input id="uplaod" name="btn_send" type="button" value="上传测试"/>
<div id=result></div>
<PRE class=js name="code"><SCRIPT LANGUAGE=JavaS...
<?php//突破防盗链的显示图片标签[simg],PHP程序部分
ob_start();
$img=$_GET['url'];
$host=$path=str_replace('http://','',$img);
$host=explode('/',$host);
$host=$host[0];
$path=strstr($path,'/');
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp)
{
@fputs($fp, "GET $path HTTP/1.1rn");
@fputs($fp, "Host: $hostrn");
@fputs($fp, "Accept: */*rn");
@fputs($fp, "Referer: http://$host/rn");
@fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)rn");
@fputs($fp, "Connect...
<?php$url='../images'; //图片地址,只可以用./you/images这样的路径$files=array();if ($handle=opendir("$url")) { while(false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(substr($file,-3)=='gif' || substr($file,-3)=='jpg') $files[count($files)] = $file; } } }closedir($handle); $random=rand(0,count($files)-1);if(substr($files[$random],-3)=='gif') header("Content-type: image/gif");elseif(substr($files[$random],-3)==...
如果对PHP的GD库比较熟悉,看懂这篇文章一点都不难了!
<?php
/******************************************************************************
参数说明:
$max_file_size : 上传文件大小限制, 单位BYTE
$destination_folder : 上传文件路径
$watermark : 是否附加水印(1为加水印,其他为不加水印);
使用说明:
1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
2. 将extension_dir =改为你的php_gd2.dll所在目录;
***********...
二、给图片增加水印的php文件,包含实际运行代码,支持图片和文字水印
<?php
/*
* 功能:PHP图片水印 (水印支持图片或文字)
* 参数:
* $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterPos 水印位置,有10种状态,0为随机位置;
* 1为顶端居左,2为顶端居中,3为顶端居右;
* 4为中部居左,5为中部居中,6为中部居右;
* 7为底端居左,8为底端居中,9为底端居右;
* $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
* $textFont 文字大小,值为1、2、3、...
一、PHP给图片增加水印的类
<?php
Class Gimage{
var $src_image_name = ""; //输入图片的文件名(必须包含路径名)
var $jpeg_quality = 90; //jpeg图片质量
var $save_image_file = ''; //输出文件名
var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)
var $wm_image_pos = 1; //水印图片放置的位置
// 0 = middle
// 1 = top left
// 2 = top right
// 3 = bottom right
// 4 = bottom left
// 5 = top middle
// 6 = middle right
// 7 = bottom middle
// 8 = middle left
//other = 3
var $wm_image_transition = 20; //水印图片与原图片的融合度 (1=100)
...
<?php//小猪会气功session_start();session_register('code');$width = "70";//图片宽$height = "25";//图片高$len = "4";//生成几位验证码$bgcolor = "#ffffff";//背景色$noise = true;//生成杂点$noisenum = 200;//杂点数量$border = false;//边框$bordercolor = "#000000";$image = imageCreate($width, $height);$back = getcolor($bgcolor);imageFilledRectangle($image, 0, 0, $width, $height, $back);$size = $width/$len;if($size>$height) $size=$height;$left = ($width-$len*($size+$size/10))/$size;for ($i=0; $i<$len; $i++){ $randtext = rand(0, 9); &...
PHP图片验证码原代码,需支持gd2.dll扩展,需修改PHP.ini后重启IIS or ap 调用方法<img src="this.php">
<?PHP session_start(); session_register('SafeCode'); $type = 'gif'; $width= 40; $height= 16; header("Content-type: image/".$type); srand((double)microtime()*1000000); $randval = randStr(4,""); if($type!='gif' && function_exists('imagecreatetruecolor')){ $im = @imagecreatetruecolor($width,$height); }else{ $im = @imagecreate($width,$height); } $r = Array(225,211,255,223); $g = Array(225,236,237,215); $b = Array(225,236,166,125); $key = rand(0,3); $backColor = I...
做了一段手机游戏移植。有时候碰到游戏的资源文件被做成一个数据文件。想提出来很费事,所以自己写了一个程序来处理。方法就是自定义几个字节。与PNG格式的图片字节相匹配。这样把PNG图片信息提出来 以下是JAVA代码:【复制】
Code:/* *数据文件转png图片程序 *@author k7sem *05年10月12日修改 */ import java.io.*; class transfer { byte a1 = (byte)0x89; //定义PNG文件头部的匹配参数 byte a2 = (byte)0x50; byte a3 = (byte)0x4e; byte a4 = (byte)0x47; byte a5 = (byte)0x0d; byte a6 = (byte)0x0a; byte a7 = (byte)0x1a; byte a8 = (byte)0x0a; //------------png file end--------------//png文件尾 byte ...
图片文件格式转换最核心的问题就是要解决各种图片格式的编码和解码,推荐用jimi包,可从http://java.sun.com/products/jimi/下载该包及其源码和demo。 需要注意的是:jimi提供了对几乎所有图片格式的解码支持,但是为提供gif和tif/tiff格式的编码api,gif编码问题在网上可以找到县相关的源码,tiff好像jai:http://java.sun.com/products/java-media/jai/index.jsp提供了它的编码,不过我没有用jai,暂时用jpeg编码代替了。 察看jimi的源码,在com.sun.jimi.core中是jimi的核心处理api。public void toJPG(String source, String dest, int quality) {if (dest == null || dest.trim().equals("")) dest = source;if (!dest...