php文字水印 圖片水印 圓形微信頭像水印

一. 看效果(需求是將用戶的微信頭像變成水印打在背景圖上)

二.看代碼

? ? //準備繪圖元素

????$location= array();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//坐標? ?可寫在配置文件里

????$headImg? = $userInfo['headimgurl'];? ? ? ? ? ?//頭像

? ? $time= date("Y/m/d",time());? ? ? ? ? ? ? ? ? ? ? ? ? //日期

????//相關證書背景圖存在,則以為背景,重新合成圖(打上頭像、日期)

????$BG_URL= '/bg.jpg';

????$imageUrl= $this->createImg ($BG_URL, $headImg, $time,$FONT, $location);


????private function createImg($source, $text1, $text2, $font ,$L) {

? ? ? ? $file_root = '/tmp/';

? ? ? ? $file_name =? md5 ( $source . $text1 . $text2) . '.jpg';

? ? ? ? if(!file_exists ($source ) or !file_exists ($font ) ){

? ? ? ? ? ? die("資源不存在!");

? ? ? ? }

? ? ? ? if(@!file_get_contents($file_root.$file_name)){

? ? ? ? ? ? $main? = imagecreatefromjpeg ( $source );

? ? ? ? ? ? $w? = imagesx ( $main );

? ? ? ? ? ? $h = imagesy ( $main );

? ? ? ? ? ? $target = imagecreatetruecolor ( $width, $height );

? ? ? ? ? ? $white? = imagecolorallocate ( $target, 255, 255, 255 );

? ? ? ? ? ? imagefill ( $target, 0, 0, $white );

? ? ? ? ? ? imagecopyresampled ( $target, $main, 0, 0, 0, 0, $w, $h, $w, $h );

? ? ? ? ? ? $fontColor = imagecolorallocate ( $target, 22,172,169 );? ?//字的RGB顏色

? ??????????//打上文字

? ? ? ? ? ? imagettftext ( $target, n,n,n,n, $fontColor, $font, $text2 );?

? ? ? ? ? ? //獲取頭像圖(將圖像存在本地)

? ? ? ? ? ? $head_file? =? 'tx'.md5 ($text1) . '.jpg';

? ? ? ? ? ? $localPath? =? $file_root.$head_file;

? ? ? ? ? ? //如文件不存在,將遠程文件下載到本地

? ? ? ? ? ? if (!file_exists($localPath)) {

? ? ? ? ? ? ? ? $image? = $this->getImage($text1);? ? ? ? ? ? //獲取遠程文件,用curl請求

? ? ? ? ? ? ? ? file_put_contents($localPath,$image);

? ? ? ? ? ? }

? ? ? ? ? ? $localPath? = $this->circular($localPath,$file_root);? ?//將頭像處理成圓形

? ? ? ? ? ? $localPath? = imagecreatefromstring(file_get_contents($localPath));

? ???????????//打印水印

? ? ? ? ? ? imagecopy ( $target, $localPath,n,n,n,n,n,n);??

? ? ? ? ? ? imagejpeg ( $target, $file_root.$file_name, 95 );? ? ?//?輸出合成的新圖象到文件

? ? ? ? ? ? imagedestroy ( $main );

? ? ? ? ? ? imagedestroy ( $target );

? ? ? ? }

? ??????//同步圖片到專門存儲圖片的服務器(這個具體看公司業(yè)務)

? ? ? ? $file_path =$this->synchronou($file_root.$file_name);?

? ? ? ? $data = json_decode($file_path,true);

? ? ? ? if ($data['ret_code']==1) {

? ? ? ? ? ? return? "www.xxx.com/".$data['filename'];

? ? ? ? }

? ? }

? ?//下載遠程圖片

? ? private function getImage($url ){

? ? ? ? $ch = curl_init();

? ? ? ? curl_setopt($ch, CURLOPT_URL, $url);

? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

? ? ? ? curl_setopt($ch, CURLOPT_ENCODING, "");?

? ? ? ? curl_setopt($ch, CURLOPT_HEADER, 0);

? ? ? ? $output = curl_exec($ch);

? ? ? ? curl_close($ch);

? ? ? ? return $output;

? ? }

三.小結

? ? ? ? ? 主要用到的是GD庫知識,不清楚的地方可去看官方手冊,注意函數參數。

? ? ? ? ? 獲取遠程圖片資源使用curl,比如獲取微信頭像存到本地curl不到1秒時間,

? ? 使用file_get_contents()竟然要花費18秒。

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容