存档

文章标签 ‘缩略图’

PHP中图片上传、生成缩略图并添加水印

2009年5月11日

在PHP的应用中,通常需要进行图片的上传,并根据上传的图片文件生成指定尺寸的缩略图。这里本博在看之前的文稿时,看到一个从前的实例,这里将其与各位分享,希望能够给初学者一点启示。
废话就不讲了,上代码为王道!

<?php
	$uptypes=array(								//允许上传文件类型列表
	    'image/jpg',
	    'image/jpeg',
	    'image/png',
	    'image/pjpeg',
	    'image/gif',
	    'image/bmp',
	    'image/x-png'
	);
	$max_file_size = 2000000;     					//上传文件大小限制, 单位BYTE
	$destination_folder = "uploadimg/";					//上传文件路径
	$watermark = 1;      							//是否附加水印(1为加水印,其他为不加水印)
	$watertype = 1;      							//水印类型(1为文字,2为图片)
	$waterposition = 1;     							//水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中)
	$waterstring = "http://www.phpcoding.cn/";  			//水印字符串
	$waterimg = "water.gif";    						//水印图片
	$imgpreview = 1;      							//是否生成预览图(1为生成,其他为不生成)
	$imgpreviewsize = 1/3;    						//缩略图比例

if ($_SERVER['REQUEST_METHOD'] == 'POST'){			//判断是否用户提交
	if (!is_uploaded_file($_FILES["upfile"][tmp_name]))		//判断是否存在文件
	{
	     echo "图片不存在!";
	     exit;
	}

	$file = $_FILES["upfile"];
	if($max_file_size < $file["size"])						//检查文件大小
	{
	    echo "文件太大!";
	    exit;
	}

	if(!in_array($file["type"], $uptypes))					//检查文件类型
	{
	    echo "文件类型不符!".$file["type"];
	    exit;
	}

	if(!file_exists($destination_folder))					//判断上传文件夹
	{
	    mkdir($destination_folder);
	}

	$filename=$file["tmp_name"];						//文件名
	$image_size = getimagesize($filename);				//获取上传文件尺寸
	$pinfo=pathinfo($file["name"]);
	$ftype=$pinfo['extension'];
	$destination = $destination_folder.time().".".$ftype;

	if (file_exists($destination) && $overwrite != true)		//判断文件是否存在
	{
	    echo "同名文件已经存在了";
	    exit;
	}

	if(!move_uploaded_file ($filename, $destination))		//移动上传的文件
	{
	    echo "移动文件出错";
	    exit;
	}

	$pinfo=pathinfo($destination);
	$fname=$pinfo[basename];
	echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
	echo " 宽度:".$image_size[0];
	echo " 长度:".$image_size[1];
	echo "<br> 大小:".$file["size"]." bytes";

	if($watermark==1) {									//判断是否添加水印
		$iinfo=getimagesize($destination,$iinfo);
		$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
		$white=imagecolorallocate($nimage,255,255,255);
		$black=imagecolorallocate($nimage,0,0,0);
		$red=imagecolorallocate($nimage,255,0,0);
		imagefill($nimage,0,0,$white);

		switch ($iinfo[2])								//缩略图类别
		{
			case 1:
				$simage =imagecreatefromgif($destination);
				break;
			case 2:
				$simage =imagecreatefromjpeg($destination);
				break;
			case 3:
				$simage =imagecreatefrompng($destination);
				break;
			case 6:
				$simage =imagecreatefromwbmp($destination);
				break;
			default:
				die("不支持的文件类型");
				exit;
		}

		imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
		imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);

		switch($watertype)							//水印类别
		{
		    case 1:  								//加水印字符串
		    imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
		    break;
		    case 2:   								//加水印图片
		    $simage1 =imagecreatefromgif("xplore.gif");
		    imagecopy($nimage,$simage1,0,0,0,0,85,15);
		    imagedestroy($simage1);
		    break;
		}

		switch ($iinfo[2])
		{
			case 1:
			//imagegif($nimage, $destination);
			imagejpeg($nimage, $destination);
			break;
			case 2:
			imagejpeg($nimage, $destination);
			break;
			case 3:
			imagepng($nimage, $destination);
			break;
			case 6:
			imagewbmp($nimage, $destination);
			//imagejpeg($nimage, $destination);
			break;
		}

		//覆盖原上传文件
		imagedestroy($nimage);
		imagedestroy($simage);
	}

	if($imgpreview==1)								//预览图片
	{
		echo "<br>图片预览:<br>";
		echo "<img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
		echo " alt="图片预览: 文件名:".$destination." 上传时间:">";
	}
}
?>
<html>
<head>
<title>图片上传程序</title>
<style type="text/css">
<!--
body{
     font-size: 9pt;
}
input{
     background-color: #66CCFF;
     border: 1px inset #CCCCCC;
}
-->
</style>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="upform">
  上传文件:
  <input name="upfile" type="file">
  <input type="submit" value="上传"><br>
  允许上传的文件类型为:<?=implode(', ',$uptypes)?>
