-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_image.php
More file actions
134 lines (98 loc) · 4.06 KB
/
transform_image.php
File metadata and controls
134 lines (98 loc) · 4.06 KB
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
<?php
/**
* Plugin Name: Transform Image Plugin
* Description: Take a image and difficault level and transform it into pixel art
* Version: 1.0
* Author: Liki Crus
*/
// Exit if accessed directly
if (!defined('ABSPATH')) exit;
require 'pixelart_lib.php';
if (!class_exists('TransferImagePlugin')) :
class TransferImagePlugin
{
public static $_instance;
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function init()
{
add_action('admin_menu', array($this, 'plugin_admin_page'));
add_shortcode('pixelart', array($this, "add_transform_img_shortcode"));
add_action('wp_enqueue_scripts', array($this, 'pa_load_scripts'));
add_action('wp_ajax_image_submission', array($this, 'pa_image_submission_cb'));
add_action('wp_ajax_nopriv_image_submission', array($this, 'pa_image_submission_cb'));
add_filter('admin_init', array($this, 'check_upload_dir_change'), 999);
}
public function pa_load_scripts()
{
if (!wp_script_is('jquery', 'enqueued')) {
wp_enqueue_script('jquery');
}
wp_enqueue_script('pa_script', plugin_dir_url(__FILE__) . 'assets/js/pa_script.js');
$data = array(
'upload_url' => admin_url('async-upload.php'),
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('image-submission')
);
wp_localize_script('pa_script', 'pa_config', $data);
}
function check_upload_dir_change()
{
global $pagenow;
if (!empty($_POST['action'] && $_POST['action'] == 'image_submission') && ('admin-ajax.php' == $pagenow)) {
add_filter('upload_dir', array($this, 'set_pa_upload_dir'));
}
}
function set_pa_upload_dir($upload)
{
$upload['subdir'] = '/pixelarts' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
function pa_image_submission_cb()
{
check_ajax_referer('image-submission');
$file_id = "async-upload";
$upload_overrides = array('test_form' => false);
$upload_result = wp_handle_upload($_FILES[$file_id], $upload_overrides);
if (isset($upload_result['error'])) {
wp_send_json_error(array('msg' => 'Validation failed. Please try again later.'));
}
# Creating the pixel art now.
$file = $upload_result['file'];
$path_info = pathinfo($file);
$output_filename = $path_info['filename'] . "_pa";
$output_file = $path_info['dirname'] . "/" . $output_filename;
$box_count = intval($_REQUEST['box_count']);
$box_size = floatval($_REQUEST['box_size']);
$output_file_gen = create_fixed_pixel_art($file, $output_file, $box_count, $box_size);
$url = str_replace($path_info['basename'], $output_file_gen, $upload_result['url']);
remove_filter('upload_dir', array($this, 'set_pa_upload_dir'));
wp_send_json_success(array(
'msg' => 'Your image uploaded successfully.',
'url' => $url,
//'path' => $upload_result['file'], // NOTE: Should be removed on server.
));
}
public function plugin_admin_page()
{
// Admin functions
}
// Shortcut function
public static function add_transform_img_shortcode($atts, $content = '')
{
ob_start();
include_once '_form.php';
$html = ob_get_contents();
ob_end_clean();
return $html;
}
}
TransferImagePlugin::instance()->init();
endif;