php使用Imagick生成圖片的方法
更新時(shí)間:2015年07月31日 14:57:57 作者:龍sky騰
這篇文章主要介紹了php使用Imagick生成圖片的方法,實(shí)例分析了php基于Imagick實(shí)現(xiàn)添加水印、文字的圖片功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了php使用Imagick生成圖片的方法。分享給大家供大家參考。具體如下:
這里使用Imagick 生成圖片
解決了圖片寫中文文字亂碼問題,添加支持的字體
public function getPic(){
header('Content-Type: text/html; charset=utf-8');
$text = '中糧屯河(sh600737)';//中糧屯河(sh600737)
$watermark = '305988103123zczcxzas';
$len = strlen($text);
$width = 10.5*(($len-8)/3*2+8);
$height = 26;
$imagick = new Imagick();
$color_transparent = new ImagickPixel('#ffffff'); //transparent 透明色
$imagick->newImage($width, $height, $color_transparent, 'jpg');
//$imagick->borderimage('#000000', 1, 1);
$style['font_size'] = 12;
$style['fill_color'] = '#000000';
for($num= strlen($watermark); $num>=0; $num--){
$this->add_text($imagick,substr($watermark, $num,1), 2+($num*8), 30, 1,$style);
$this->add_text($imagick,substr($watermark, $num,1), 2+($num*8), 5, 1,$style);
}
//return;
$style['font_size'] = 20;
$style['fill_color'] = '#FF0000';
$style['font'] = './msyh.ttf'; ///微軟雅黑字體 解決中文亂碼
//$text=mb_convert_encoding($text,'UTF-8'); //iconv("GBK","UTF-8//IGNORE",$text);
$this->add_text($imagick,$text, 2, 20, 0,$style);
header ( 'Content-type: ' . strtolower ($imagick->getImageFormat ()) );
echo $imagick->getImagesBlob ();
}
// 添加水印文字
public function add_text(& $imagick, $text, $x = 0, $y = 0, $angle = 0, $style = array()) {
$draw = new ImagickDraw ();
if (isset ( $style ['font'] ))
$draw->setFont ( $style ['font'] );
if (isset ( $style ['font_size'] ))
$draw->setFontSize ( $style ['font_size'] );
if (isset ( $style ['fill_color'] ))
$draw->setFillColor ( $style ['fill_color'] );
if (isset ( $style ['under_color'] ))
$draw->setTextUnderColor ( $style ['under_color'] );
if (isset ( $style ['font_family'] ))
$draw->setfontfamily( $style ['font_family'] );
if (isset ( $style ['font'] ))
$draw->setfont($style ['font'] );
$draw->settextencoding('UTF-8');
if (strtolower ($imagick->getImageFormat ()) == 'gif') {
foreach ( $imagick as $frame ) {
$frame->annotateImage ( $draw, $x, $y, $angle, $text );
}
} else {
$imagick->annotateImage ( $draw, $x, $y, $angle, $text );
}
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- PHP基于imagick擴(kuò)展實(shí)現(xiàn)合成圖片的兩種方法【附imagick擴(kuò)展下載】
- PHP編程獲取圖片的主色調(diào)的方法【基于Imagick擴(kuò)展】
- PHP Imagick完美實(shí)現(xiàn)圖片裁切、生成縮略圖、添加水印
- PHP中使用imagick實(shí)現(xiàn)把PDF轉(zhuǎn)成圖片
- PHP中使用Imagick實(shí)現(xiàn)各種圖片效果實(shí)例
- php_imagick實(shí)現(xiàn)圖片剪切、旋轉(zhuǎn)、銳化、減色或增加特效的方法
- php Imagick獲取圖片RGB顏色值
- PHP基于php_imagick_st-Q8.dll實(shí)現(xiàn)JPG合成GIF圖片的方法
- php使用imagick模塊實(shí)現(xiàn)圖片縮放、裁剪、壓縮示例
- PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能示例
相關(guān)文章
php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
這篇文章主要介紹了php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
探討php define()函數(shù)及defined()函數(shù)使用詳解
本篇文章是對(duì)php中define()函數(shù)及defined()函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
關(guān)于Iframe如何跨域訪問Cookie和Session的解決方法
本篇文章小編將為大家介紹,關(guān)于Iframe如何跨域訪問Cookie和Session的解決方法,有需要的朋友可以參考一下2013-04-04
PHP FOR MYSQL 代碼生成助手(根據(jù)Mysql里的字段自動(dòng)生成類文件的)
根據(jù) Mysql里的字段自動(dòng)生成類文件的實(shí)現(xiàn)代碼,學(xué)習(xí)php的朋友可以參考下。2011-07-07
PHP實(shí)現(xiàn)JWT的Token登錄認(rèn)證
這篇文章通過實(shí)例代碼介紹了PHP實(shí)現(xiàn)JWT的Token登錄認(rèn)證的方式,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