</form>
</body>
</html>

PHP教程 , , ,

动态生成缩略图

2008年10月7日
<?php
/***************************************
按最小裁剪生成的缩图类,动态生成缩略图, 按最佳比例生成
 ***************************************/
class CreatMiniature {
	//公共变量
	var $srcFile = ""; //原图
	var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件
	var $im = ""; //临时变量
	var $srcW = ""; //原图宽
	var $srcH = ""; //原图高

	//设置变量及初始化
	function SetVar($srcFile, $echoType) {
		$this->srcFile = $srcFile;
		$this->echoType = $echoType;

		$info = "";
		$data = GetImageSize ( $this->srcFile, $info );
		switch ($data [2]) {
			case 1 :
				if (! function_exists ( "imagecreatefromgif" )) {
					echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
					exit ();
				}
				$this->im = ImageCreateFromGIF ( $this->srcFile );
				break;
			case 2 :
				if (! function_exists ( "imagecreatefromjpeg" )) {
					echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
					exit ();
				}
				$this->im = ImageCreateFromJpeg ( $this->srcFile );
				break;
			case 3 :
				$this->im = ImageCreateFromPNG ( $this->srcFile );
				break;
		}
		$this->srcW = ImageSX ( $this->im );
		$this->srcH = ImageSY ( $this->im );
	}

	//生成最小裁剪后的缩图
	function Cut($toFile, $toW, $toH) {
		$toWH = $toW / $toH;
		$srcWH = $this->srcW / $this->srcH;
		if ($toWH <= $srcWH) {
			$ctoH = $toH;
			$ctoW = $ctoH * ($this->srcW / $this->srcH);
		} else {
			$ctoW = $toW;
			$ctoH = $ctoW * ($this->srcH / $this->srcW);
		}
		$allImg = $this->CreatImage ( $this->im, $ctoW, $ctoH, 0, 0, 0, 0, $this->srcW, $this->srcH );
		$cImg = $this->CreatImage ( $allImg, $toW, $toH, 0, 0, ($ctoW - $toW) / 2, ($ctoH - $toH) / 2, $toW, $toH );
		return $this->EchoImage ( $cImg, $toFile );
		ImageDestroy ( $cImg );
		ImageDestroy ( $allImg );
	}

	function CreatImage($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH) {
		if (function_exists ( "imagecreatetruecolor" )) {
			@$creatImg = ImageCreateTrueColor ( $creatW, $creatH );
			if ($creatImg)
				ImageCopyResampled ( $creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH );
			else {
				$creatImg = ImageCreate ( $creatW, $creatH );
				ImageCopyResized ( $creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH );
			}
		} else {
			$creatImg = ImageCreate ( $creatW, $creatH );
			ImageCopyResized ( $creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH );
		}
		return $creatImg;
	}

	//输出图片,link---只输出,不保存文件。file--保存为文件
	function EchoImage($img, $to_File) {
		switch ($this->echoType) {
			case "link" :
				if (function_exists ( 'imagejpeg' ))
					return ImageJpeg ( $img );
				else
					return ImagePNG ( $img );
				break;
			case "file" :
				if (function_exists ( 'imagejpeg' ))
					return ImageJpeg ( $img, $to_File );
				else
					return ImagePNG ( $img, $to_File );
				break;
		}
	}
}
?>

PHP教程 ,

生成图片的缩略图类二

2007年7月29日

按比例生成缩略图文件及显示缩略图

<?php
function CreateThumbnail($path,$newFileName,$width=100,$height=100){
	@$s_img = getimagesize($path);
	@$type = $s_img[2];
	switch($type){
		case 1 : @$im = imagecreatefromgif($path);break;
		case 2 : @$im = imagecreatefromjpeg($path); break;
		case 3 : @$im = imagecreatefrompng($path);break;
		default: $im = false;
	}
	@$s_width = imagesx($im);
	@$s_height = imagesy($im);
	$sizexy = getScaleImage($path,$width,$height);
	$width = $sizexy[0];
	$height = $sizexy[1];
	if($im){
		$im2 = imagecreatetruecolor($width,$height);
		imagecopyresized ($im2,$im,0,0,0,0,$width,$height,$s_width,$s_height);
		imagejpeg($im2,$newFileName);
	}
	return false;
}

