/** * * Include all required scripts and styles * * @since 0.0.1 * **/ function bedev_include_files() { wp_enqueue_style('guillotine-css', plugins_url() . '/bedev-imagemagick/apps/guillotine/css/jquery.guillotine.css'); wp_enqueue_style('bedev-imagemagick-css', plugins_url() . '/bedev-imagemagick/inc/css/bedev-imagemagick.css'); wp_enqueue_style('bedev-font-awesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'); wp_enqueue_script('guillotine-js', plugins_url() . '/bedev-imagemagick/apps/guillotine/js/jquery.guillotine.js', array('jquery'), '1.0.0', true); wp_enqueue_script('bedev-imagemagick-js', plugins_url() . '/bedev-imagemagick/inc/js/bedev-imagemagick.js', array('jquery', 'guillotine-js', 'bedev-facebook-share'), '0.0.1', true); wp_enqueue_script('jquery-form', array('jquery'), false, true); //get the options $imagick_options = new bedev_imagemagick_options(); //get all the social network details $all_social_sites = $imagick_options->registered_sites; //get the front end path for the image editor $path = $imagick_options->options['front-end-path']; //get the pre-defined share text in case it's required front end $text = $imagick_options->link_description; $variables = array('url' => admin_url('admin-ajax.php'), 'path' => $path, 'description' => $text); foreach ($all_social_sites as $site => $all_social_site) { //set variables to send to the front end $variables[$site] = $imagick_options->get_social_network_info($site); //enqueue social network specific scripts wp_enqueue_script('bedev-' . $site . '-share', plugins_url() . '/bedev-imagemagick/inc/js/' . $site . '-share.js', array('jquery'), '0.0.1', true); } wp_localize_script('guillotine-js', 'ajax', $variables); }
/** * * Do ImageMagick * * Handle the AJAX request containing image chop instructions, then create and save a branded montage * * @since 0.0.1 * **/ function bedev_do_imagemagick() { /*if ( ! isset( $_POST['_wp_nonce_bedev_imagemagick'] ) || ! wp_verify_nonce( $_POST['_wp_nonce_bedev_imagemagick'], 'bedev_do_imagemagick' ) ) { $response = array( 'status' => 'error', 'message' => 'Security error: either something has gone wrong, or you are being very naughty.', ); } else {*/ //get the image class $imagick_options = new bedev_imagemagick_options(); //get the social network details $imagic_details = $imagick_options->get_social_network_info($_POST['network']); //get the montage dimensions $imagick_dimensions = $imagic_details['image_sizes']; //get the network's set overlay if there is one if (isset($imagic_details['montage-overlay'])) { $imagic_overlay = $imagic_details['montage-overlay']; } else { $imagic_overlay = false; } //create and process all images received according to their received instructions for ($x = 0; $x < $_POST['number-images']; $x++) { //get the path to the file $bedev_new_image_path = get_attached_file($_POST['attachment-' . $x]); //get the image editor $bedev_image_edit['image-' . $x] = wp_get_image_editor($bedev_new_image_path); //get the edit instructions $bedev_new_image = $bedev_image_edit['image-' . $x]->translate_front_end_instructions($_POST['image-' . $x]); //process the image according to instructions //1.rotate $bedev_image_edit['image-' . $x]->rotate(360 - $bedev_new_image['rotate']); //2. crop $bedev_image_edit['image-' . $x]->crop($bedev_new_image['x'], $bedev_new_image['y'], $bedev_new_image['width'], $bedev_new_image['height']); //3. resize it to the required size, N.B. image will be enlarged if required $resize = $bedev_image_edit['image-' . $x]->resizeImage($imagick_dimensions['width'] / 2, $imagick_dimensions['height'], true); } //create the montage $master_image = $bedev_image_edit['image-0']; $additional_image = $bedev_image_edit['image-1']; $montage = $master_image->montage($additional_image, $imagick_dimensions); //add the overlay if there is one if ($imagic_overlay) { $overlay_path = get_attached_file($imagic_overlay); $overlay_bedev_object = wp_get_image_editor($overlay_path); $overlay_bedev_object->resizeImage($imagick_dimensions['width'], $imagick_dimensions['height'], true); $overlay_object = $overlay_bedev_object->get_imagick(); $montage->compositeImage($overlay_object, Imagick::COMPOSITE_DEFAULT, 0, 0); } $path = wp_upload_dir(); $path = $path['path']; //save the result temporarily $bedev_image_path = $master_image->generate_filename('montage', $path, 'jpg'); $montage->writeImage($bedev_image_path); //process into WordPress if (!function_exists('media_handle_upload')) { require_once ABSPATH . "wp-admin" . '/includes/image.php'; require_once ABSPATH . "wp-admin" . '/includes/file.php'; require_once ABSPATH . "wp-admin" . '/includes/media.php'; } //store and process the image into WordPress //NB it's expected that media_handle_sideload() will also delete the tmp file we save earlier $file_array = array('tmp_name' => $bedev_image_path, 'name' => basename($bedev_image_path), 'type' => 'image/jpeg', 'error' => 0, 'size' => filesize($bedev_image_path)); $id = media_handle_sideload($file_array, 0); $unique_ref = time(); //save a random identifier in the images meta data update_post_meta($id, 'share-identifier', $unique_ref); //get the image src $image_src = wp_get_attachment_url($id); //generate social share buttons that have been registered by the administrator $social_buttons = bedev_get_social_share_button($id, $_POST['network']); $response = array('status' => 'success', 'montage' => $unique_ref, 'src' => $image_src, 'buttons' => $social_buttons, 'debug' => array('overlay_path' => $overlay_path, 'upload_dir' => $path, 'tmp_path' => $bedev_image_path, 'sideload_result' => $id, 'image_path' => $image_path_1, 'request' => $_POST)); //} echo json_encode($response); die; }
/** * * Display Guillotine Image Editor linked to BeDev ImageMagick * * This function is intended to generate the markup for the BeDev ImageMagick montage generator * It can be called using the [bedev_show_guillotine] shortcode or by calling directly in php * * @param $args arr an array of arguments * * @since 0.0.1 * **/ function bedev_show_guillotine($args = null) { //get the options $imagick_options = new bedev_imagemagick_options(); //get the facebook details //later to be replaced with an option sent in the ajax request $imagic_details = $imagick_options->get_social_network_info('facebook'); //load the images //check if image ids have been provided in $args, if not use the demo images stored in the DB if (!isset($args['images'])) { $images = $imagick_options->options['montage-images']; } else { //check if an array of image IDs has been provided in $args, if so, use it if (is_array($args['images'])) { if (count($args['images'] !== 2)) { return 'Please provide 2 image IDs if providing an array.'; } $images = $args['images']; //else if a string has been provided, check if it's appropriate to convert to an array } else { $array_check = substr_count($args['images'], ','); if ($array_check !== 1) { return 'Please check your image request and only provide 2 image IDs separated by a comma, or an array of image IDs'; } $images = explode(',', $args['images']); } } //load the overlay IDs if (!isset($args['overlay'])) { $overlay = $imagic_details['montage-overlay']; } else { $overlay = $args['overlay']; } ob_start(); //create a wrapper for the ImageMagick elements ?> <div id="bedev-imagemagick-container"> <img id="bedev-imagemagick-loader" alt="bedev-imagemagick-loader" src="<?php echo plugins_url() . '/bedev-imagemagick/inc/images/loading-gif-1.gif'; ?> " style="display:none;"/> <div id="bedev-imagemagick-response" style="display:none;"></div> <div id="bedev-imagemagick-initial"> <?php foreach ($images as $key => $image) { //create one image and button set per image $image_url = wp_get_attachment_url($image); ?> <div id="theparent-<?php echo $key; ?> " class="bedev-image-parent" style="width: 48%; float: left;margin:2px;"> <img id="thepicture-<?php echo $key; ?> " class="bedev-image-picture" data-image-control="<?php echo $key; ?> " src="<?php echo $image_url; ?> "> </div> <?php } ?> </div> <?php foreach ($images as $key => $image) { //create one image and button set per image $image_url = wp_get_attachment_url($image); ?> <div id="theparent-<?php echo $key; ?> " class="bedev-image-parent" style="width: 48%; float: left;margin:2px;"> <img id="thepicture-<?php echo $key; ?> " class="bedev-image-picture" data-image-control="<?php echo $key; ?> " src="<?php echo $image_url; ?> "> <div id="controls"> <button id="rotateLeft-<?php echo $key; ?> " class="rotate-left" data-image-control="rotate-left-<?php echo $key; ?> " type='button' title='Rotate left'><i class="fa fa-undo"></i></button> <button id="zoomOut-<?php echo $key; ?> " class="zoom-out" data-image-control="zoom-out-<?php echo $key; ?> " type='button' title='Zoom out'><i class="fa fa-search-minus"></i></button> <button id="fit-<?php echo $key; ?> " class="fit" data-image-control="fit-<?php echo $key; ?> " type='button' title='Fit image'><i class="fa fa-refresh"></i></button> <button id="zoomIn-<?php echo $key; ?> " class="zoom-in" data-image-control="zoom-in-<?php echo $key; ?> " type='button' title='Zoom in'><i class="fa fa-search-plus"></i></button> <button id="rotateRight-<?php echo $key; ?> " class="rotate-right" data-image-control="rotate-right-<?php echo $key; ?> " type='button' title='Rotate right'><i class="fa fa-repeat"></i></button> </div> </div> <?php } ?> <form role="form" id="bedev-image-magick-form" action="#" method="post" enctype="multipart/form-data"> <input type="hidden" name="action" value="bedev_do_imagemagick"/> <?php foreach ($images as $key => $image) { ?> <input type="hidden" name="attachment-<?php echo $key; ?> " value="<?php echo $image; ?> "/> <?php } ?> <input type="hidden" name="number-images" value="<?php echo count($images); ?> "/> <input type="hidden" name="overlay" value="<?php echo $overlay; ?> "/> <?php wp_nonce_field('bedev_do_imagemagick', '_wp_nonce_bedev_imagemagick', false, true); ?> <input type="submit" id='imagemagick-submit' title='imagemagick-submit'> </form> </div> <?php $html = ob_get_contents(); if ($html) { ob_end_clean(); } return $html; }