'', 'showoriginal' => 'true', 'colorized' => 'true' ), $atts ) ); if(extension_loaded('imagick')) { $img = new Imagick(esc_attr($file)); if($colorized == 'false' || $colorized == '0'){ /* leave hue at 100%, drop saturation by 100%, leave brightness at 100% */ $img->modulateImage(100, 0, 100); } /* Makke a clone of the image, not a reference */ $unscaledImg = clone $img; /* Get the Image Properties */ $imageprops = $img->getImageGeometry(); $imgWidth = $imageprops['width']; $imgHeight = $imageprops['height']; /* Defines the Resolution of the ASCII-Image */ $scaleFactor = 8; /* Calulate the new scaled size */ $imgWidthScaled = floor($imgWidth / $scaleFactor); $imgHeightScaled = floor($imgHeight / $scaleFactor); /* Scale the original image to cluster the Pixels */ $img->resizeImage($imgWidthScaled, $imgHeightScaled, imagick::FILTER_LANCZOS, 1); /* Imagemagick PixelIterator */ $it = $img->getPixelIterator(); $pixelColors = []; /* Loop trough pixel rows */ foreach( $it as $row => $pixels ){ $rowColors = []; /* Loop trough the pixels in the row (columns) */ foreach ( $pixels as $column => $pixel ){ /* getColor returns an rgba-Array */ $color = $pixel->getColor(); array_push($rowColors, $color); } array_push($pixelColors, $rowColors); /* Sync the iterator, this is important to do on each iteration */ $it->syncIterator(); } /* Values for the CSS Style */ $rowHeight = $imgHeight / $imgHeightScaled; $fontSize = ($imgWidth / $imgWidthScaled) * 2; $columnWidth = $imgWidth / $imgWidthScaled; /* return variable */ $ascii = ''; /* Loop trough PixelColors and print each Pixel */ $ascii .= '
'; foreach($pixelColors as $row){ $ascii .= '
'; /* Loop through each Column */ foreach($row as $column => $value){ $rgb = 'rgb('.$value['r'].','.$value['g'].','.$value['b'].')'; $ascii .= '#'; } $ascii .= '
'; } $ascii .= "
"; if($showoriginal == 'true' || $showoriginal == '1'){ $ascii .= sprintf( '' ); } return $ascii; } else { echo 'imagemagick missing.'; } } /** * Add custom buttons in TinyMCE. */ function registerButtons( $buttons ) { array_push( $buttons, '|', 'ascii' ); return $buttons; } /** * Register button scripts. */ function addButtons( $plugin_array ) { $plugin_array['ascii'] = plugins_url( 'tinymce/ascii.js' , __FILE__ ); return $plugin_array; } /** * Register buttons in init. */ function buttonsInit() { if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { return; } if ( true == get_user_option( 'rich_editing') ) { add_filter( 'mce_external_plugins', array( $this, 'addButtons' ) ); add_filter( 'mce_buttons', array( $this, 'registerButtons' ) ); } } /** * Displays the shortcode modal dialog. * * @return string Modal Dialog HTML. */ function dialog() { @ob_clean(); include plugin_dir_path( __FILE__ ) . 'tinymce/dialog.php'; die(); } } // close class. new Ascii_Factory_Shortcode;