function getScaleImage($image,$perfectWidth,$perfectHeight){
	if (realpath($image) != false){
		$sizeArray = getimagesize($image);
		$sizeX = $sizeArray[ 0 ] ;
		$sizeY = $sizeArray[ 1 ] ;
		if ($perfectWidth!=0 && $perfectHeight==0){
			if ($sizeX>$perfectWidth){
				$scale = $sizeX/$sizeY ;
				$sizeX = $perfectWidth;
				$sizeY = floor($sizeX/$scale);
			}
		}

		if ($perfectHeight != 0 && $perfectWidth == 0){
			if ($sizeY > $perfectHeight){
				$scale = $sizeY / $sizeX ;
				$sizeY = $perfectHeight;
				$sizeX = floor($sizeY / $scale);
			}
		}

		if ($perfectHeight != 0 && $perfectWidth != 0){
			$sizeX = $perfectWidth;
			$sizeY = $perfectHeight;
		}
		$tmp[] = $sizeX;
		$tmp[] = $sizeY;
	} else {
		$tmp[] = 0;
	}
	return $tmp;
}

$tm = getScaleImage("100_3353.jpg",0,0);
echo "<img src="100_3353.jpg" width='".$tm[0]."' height='".$tm[1]."' />
?>

PHP教程 ,

生成图片的缩略图类一

2007年7月29日

生成图片的缩略图类一

<?php
class image{
	var $src = ''; //原图片地址
	var $dst = ''; //目标图片标示
	var $dst_file = ''; //目标图片文件地址
	var $dst_width = 0; //目标最大宽度
	var $dst_height = 0; //目标最大高度
	var $mode = 0; //是否显示(0显示,1不显示)
	var $exe = ''; //配置新的文件名例如源文件名是:mm.jpg 可以将其配置成plmm.jpg/gg_mm.jpg
	function resize_image($src,$dst_width,$dst_height,$exe='',$mode = 0){
		$this->src = $src;
		$this->dst_width = $dst_width;
		$this->dst_height = $dst_height;
		$this->exe = $exe;
		$this->mode = $mode;
		$width = $this->get_msg($this->src,0);
		$height = $this->get_msg($this->src,1);
		$src_h = $this->get_msg($this->src,2);//return:resouse 返回图象标示符
		$size = $this->resize($this->src,$this->dst_width,$this->dst_height);
		$this->dst = @imagecreatetruecolor($size[x],$size[y]);
		$bool = @imagecopyresized($this->dst,$src_h,0,0,0,0,$size[x],$size[y],$width,$height);
		$new_file = $this->exe.basename($this->src);
		if($bool){
			if(isset($mode) && $mode == 0){
				switch ($this->get_msg($src,3)){
					case 1:
						return imagegif($this->dst);
						break;
					case 2:
						return imagejpeg($this->dst);
						break;
					case 3:
						return imagepng($this->dst);
						break;
				}
			}else{
				switch ($this->get_msg($src,3)){
				case 1:
					return imagegif($this->dst,$new_file);
					break;
				case 2:
					return imagejpeg($this->dst,$new_file);
					break;
				case 3:
					return imagepng($this->dst,$new_file);
					break;
				}
			}
		} else {
			echo "拷贝图象并调整大小过程出错啦~";
		}
	}
	/* $src 源图片的地址
	 * $fetch 获取类型 0 获取image width 1 获取image height 2 获取image 3获取type
	 */
	function get_msg($src, $fetch = 0){
		$data = @getimagesize($src);
		switch ($fetch){
			case 0:
				$width = $data[0];
				return $width;
				break;
			case 1:
				$height = $data[1];
				return $height;
				break;
			case 2:
				switch ($data[2]){
					case 1:
						$dst = imagecreatefromgif($src);
						return $dst;
						break;
					case 2:
						$dst = imagecreatefromjpeg($src);
						return $dst;
						break;
					case 3:
						$dst = imagecreatefrompng($src);
						return $dst;
						break;
				}
			case 3:
				return $data[2];
				break;
		}
	}

	function resize($src,$dst_width,$dst_height){
		$this->src = $src;
		$this->dst_width = $dst_width;
		$this->dst_height = $dst_height;
		$width = $this->get_msg($src,0);
		$height = $this->get_msg($src,1);
		if($this->dst_width && $width > $this->dst_width ){
			$resize_width = 1;
			$width_ratio = $this->dst_width/$width;
		}
		if($this->dst_height && $height > $this->dst_height){
			$resize_height = 1;
			$height_ratio = $this->dst_height/$height;
		}
		if($resize_height && $resize_width){
			//宽度优先
			if($width_ratio < $height_ratio)		{
				$size[x] = $width * $width_ratio;
				$size[y] = $height * $width_ratio;
				return $size;
			}
			//高度优先
			else{
				$size[x] = $width * $height_ratio;
				$size[y] = $height * $height_ratio;
				return $size;
			}
		} elseif($resize_height){
			$size[x] = $width;
			$size[y] = $height * $height_ratio;
			return $size;
		} elseif($resize_width){
			$size[x] = $width * $width_ratio;
			$size[y] = $height;
			return $size;
		}
	}
}
?>

PHP教程 ,