<?php
/**
* 圖片水印
* @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);//設(shè)置為混合填色模式
? 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坐標(biāo)
? $wimg_y = $top; //水印y坐標(biāo)
? imagecopy($img, $img_water_map, $wimg_x, $wimg_y, 0, 0, $water_x, $water_y); //分別為原圖访雪,水印叼屠,水印x坐標(biāo)冒嫡,水印y坐標(biāo)妈嘹,水印圖片橫軸開始點(diǎn),水印圖片縱軸開始點(diǎn)莽使,水印橫軸結(jié)束燎字,水印縱軸結(jié)束
? 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); //銷毀內(nèi)存數(shù)據(jù)流
? imagedestroy($img_water_map); //銷毀內(nèi)存數(shù)據(jù)流
}
/**
? * 壓縮圖片
? * @param $source_path? 原圖路徑
? * @param $target_path? 保存路徑
? * @param $imgWidth? ? 目標(biāo)寬度
? * @param $imgHeight? ? 目標(biāo)高度
? * @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); //創(chuàng)建一個彩色的底圖
? ? 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? ? ? ? 文本內(nèi)容
? * @param $circleSize? 文本旋轉(zhuǎn)角度
? * @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';//字體,字體文件需保存到相應(yīng)文件夾下
? $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 文件,上面也可設(shè)置相應(yīng)的保存目錄及文件名煤痕。
? ? ? ? ? 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='我是文本內(nèi)容我是文本內(nèi)容我是文本內(nèi)容');
?>