图片文字水印 - 图片水印-按尺寸压缩

/**

* 图片水印

* @param string $source_img 原始图片url

* @param string $water_map 水印图片url

*/

function addWaterMap($source_img, $water_map,$left,$top)

{

  list($dst_w, $dst_h, $dst_type) = getimagesize($source_img);

  switch ($dst_type)

  {

      case 1:

          $img = imagecreatefromgif($source_img);

          break;

      case 2:

          $img = imagecreatefromjpeg($source_img);

          break;

      case 3:

          $img = imagecreatefrompng($source_img);

          break;

      default:

          return false;

          break;

  }

  $img_x    = imagesx($img); //原图宽

  $img_y    = imagesy($img); //原图高

  imagealphablending($img, true);//设置为混合填色模式

  list($map_w, $map_h, $map_type) = getimagesize($water_map);

  switch ($map_type)

  {

      case 1:

          $img_water_map = imagecreatefromgif($water_map);

          break;

      case 2:

          $img_water_map = imagecreatefromjpeg($water_map);

          break;

      case 3:

          $img_water_map = imagecreatefrompng($water_map);

          break;

      default:

          return false;

          break;

  }

  $water_x = imagesx($img_water_map); //水印宽

  $water_y = imagesy($img_water_map); //水印高

  $wimg_x = $left; //水印x坐标

  $wimg_y = $top; //水印y坐标

  imagecopy($img, $img_water_map, $wimg_x, $wimg_y, 0, 0, $water_x, $water_y); //分别为原图,水印,水印x坐标,水印y坐标,水印图片横轴开始点,水印图片纵轴开始点,水印横轴结束,水印纵轴结束


  switch ($dst_type) {

      case 1://GIF

          imagegif($img,$source_img);

          break;

      case 2://JPG

          imagejpeg($img,$source_img);

          break;

      case 3://PNG

          imagepng($img,$source_img);

          break;

      default:

          break;

  }

  imagedestroy($img); //销毁内存数据流

  imagedestroy($img_water_map); //销毁内存数据流

}

/**

  * 压缩图片

  * @param $source_path  原图路径

  * @param $target_path  保存路径

  * @param $imgWidth    目标宽度

  * @param $imgHeight    目标高度

  * @return bool|string

  */

function resize_image($source_path,$target_path,$imgWidth,$imgHeight)

{

    $source_info = getimagesize($source_path);

    $source_mime = $source_info['mime'];

    switch ($source_mime)

    {

        case 'image/gif':

            $source_image = imagecreatefromgif($source_path);

            $ext = ".gif";

            break;

        case 'image/jpeg':

            $source_image = imagecreatefromjpeg($source_path);

            $ext = ".jpg";

            break;

        case 'image/png':

            $source_image = imagecreatefrompng($source_path);

            $ext = ".png";

            break;

    }

    $target_image    = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图

    imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]);

    //保存图片到本地

    $dir = $target_path. '/'. date("Ymd") . '/';

    if (!is_dir($dir)) {

        mkdir($dir, 0777);

    }

    $fileName = $dir.date("YmdHis").uniqid().$ext;

    switch ($source_mime)

    {

        case 'image/gif'://GIF

            $listen = imagegif($target_image,'./'.$fileName);

            break;

        case 'image/jpeg'://JPG

            $listen = imagejpeg($target_image,'./'.$fileName);

            break;

        case 'image/png'://PNG

            $listen = imagepng($target_image,'./'.$fileName);

            break;

    }

    if($listen)

    {

        $fileName = '';

    }

    imagedestroy($target_image);

    return $fileName;

}

/**

  * 文字水印

  * @param $dst_path    原图地址

  * @param $fontSize    文字大小

  * @param $left        文字左边位置

  * @param $top          文字顶部位置

  * @param $text        文本内容

  * @param $circleSize  文本旋转角度

  * @return bool|string

  */

function font_stamp($dst_path,$fontSize = 20,$left=0,$top=0,$text='',$circleSize = 0)

{

  $img = imagecreatefromstring(file_get_contents($dst_path));


  $font = dirname(__FILE__).'\xdct.ttf';//字体,字体文件需保存到相应文件夹下

  $black = imagecolorallocate($img, 255, 255, 255);//字体颜色 RGB

  imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, $text);


  list($bgWidth, $bgHight, $bgType) = getimagesize($dst_path);

  switch($bgType){

      case 1: //gif

          header('Content-Type:image/gif');

          imagegif($img);

          break;

      case 2: //jpg

          header('Content-Type:image/jpg');

          imagejpeg($img);

          break;

      case 3: //png

          header('Content-Type:image/png');

          imagepng($img);  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。

          break;

  }

  imagedestroy($img);

}

$source = "poster.png";

$stamp  = "watermark.png";

// addWaterMap($source,$stamp,0,0);

// $file =  resize_image($source,"thumb",100,100);

// echo $file;

font_stamp($source,50,$left=10,$top=100,$text='我是文本内容我是文本内容我是文本内容');

?>

你可能感兴趣的:(图片文字水印 - 图片水印-按尺寸压缩)