首页 > 开发 > PHP > 正文

php生成图片缩略图功能示例

2024-05-04 22:48:58
字体:
来源:转载
供稿:网友

本文实例讲述了php生成图片缩略图功能。分享给大家供大家参考,具体如下:

完整代码如下(为方便阅读,代码使用http://tools.jb51.net/code/phpformat进行了格式化处理):

<?php/* * Created on 2011-3-18 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates*//*构造函数-生成缩略图+水印,参数说明:$srcFile-图片文件名,$dstFile-另存文件名,$markwords-水印文字,$markimage-水印图片,$dstW-图片保存宽度,$dstH-图片保存高度,$rate-图片保存品质*/makethumb("1.jpg", "aa/b.jpg", "50", "50");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);  }  ImageJpeg($ni, $dstFile, $rate);  ImageJpeg($ni, $srcFile, $rate);  imagedestroy($im);  imagedestroy($ni);}?>

PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:

在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/img2base64

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表