phpimagephp 處理圖片處理文字合集
phpimage 是一個 PHP 環(huán)境下的圖片處理工具,裁剪+水印+文字+壓縮+識別等。
1.圖片合成,兩張圖片合成一張圖片
//目標(biāo)圖片透明的地方不透明了,也就是說png透明的會被當(dāng)做白色處理
//imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )---拷貝并合并圖像的一部分
//將 src_im 圖像中坐標(biāo)從 src_x,src_y 開始,寬度為 src_w,高度為 src_h 的一部分拷貝到 dst_im 圖像中坐標(biāo)為 dst_x 和 dst_y 的位置上。兩圖像將根據(jù) pct 來決定合并程度,其值范圍從 0 到 100。當(dāng) pct = 0 時,實際上什么也沒做,當(dāng)為 100 時對于調(diào)色板圖像本函數(shù)和 imagecopy() 完全一樣,它對真彩色圖像實現(xiàn)了 alpha 透明
public function doMerge($dis, $src, $newImg)
{
$imageD = imagecreatefrompng($dis);//目標(biāo)圖
$imageS = imagecreatefrompng($src);
imagecopymerge($imageD, $imageS, 0, 0, 0,0, imagesx($imageS), imagesy($imageS), 0);;
imagepng($imageD, $newImg);//bool(true)
}
2.圖片壓縮
//源文件,新的寬,新的高
//return 新文件的地址
public function zoomImg($file, $nw, $nh)
{
$nfile = '/tmp/shuiyin/zoom.png';
$new = imagecreatetruecolor($nw, $nh);
list($width, $height) = getimagesize($file);
$img = imagecreatefromjpeg($file);
imagecopyresized($new, $img,0, 0,0, 0, $nw, $nh, $width, $height);
imagejpeg($new, $nfile);
imagedestroy($new);
imagedestroy($img);
return $nfile;
}
3.向圖片寫入文件
/**
* PHP實現(xiàn)圖片上寫入實現(xiàn)文字自動換行
* @param $fontsize 字體大小
* @param $angle 角度
* @param $font 字體路徑
* @param $string 要寫在圖片上的文字
* @param $width 預(yù)先設(shè)置圖片上文字的寬度
* @param $flag 換行時單詞不折行
*/
public function wordWrap($fontsize,$angle,$font,$string,$width,$flag=true) {
$content = "";
if($flag){
$words = explode(" ",$string);
foreach ($words as $key=>$value) {
$teststr = $content." ".$value;
$testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
if(($testbox[2] > $width)) {
$content .= "\n";
}
$content .= $value." ";
}
} else {
for ($i=0;$i<mb_strlen($string);$i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
// 判斷拼接后的字符串是否超過預(yù)設(shè)的寬度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
}
return $content;
}
4.等等,圖片文字處理相關(guān)功能會繼續(xù)升級更新
備注:調(diào)用舉例
//寫入文字***********時期
$config['file'] = "/tmp/shuiyin/shuiyin_ret.png";
$config['nFile'] = "/tmp/shuiyin/shuiyin_ret_1.png";//日期
$config['size'] = 32;
$config['width'] = 600;
$config['angle'] = 0;
$config['fontfile'] = "/tmp/shuiyin/msyh.ttc";
$config['x'] = 56;
$config['y'] = 1345;
$font = new \Phpmedia\Font\WordsOnImg($config);
$content = $font->writeWordsToImg($day, false);
評論
圖片
表情
