Guest User

Untitled

a guest
Nov 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. class OCR {
  2.  
  3. function __construct(){
  4. if ( function_exists(‘register_uninstall_hook’) ){
  5. register_uninstall_hook( __FILE__, array( $this, ‘Uninstall’ ) );
  6. }
  7.  
  8. add_action( ‘add_attachment’, array( $this, ‘AnalyzeImage’ ) );
  9. add_action( ‘admin_menu’, array( $this, ‘SubMenuItem’ ) );
  10.  
  11. add_filter( ‘attachment_fields_to_edit’, array( $this, ‘EditOCRText’ ), 10, 2);
  12. add_filter( ‘attachment_fields_to_save’, array( $this, ‘SaveOCRText’ ), 10, 2);
  13.  
  14. add_option( ‘ocr_resize_percent’, 200 ); //set the default value for the resize percent
  15. }
  16.  
  17. function AnalyzeImage($image_id){
  18. $upload_dir = wp_upload_dir();
  19. $upload_dir = $upload_dir[‘basedir’];
  20. $image_path = $upload_dir.’/’.get_post_meta($image_id, ‘_wp_attached_file’, true);
  21. if(getimagesize($image_path)){ //only go through the steps for OCR if the file is an image
  22. $imagemagick = get_option(‘ocr_imagemagick_path’);
  23. $tesseract = get_option(‘ocr_tesseract_path’);
  24. $size_percent = get_option(‘ocr_resize_percent’);
  25. if($imagemagick && $tesseract && $size_percent){ //only analyze the image if the plugin configuration has been filled in
  26. $temp_image = $upload_dir.’/ocr_image.tif’; //tesseract requires a tiff
  27. $temp_text = $upload_dir.’/ocr_text’;
  28. $command = $imagemagick.’ -resize ‘.$size_percent.’% ‘.$image_path.’ ‘.$temp_image.’ && ‘.$tesseract.’ ‘.$temp_image.’ ‘.$temp_text.’ && cat ‘.$temp_text.’.txt && rm -f ‘.$temp_text.’.txt ‘.$temp_image;
  29. $ocr_text = shell_exec($command);
  30. add_post_meta( $image_id, ‘ocr_text’, $ocr_text, true );
  31. }
  32. }
  33. }
  34.  
  35. function SubMenuItem(){
  36. add_submenu_page( ‘plugins.php’, ‘OCR Configuration’, ‘OCR’, ‘administrator’, __FILE__, array( $this, ‘SettingsPage’ ) );
  37. add_action( ‘admin_init’, array( $this, ‘RegisterSettings’ ) );
  38. }
  39.  
  40. function RegisterSettings() {
  41. register_setting( ‘ocr-settings-group’, ‘ocr_imagemagick_path’ );
  42. register_setting( ‘ocr-settings-group’, ‘ocr_tesseract_path’ );
  43. register_setting( ‘ocr-settings-group’, ‘ocr_resize_percent’ );
  44. }
  45.  
  46. function SettingsPage(){
  47. ?>
  48. <div class=”wrap”>
  49. <h2>OCR Settings</h2>
  50. <p>
  51. The OCR plugin requires PHP5 and two command line utilities: ImageMagick for preparing the images and Tesseract for the actual OCR.
  52. These utilities must be manually installed on your server and executable by PHP. This process, and consequently this plugin, is recommended only for advanced users.
  53. </p>
  54. <form method=”post” action=”options.php”>
  55. <?php settings_fields( ‘ocr-settings-group’ ); ?>
  56. <table class=”form-table”>
  57. <tr valign=”top”>
  58. <th scope=”row”>Absolute Path to ImageMagick’s convert<br><i style=”font-size:10px;”>(ex: /opt/local/bin/convert)</i></th>
  59. <td><input type=”text” name=”ocr_imagemagick_path” value=”<?php echo get_option(‘ocr_imagemagick_path’); ?>” /></td>
  60. </tr>
  61. <tr valign=”top”>
  62. <th scope=”row”>Absolute Path to Tesseract<br><i style=”font-size:10px;”>(ex: /opt/local/bin/tesseract)</i></th>
  63. <td><input type=”text” name=”ocr_tesseract_path” value=”<?php echo get_option(‘ocr_tesseract_path’); ?>” /></td>
  64. </tr>
  65. <tr valign=”top”>
  66. <th scope=”row”>Resize percentage<br><i style=”font-size:10px;”>A higher % might lead to more accurate OCR but will take longer to calculate. Default = 200%</i></th>
  67. <td><input type=”text” name=”ocr_resize_percent” value=”<?php echo get_option(‘ocr_resize_percent’); ?>” />%</td>
  68. </tr>
  69. </table>
  70. <p class=”submit”>
  71. <input type=”submit” class=”button-primary” value=”<?php _e(‘Save Changes’) ?>” />
  72. </p>
  73. </form>
  74. </div>
  75. <?php
  76. }
  77.  
  78. function EditOCRText( $form_fields, $post ){
  79. if ( substr($post->post_mime_type, 0, 5) == ‘image’ ) {
  80. $ocr_text = get_post_meta($post->ID, ‘ocr_text’, true);
  81. if ( empty($ocr_text) )
  82. $ocr_text = ”;
  83.  
  84. $form_fields[‘ocr_text’] = array(
  85. ‘value’ => $ocr_text,
  86. ‘label’ => __(‘OCR Text’),
  87. ‘helps’ => __(‘Text automatically pulled from the image via the OCR plugin.’),
  88. ‘input’ => ‘textarea’
  89. );
  90. }
  91. return $form_fields;
  92. }
  93.  
  94. function SaveOCRText($post, $attachment){
  95. if ( isset($attachment[‘ocr_text’]) && !empty($attachment[‘ocr_text’]) ) {
  96. update_post_meta($post[‘ID’], ‘ocr_text’, $attachment[‘ocr_text’]);
  97. }
  98. return $post;
  99. }
  100.  
  101. function Uninstall(){
  102. delete_option( ‘ocr_imagemagick_path’ );
  103. delete_option( ‘ocr_tesseract_path’ );
  104. delete_option( ‘ocr_resize_percent’ );
  105. }
  106. }
  107.  
  108. if(!$ocr_plugin){ $ocr_plugin = new OCR(); }
Add Comment
Please, Sign In to add comment