电话号码生成图片格式
生活随笔
收集整理的這篇文章主要介紹了
电话号码生成图片格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在信息時代,數(shù)據(jù)就是一種寶貴資源。公司網(wǎng)站的展示頁有許多用戶的手機(jī)號碼,但是為了防止競爭對手采集電話號碼信息或者說提高他們采集數(shù)據(jù)的難度。于是就有了電話號碼以圖片格式輸出的這個需求。下面已yii為例講解:
php代碼:
class SiteController extends Controller {public function actionGetPhone($id){//從redis緩存里面讀取數(shù)據(jù),你可以根據(jù)你的實(shí)際情況讀取數(shù)據(jù)$redis = new RedisCache;$res = $redis->getInfo($id, $this->id);if (!$res) {Yii::app()->end();}$res = json_decode($res['model'], TRUE);//endheader("Cache-Control: private, max-age=10800, pre-check=10800");header("Pragma: private");header("Expires: " . date(DATE_RFC822, strtotime(" 1 year")));if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $res['update_time'])) {header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $res['update_time']) . ' GMT', TRUE, 304);Yii::app()->end();}$phone = explode(',', $res['phone']);/** This page creates a simple image.* The image makes use of a TrueType font.*/// Establish image factors:$text = $phone[0];$font_size = Yii::app()->request->getParam('fs'); // Font size is in pixels.if (!$font_size) {$font_size = 14;}$font_file = Yii::app()->basePath . '/data/Georgia.ttf'; // This is the path to your font file.// Retrieve bounding box:$type_space = imagettfbbox($font_size, 0, $font_file, $text);// Determine image width and height, 10 pixels are added for 5 pixels padding:$image_width = abs($type_space[4] - $type_space[0]) + 2;$image_height = abs($type_space[5] - $type_space[1]) + 2;// Create image:$image = imagecreatetruecolor($image_width, $image_height);// Allocate text and background colors (RGB format):$text_colors = Yii::app()->request->getParam('fc');//文本顏色if ($text_colors) {$text_colors = explode(',', $text_colors);} else {$text_colors = array(0, 0, 0);}$bg_colors = Yii::app()->request->getParam('bc');//背景顏色if ($bg_colors) {$bg_colors = explode(',', $bg_colors);} else {$bg_colors = array(255, 255, 255);}$text_color = imagecolorallocate($image, $text_colors[0], $text_colors[1], $text_colors[2]);$bg_color = imagecolorallocate($image, $bg_colors[0], $bg_colors[1], $bg_colors[2]);// Fill image:imagefill($image, 0, 0, $bg_color);// Fix starting x and y coordinates for the text:$x = 0; // Padding of 5 pixels.$y = $image_height - 5; // So that the text is vertically centered.// Add TrueType text to image:imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_file, $text);// Using imagepng() results in clearer text compared with imagejpeg()header('Content-type:image/png');header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $res['update_time']) . ' GMT');imagepng($image);imagedestroy($image);}}
html代碼:
<a style="cursor:default;padding-left: 25px;"><img class="serve-master-number"> </a>
js代碼:
這里是js異步請求的方式加載。
//電話號碼以圖片格式輸出$('.serve-master-number').attr('src', '<?php echo Yii::app()->request->baseUrl; ?>/site/getPhone/id/<?php echo $value['masterInfo']['master_id'] ?>/fs/14/fc/254,143,0/bc/255,255,255');
總結(jié)
以上是生活随笔為你收集整理的电话号码生成图片格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 变形金刚登陆成都太古里3D大屏首秀远超所
- 下一篇: 【OpenGL学习笔记⑧】——键盘控制正