-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-classify.php
158 lines (115 loc) · 4.82 KB
/
image-classify.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Plugin Name: Image Classify
* Description: Block to classify image and allow or block upload.
* Requires at least: 6.4.2
* Requires PHP: 7.0
* Version: 1.0.0
* Author: UjW0L
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: image-classify
*
* @package create-block
*/
namespace ctcImageClassify;
if ( ! defined( 'ABSPATH' ) ) exit;
class ctcImageClassify{
public function __construct(){
define('CTCIC__DIR__PATH',plugin_dir_url(__FILE__) );
SELF::requiredWpAction();
}
/**
* @since 0.1.0
*
* Register required wp action
*/
public function requiredWpAction(){
add_action( 'init', array($this,'create_block_image_classify_block_init') );
add_action('wp_ajax_image_classify_uploadImage',array($this,'image_classify_uploadImage'));
add_action('wp_ajax_nopriv_image_classify_uploadImage', array($this ,'image_classify_uploadImage'));
}
/**
* Upload image with AJAX
*/
public function image_classify_uploadImage(){
check_ajax_referer( 'my_ajax_nonce', 'nonce' );
$ext = 'jpg'== $_POST['ext'] ? 'jpeg' : sanitize_text_field( $_POST['ext']);
$fileName = time().'.'.$ext;
$outPutFile = wp_upload_dir()['path'].'/'. $fileName;
$upload_file = file_put_contents($outPutFile, base64_decode(str_replace(' ', '+',str_replace('data:image/'.$ext.';base64,', '',$_POST['blob']))));
$attachment = array(
'guid' => wp_upload_dir()['url'] . '/' . $fileName,
'post_mime_type' => wp_check_filetype( basename( $fileName ), null )['type'],
'post_title' => sanitize_file_name( pathinfo( basename( $outPutFile), PATHINFO_FILENAME ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment );
if(is_numeric($attach_id)):
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $outPutFile);
wp_update_attachment_metadata( $attach_id, $attach_data );
echo esc_html(__('Image sucessfully uploaded','image-classify'));
else:
echo esc_html(__('Image could not be uploaded','image-classify'));
endif;
wp_die();
}
/**
* Since 0.1.0
* @param $atts atrributes
* @param $content
*
*/
public function renderCallBack($atts, $content){
$allow = $atts["allowImage"] ? 'allow':'noAllow';
$nonce = wp_create_nonce( 'my_ajax_nonce' );
ob_start();
?>
<div></div>
<div class="image-classify-cont" data-nonce= "<?php echo (esc_attr(($nonce))); ?>" data-info='<?php echo (esc_attr(($atts['ctcIcParam'])));?>' data-conf="<?php echo (esc_attr( ($atts["minProbabilty"]/100)));?>" data-train-images='<?php echo (esc_attr(wp_json_encode(($atts["trainData"]))))?>' data-allow="<?php echo(esc_attr($allow));?>" >
<div><span><?php echo (esc_html(__("Select Image: ", 'image-classify')))?></span><span><input class="select-img" type="file" accept='image/*' /></span><span class="loading-result"></span></div>
</div>
<?php
return ob_get_clean();
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*
*
*/
public function create_block_image_classify_block_init() {
$params = array(
'invalidImage'=>esc_html(__('This kind of image is not allowed.','image-classify')),
'validImage'=>esc_html(__('Image allowed. Would you like to upload it?.','image-classify')),
'modelLoading'=>esc_html(__('Loading ....','image-classify')),
'modelLoaded'=>esc_html(__('Done.','image-classify')),
'ajaxUrl'=> admin_url( 'admin-ajax.php' ),
);
register_block_type( __DIR__ . '/build' ,
array('attributes' => array(
"ctcIcParam"=> ["type"=>'string', "default"=>json_encode($params)],
"allowImage"=>["type"=>'boolean', "default"=>true],
"labelImg1"=>["type"=>"array", "default"=>[]],
"labelImg2"=>["type"=>"array", "default"=>[]],
"labelImg3"=>["type"=>"array", "default"=>[]],
"labelImg4"=>["type"=>"array", "default"=>[]],
"labelImg5"=>["type"=>"array", "default"=>[]],
"createModel"=>["type"=>'boolean',"default"=>false],
"trainButtonDis"=>["type"=>'boolean',"default"=>true],
"trainData"=>["type"=>"array","default"=>[]],
"minProbabilty"=> ["type"=>'number',"default"=>70 ],
),
"render_callback"=> array($this, 'renderCallBack') ,
)
);
}
}
new ctcImageClassify();