之前有一個我寫的帖子是可以拿到遠程地址的圖片保存本地的
前段時間搞的很火的朋友圈標簽,可以使用下面僅僅三個自定義函數即可合成,具體參數石頭已經給大家寫出,有需要改進的地方請直接留言!
/** * a.合成圖片信息 復制一張圖片的矩形區(qū)域到另外一張圖片的矩形區(qū)域 * @param [type] $bg_image [目標圖] * @param [type] $sub_image [被添加圖] * @param [type] $add_x [目標圖x坐標位置] * @param [type] $add_y [目標圖y坐標位置] * @param [type] $add_w [目標圖寬度區(qū)域] * @param [type] $add_h [目標圖高度區(qū)域] * @param [type] $out_image [輸出圖路徑] * @return [type] [description] */ function image_copy_image($bg_image,$sub_image,$add_x,$add_y,$add_w,$add_h,$out_image){ if($sub_image){ $bg_image_c = imagecreatefromstring(file_get_contents($bg_image)); $sub_image_c = imagecreatefromstring(file_get_contents($sub_image)); imagecopyresampled($bg_image_c, $sub_image_c, $add_x, $add_y, 0, 0, $add_w, $add_h, imagesx($sub_image_c), imagesy($sub_image_c)); //保存到out_image imagejpeg($bg_image_c, $out_image, 80); imagedestroy($sub_image_c); imagedestroy($bg_image_c); } } /** * b.生成文字圖片并插入廣告圖中 * @param [type] $filename [背景路徑] * @param [type] $text [文字內容] * @param [type] $font [文字大小] * @param [type] $size [文字畫布的寬] * @param [type] $width_f [文字顏色] * @param [type] $red [紅] * @param [type] $grn [綠] * @param [type] $blu [藍] */ function create_text($filename,$text,$font,$size,$width_f,$red,$grn,$blu){ $rot = 0; // 旋轉角度 $width = 0; //寬度 $height = 0; //高度 $offset_x = 0; //x偏移 $offset_y = 0; //y偏移 $bounds = array(); $text = autowrap($size, 0, $font, $text,$width_f); // 自動換行處理 /** [字體大小] [角度] [字體名稱] [字符串] [預設寬度] */ // 確定邊框高度. $bounds = ImageTTFBBox($size, $rot, $font, "W"); if ($rot < 0) { $font_height = abs($bounds[7]-$bounds[1]); } else if ($rot > 0) { $font_height = abs($bounds[1]-$bounds[7]); } else { $font_height = abs($bounds[7]-$bounds[1]); } // 確定邊框高度. $bounds = ImageTTFBBox($size, $rot, $font, $text); if ($rot < 0) { $width = abs($bounds[4]-$bounds[0]); $height = abs($bounds[3]-$bounds[7]); $offset_y = $font_height; $offset_x = 0; } else if ($rot > 0) { $width = abs($bounds[2]-$bounds[6]); $height = abs($bounds[1]-$bounds[5]); $offset_y = abs($bounds[7]-$bounds[5])+$font_height; $offset_x = abs($bounds[0]-$bounds[6]); } else { $width = abs($bounds[4]-$bounds[6]); $height = abs($bounds[7]-$bounds[1]); $offset_y = $font_height; $offset_x = 0; } $bg = imagecreatetruecolor($width + 20,$height + 20); // 創(chuàng)建畫布 $color=imagecolorallocatealpha($bg , 0 , 0 , 0 ,127);//拾取一個完全透明的顏色 imagealphablending($bg ,false);//關閉混合模式,以便透明顏色能覆蓋原畫布 imagefill($bg , 0 , 0, $color);//填充 imagesavealpha($bg ,true);//設置保存PNG時保留透明通道信息 $textImg = imagecolorallocate($bg, $red, $grn, $blu); // 創(chuàng)建白色 ImageTTFText($bg, $size, 0, 10, $size + 10, $textImg, $font, $text); imagepng($bg,$filename); } /** * 文字自動換行 * @param [type] $fontsize [字體大小] * @param [type] $angle [角度] * @param [type] $fontface [字體名稱] * @param [type] $string [字符串] * @param [type] $width [預設寬度] */ function autowrap($fontsize, $angle, $fontface, $string, $width) { $content = ""; // 將字符串拆分成一個個單字 保存到數組 letter 中 preg_match_all("/./u", $string, $arr); $letter = $arr[0]; foreach ($letter as $l) { $teststr = $content." ".$l; $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr); // 判斷拼接后的字符串是否超過預設的寬度 if (($testbox[2] > $width) && ($content !== "")) { $content .= PHP_EOL; } $content .= $l; } return $content; }
PHP怎么學習?PHP怎么入門?PHP在哪學?PHP怎么學才快?不用擔心,這里為大家提供了PHP速學教程(入門到精通),有需要的小伙伴保存下載就能學習啦!
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號