构造方法只需要传递图片所在路径即可!对应方法及参数都有注释!
请给予最佳答案!!
<?php
class Img{
private $path
//构造方法,初始化图片信息
function __construct($path='./imgs/'){
$this->path=rtrim($path,'/').'/'
}
/**
* 对图片进行缩放
* 参数对应:文件名 缩放后宽度 缩放后高度 缩放后图片名前缀
*/
function thumb($name,$width,$height,$pre="th_"){
if(file_exists($this->path.$name)){
$imgInfo=$this->getInfo($name)
$img=$this->getImg($name,$imgInfo)
$newSize=$this->getNewSize($name,$width,$height,$imgInfo)
$newImg=$this->getNewInfo($img,$newSize,$imgInfo)
return $this->createNewImage($newImg, $pre.$name, $imgInfo)
}else{
echo '图片'.$this->path.$name.'不存在,请检查文件名及路径是否填写正确'
}
}
//辅助图片处理,获取图片的宽、高、类型属性
private function getInfo($name){
$temp=getImageSize($this->path.$name)
$imgInfo['width']=$temp[0]
$imgInfo['height']=$temp[1]
$imgInfo['type']=$temp[2]
return $imgInfo
}
//辅助图片处理,获取创建的图片资源
private function getImg($name,$imgInfo){
$src=$this->path.$name
switch($imgInfo['type']){
case 1:
$img=imagecreatefromgif($src)
break
case 2:
$img=imagecreatefromjpeg($src)
break
case 3:
$img=imagecreatefrompng($src)
break
}
return $img
}
//辅助图片处理,获取创建的图片资源
private function getNewSize($name,$width,$height,$imgInfo){
$newSize['width']=$imgInfo['width']
$newSize['height']=$imgInfo['height']
if($width<$imgInfo['width']){
$newSize['width']=$width
}
if($height<$imgInfo['height']){
$newSize['height']=$height
}
if($imgInfo["width"]*$newSize["width"] >$imgInfo["height"] * $newSize["height"]){
$newSize["height"]=round($imgInfo["height"]*$newSize["width"]/$imgInfo["width"])
}else{
$newSize["width"]=round($imgInfo["width"]*$newSize["height"]/$imgInfo["height"])
}
print_r($newSize)
return $newSize
}
//辅助图片处理,获取缩放的图片资源
private function getNewInfo($img,$newSize,$imgInfo){
$newImg=imagecreatetruecolor($newSize['height'],$newSize['height'])
$otsc=imagecolortransparent($img)
if($otsc >=0 &&$otsc <= imagecolorstotal($img)){
$tran=imagecolorsforindex($img, $otsc)
$newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"])
imagefill($newImg, 0, 0, $newt)
imagecolortransparent($newImg, $newt)
}
imagecopyresized($newImg, $img, 0, 0, 0, 0, $newSize["width"], $newSize["height"], $imgInfo["width"], $imgInfo["height"])
imagedestroy($img)
return $newImg
}
//辅助图片处理,创建新的图片
private function createNewImage($newImg, $newName, $imgInfo){
switch($imgInfo["type"]){
case 1://gif
$result=imageGif($newImg, $this->path.$newName)
break
case 2://jpg
$result=imageJPEG($newImg, $this->path.$newName)
break
case 3://png
$return=imagepng($newImg, $this->path.$newName)
break
}
imagedestroy($newImg)
return $newName
}
/**
* 对图片加水印
* 参数对应:需水印图片 水印图片 加水印后图片名前缀
*/
function waterMark($name,$wname,$pre="wa_"){
if(file_exists($this->path.$name)){
if(file_exists($this->path.$wname)){
$info=$this->getInfo($name)
$winfo=$this->getInfo($wname)
if($p=$this->getPosition($info,$winfo)){
$img=$this->getImg($name,$info)
$wImg=$this->getImg($wname,$winfo)
imagecopy($img, $wImg, $p["x"], $p["y"], 0, 0, $winfo["width"], $winfo["height"])
imagedestroy($wImg)
return $this->createNewImage($img,$pre.$name,$info)
}else{
echo '水印图片尺寸大于原图片尺寸'
}
}else{
echo '水印图片'.$this->path.$wname.'不存在,请检查文件名及路径是否填写正确'
}
}else{
echo '图片'.$this->path.$name.'不存在,请检查文件名及路径是否填写正确'
}
}
//辅助图片处理,获取水印图片应处坐标
private function getPosition($info,$winfo){
if($info['width']<$winfo['width']||$info['height']<$winfo['height']){
return false
}
$x=$info['width']-$winfo['width']
$y=$info['height']-$winfo['height']
return array('x'=>$x,'y'=>$y)
}
/**
* 图片剪切函数
* 对应参数:原图片 X坐标 Y坐标 宽度 高度
*/
function cut($name,$x,$y,$width,$height,$pre='cx_'){
$imgInfo=$this->getInfo($name)
$img=$this->getImg($name,$imgInfo)
$newImg=imagecreatetruecolor($width,$height)
imagecopyresampled($newImg,$img,0,0,$x,$y,$width,$height,$width,$height)
return $this->createNewImage($newImg, $pre.$name, $imgInfo)
}
}
用ASP.jpeg组建,在服务器安装好后以下为加水印代码
<%
Dim Jpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' 打开目标图片
Jpeg.Open Server.MapPath("images/1.jpg")'图片地址
' 添加文字水印
Jpeg.Canvas.Font.Color = &HFF0000' 红色
Jpeg.Canvas.Font.Family = "宋体"
Jpeg.Canvas.Font.Bold = True
Jpeg.Canvas.Print 10, 10, "水印内容:http://www.aspsub.cn/"
' 保存文件
Jpeg.Save Server.MapPath("images/1.jpg")
' 注销对象
Set Jpeg = Nothing
%>
你上传玩了后,获取到上传后的图片地址
存入一个变量中 然后吧 Server.MapPath("images/1.jpg")换成那个变量就行了