谁有php批量处理图片、图片生成缩略图、图片添加水印的函数?

谁有php批量处理图片、图片生成缩略图、图片添加水印的函数?,第1张

//批量处理图片、图片生成缩略图、图片添加水印

$dir = opendir (dirname(__FILE__))

while (!!$_file = readdir($dir)){

 list($filesname,$kzm)=explode(".",$_file)//获取扩展名

 if($kzm=="gif" or $kzm=="jpg" or $kzm=="JPG" or $kzm=="png") {

  if(!makethumb("$_file","120","120","100")){

   echo '执行成功!'

  }else{

   echo '执行失败!'

  }

 }

}

closedir($dir)

/**

 * 处理缩略图并添加水印函数

 * @access publiuc

 * @param $srcFile-----------图片文件名

 * @param $dstFile-----------另存的文件名

 * @param $dstW-------------图片保存的宽度

 * @param $dstH--------------图片保存的高度

 * @param $rate---------------图片保存的品质

 * @param $markwords-----水印文字

 * @param $markimage-----水印图片

 * @param 使用方法 makethumb("a.jpg","b.jpg","120","120","100")

 */

function makethumb($srcFile/*,$dstFile*/,$dstW,$dstH,$rate=100/*,$markwords=null,$markimage=null*/) {

 

 $data = GetImageSize($srcFile)

 switch($data[2]) {

  case 1:

  $im=@ImageCreateFromGIF($srcFile)

  break

  case 2:

  $im=@ImageCreateFromJPEG($srcFile)

  break

  case 3:

  $im=@ImageCreateFromPNG($srcFile)

  break

  }

 if(!$im) return False

 $srcW=ImageSX($im)

 $srcH=ImageSY($im)

 $dstX=0

 $dstY=0

 if ($srcW*$dstH>$srcH*$dstW) {

 $fdstH = round($srcH*$dstW/$srcW)

 $dstY = floor(($dstH-$fdstH)/2)

 $fdstW = $dstW

 }

 else

 {

 $fdstW = round($srcW*$dstH/$srcH)

 $dstX = floor(($dstW-$fdstW)/2)

 $fdstH = $dstH

 }

$ni=ImageCreateTrueColor($dstW,$dstH)

$dstX=($dstX<0)?0:$dstX

$dstY=($dstX<0)?0:$dstY

$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX

$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY

$white = ImageColorAllocate($ni,255,255,255)

$black = ImageColorAllocate($ni,0,0,0)

imagefilledrectangle($ni,0,0,$dstW,$dstH,$white)// 填充背景色

ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH)

//if($markwords!=null){

// $markwords=iconv("gb2312","UTF-8",$markwords)

// //转换文字编码

// ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords) //写入文字水印,参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容

//}elseif($markimage!=null) {

// $wimage_data = GetImageSize($markimage)

// switch($wimage_data[2]) {

// case 1:

//  $wimage=@ImageCreateFromGIF($markimage)

//  break

// case 2:

//  $wimage=@ImageCreateFromJPEG($markimage)

//  break

// case 3:

//  $wimage=@ImageCreateFromPNG($markimage)

//  break

// }

// imagecopy($ni,$wimage,500,560,0,0,88,31) //写入图片水印,水印图片大小默认为88*31

// imagedestroy($wimage)

//}

$dstFile = $srcFile.'.gif'

ImageJpeg($ni,$dstFile,$rate)

//ImageJpeg($ni,$srcFile,$rate)

imagedestroy($im)

imagedestroy($ni)

}

这个要用到文件列遍函数

$dir="./images/*.jpg" //以jpg图片为例

$image_files=glob($dir) //将目录中指定类型文件url读入$image_files数组

然后用循环语句进行添加水印

for($i=0$i<count($image_files)$i++)

{

//调用图片文件

$this_image=$image_files[$i]

//添加水印代码

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11911316.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存