PageRenderTime 95ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://gitlab.com/alidzapp/Link-Preview
Markdown | 243 lines | 172 code | 71 blank | 0 comment | 0 complexity | d10e87deeb8871e1b4799d6cd9b3d65e MD5 | raw file
  1. Link Preview
  2. ==========================
  3. ## How this works
  4. The algorithm keeps tracking what you are typing in the status field and through regular expressions identifies a url. Thereafter, the text is in the field is passed to PHP that does all the work to analyze all the source code of the url found. If you enter more than one url, it will consider that the first one is the more relevant and it will create a preview.
  5. Once the source code of the url is obtained, regular expressions begin to seek out and capture relevant information on it. This information is basically the title page, the images contained therein, and a brief description of the content covered in the page.
  6. | | |
  7. :--------------------------------------------------------------------------:|:-----------------------------------------------------------------------------:|
  8. | | |
  9. <b>Right</b> | <b>Left</b> |
  10. ![Preview](/demo/img/preview.gif?raw=true "Preview") | ![Left](/demo/img/left.png?raw=true "Left") |
  11. <b>Bottom</b> | <b>Top</b> |
  12. ![Bottom](/demo/img/bottom.png?raw=true "Bottom") | ![Top](/demo/img/top.png?raw=true "Top") |
  13. <b>Gallery<b> | <b>Video</b> |
  14. ![Preview Gallery](/demo/img/preview_gallery.gif?raw=true "Preview Gallery")| ![Preview Video](/demo/img/preview_video.gif?raw=true "Preview Video") |
  15. =======
  16. ## HTTP Server
  17. - Apache (must support mod_php)
  18. ## How to add it to your project
  19. <b>1 &bull; Stylesheets</b>
  20. ```html
  21. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
  22. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  23. <link rel="stylesheet" type="text/css" href="src/link-preview/css/link-preview.css" />
  24. ```
  25. <b>2 &bull; Scripts</b>
  26. ```html
  27. <script src="https://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
  28. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  29. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js" type="text/javascript"></script>
  30. <script src="src/link-preview/js/link-preview.js" type="text/javascript"></script>
  31. <!-- Include this script below if you want to retrieve the posts inserted to database -->
  32. <script src="src/link-preview/js/link-preview-database.js" type="text/javascript"></script>
  33. ```
  34. <b>3 &bull; Configuration</b>
  35. Add the link preview module as a dependency to your application module:
  36. ```html
  37. var app = angular.module('App', ['linkpreview'])
  38. ```
  39. Add the directive inside your controller html:
  40. ```html
  41. <link-preview placeholder="What's in your mind?" />
  42. ```
  43. Attributes
  44. | att | var | default value | possible values |
  45. |:-------------:|:-------------------:|:-------------------:|:---------------------------------------------------------:|
  46. | type | type | right | right, left, bottom, top (text position) |
  47. | amount | imageAmount | -1 | any integer |
  48. | btext | buttonText | Post | any string |
  49. | bclass | buttonClass | primary | default, primary, success, info, warning, danger, link |
  50. | cbtext | cancelButtonText | Cancel | any string |
  51. | cbclass | cancelButtonClass | danger | default, primary, success, info, warning, danger, link |
  52. | ltext | loadingText | Loading | any string |
  53. | limage | loadingImage | empty.png | any image url |
  54. | placeholder | placeholder | an empty string | any string |
  55. | ttext | thumbnailText | Choose a thumbnail | any string |
  56. | nttext | noThumbnailText | No thumbnail | any string |
  57. | tpage | thumbnailPagination | %N of %N | any string, %N is the number reference of pagination |
  58. | dtitle | defaultTitle | Enter a title | any string |
  59. | ddescription | defaultDescription | Enter a description | any string |
  60. ```html
  61. <link-preview
  62. type=""
  63. ttext=""
  64. tpage=""
  65. placeholder=""
  66. amount=""
  67. limage=""
  68. ltext=""
  69. btext=""
  70. bclass=""
  71. ctext=""
  72. cclass=""
  73. dtitle=""
  74. ddescription="" />
  75. ```
  76. <b>4 &bull; Database</b>
  77. To custom your database configurations, you need to change the following values in [Database.php](/src/link-preview/php/classes/Database.php)
  78. ```php
  79. $host = "localhost";
  80. $user = "";
  81. $password = "";
  82. $database = "linkpreview";
  83. ```
  84. Additionally, the way I used to retrieve the data was creating a controller [link-preview-database.js](/src/link-preview/js/link-preview-database.js)
  85. which you can include the file somehow in your project or you can its content to yours.
  86. ```javascript
  87. app.controller('MyControllerDatabase', ['$scope', '$http', function ($scope, $http) {
  88. $scope.databasePosts = [];
  89. $scope.retrieveFromDatabase = function () {
  90. // You must insert in your page a div with the posts retrieved from database. Just like the posts div
  91. // on template html files
  92. var url = 'src/link-preview/php/retrieve.php';
  93. $http({
  94. url: url,
  95. method: "GET",
  96. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  97. }).success(function (data, status, headers, config) {
  98. for (var i = 0; i < data.length; i++) {
  99. data[i].video = data[i].videoIframe !== "";
  100. data[i].showIframe = false;
  101. data[i].textHTML = $sce.trustAsHtml(data[i].text);
  102. data[i].descriptionHTML = $sce.trustAsHtml(data[i].description);
  103. data[i].videoIframeHTML = $sce.trustAsHtml(data[i].videoIframe);
  104. console.log(data[i]);
  105. }
  106. $scope.databasePosts = data;
  107. });
  108. };
  109. $scope.deletePosted = function (post, $index) {
  110. $scope.posts.splice($index, 1);
  111. };
  112. $scope.imageAction = function (post) {
  113. if (post.video == false) {
  114. window.open(post.pageUrl, '_blank');
  115. } else {
  116. post.showIframe = true;
  117. }
  118. };
  119. $scope.hidePlay = function (post) {
  120. return post.video == false || post.showIframe == true;
  121. };
  122. $scope.layoutWithoutImage = function (post) {
  123. return post.image == '' || post.showIframe == true;
  124. };
  125. $scope.layoutWithImage = function (post) {
  126. return post.image != '' || (post.video == true && post.showIframe == false);
  127. };
  128. }]);
  129. ```
  130. Also, check the file [database-template.php](src/link-preview/template/database-template.php) to see an example of how to display the data retrieved from database.
  131. Make sure your columns are the same as those ones in [linkpreview.sql](/src/link-preview/linkpreview.sql) or customize them.
  132. ## Result Format
  133. ```json
  134. {
  135. "title":"title",
  136. "url":"original url",
  137. "pageUrl":"page url",
  138. "canonicalUrl":"canonical url",
  139. "description":"description",
  140. "images": ["array of images"],
  141. "image": "first image of images",
  142. "video":"true|false",
  143. "videoIframe":"video iframe, if it is a video"
  144. }
  145. ```
  146. ## Important
  147. Make sure the library <b>php5-curl</b> is installed and enabled on the server, either locally or remotely.
  148. - Linux
  149. ```bash
  150. $ sudo apt-get install php5-curl
  151. $ sudo service apache2 restart
  152. ```
  153. - Mac (via [macports](https://www.macports.org/))
  154. ```bash
  155. $ sudo port install php5-curl
  156. $ sudo apachectl restart
  157. ```
  158. Also where there is <b>'src/link-preview/'</b> as part of a path of a <b>Javascript, CSS or image</b> file,
  159. you must change to the path on your server.
  160. Information and Contact
  161. ===
  162. Developed by [@LeonardoCardoso](https://github.com/LeonardoCardoso).
  163. For more information, please visit [http://lab.leocardz.com/link-preview/](http://lab.leocardz.com/link-preview/).
  164. Contact me either by Twitter [@leocardz](https://twitter.com/leocardz) or emailing me to [contact@leocardz.com](mailto:contact@leocardz.com).
  165. Follow <a href='https://twitter.com/lc_link_preview' target='_blank'>@lc_link_preview</a> on Twitter to get updates of what sites the people are successfully previewing around the globe.
  166. Related Projects
  167. ===
  168. * [Swift Link Preview](https://github.com/LeonardoCardoso/Swift-Link-Preview)
  169. * [Android Link Preview](https://github.com/LeonardoCardoso/Android-Link-Preview)
  170. License
  171. =================================
  172. [MIT](LICENSE-MIT)