圖片文字水印 - 圖片水印-按尺寸壓縮

<?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),水印圖片橫軸開始點,水印圖片縱軸開始點,水印橫軸結(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)容');

?>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容