/** * The callback ran in the async request to do the face detection. * * @access private * @since 1.0 * @static * @param array $atts The associative array that is passed to an instance of cnImage::get() when processing an immage. * @param string $source The image URL or absolute path. NOTE: The onl valid pathhs will be: WP_CONTENT/UPLOADS or STYLESHEETPATH * @param string $return What to return, @see cnImage::get() * * @return void */ public static function detect($atts, $source, $return) { if (!is_wp_error($info = cnImage::info($source))) { $name = $info['basename']; $cache = cnCache::get('face_detect', 'option-cache', 'cnfc'); $image = $cache != FALSE ? json_decode($cache, TRUE) : array(); if ($image == FALSE || (!isset($image[$name]) || $image[$name]['modified'] != $info['modified'])) { if (!class_exists('svay\\FaceDetector')) { include_once CNFD_PATH . 'vendor/facedetector/FaceDetector.php'; } $detect = new svay\FaceDetector(); $detect->faceDetect($info['path']); $coord = $detect->getFace(); if (!is_null($coord)) { $atts['crop_focus'] = array(($coord['x'] + $coord['w'] / 2) / $info[0], $coord['y'] / $info[1]); } $image[$name]['path'] = $info['path']; $image[$name]['modified'] = $info['modified']; $image[$name]['mime'] = $info['mime']; $image[$name]['width'] = $info[0]; $image[$name]['height'] = $info[1]; $image[$name]['face'] = $coord; $image[$name]['crop_focus'] = $atts['crop_focus']; if (!is_wp_error($result = cnImage::get($source, $atts))) { cnCache::set('face_detect', json_encode($image), YEAR_IN_SECONDS, 'option-cache', 'cnfc'); } } } }
return $saved; } //$image['name'] = ''; Does not change because it is being overwritten. $image['path'] = $saved['path']; //$image['url'] = ''; @todo This may need updated if the file name has changed. $image['width'] = $saved['width']; $image['height'] = $saved['height']; $image['size'] = "width=\"{$saved['width']}\" height=\"{$saved['height']}\""; $image['mime'] = $saved['mime-type']; //$image['type'] = 0; @todo This might need to be updated if the image type has changed. } return $image; } } // Init the Image API cnImage::init(); /*add_action( 'plugins_loaded', 'cn_short_init', 99 ); function cn_short_init() { if ( isset( $_GET['src'] ) ) { remove_all_actions( 'setup_theme' ); remove_all_actions( 'after_setup_theme' ); remove_all_actions( 'set_current_user' ); // remove_all_actions( 'init' ); remove_all_actions( 'widgets_init' ); remove_all_actions( 'register_sidebar' ); remove_all_actions( 'wp_default_scripts' ); remove_all_actions( 'wp_default_styles' ); remove_all_actions( 'admin_bar_init' );
public static function shortcode($atts, $content = '', $tag = 'cn_thumbr') { // Grab an instance of the Connections object. $instance = Connections_Directory(); $log = array(); $srcset = array(); $permitted = array('attachment', 'featured', 'path', 'url', 'logo', 'photo'); $defaults = array('type' => 'url', 'source' => NULL, 'negate' => FALSE, 'grayscale' => FALSE, 'brightness' => 0, 'colorize' => NULL, 'contrast' => 0, 'detect_edges' => FALSE, 'emboss' => FALSE, 'gaussian_blur' => FALSE, 'blur' => FALSE, 'sketchy' => FALSE, 'sharpen' => FALSE, 'smooth' => NULL, 'opacity' => 100, 'crop_mode' => 1, 'crop_focus' => array(0.5, 0.5), 'crop_only' => FALSE, 'canvas_color' => '#FFFFFF', 'quality' => 90, 'sizes' => '1024|640|320'); $defaults = apply_filters('cn_thumbr_shortcode_atts', $defaults); $atts = shortcode_atts($defaults, $atts, $tag); if (!in_array($atts['type'], $permitted)) { return __('Valid image source type not supplied.', 'connections'); } /* * Convert some of the $atts values in the array to boolean because the Shortcode API passes all values as strings. */ cnFormatting::toBoolean($atts['negate']); cnFormatting::toBoolean($atts['grayscale']); cnFormatting::toBoolean($atts['detect_edges']); cnFormatting::toBoolean($atts['emboss']); cnFormatting::toBoolean($atts['gaussian_blur']); cnFormatting::toBoolean($atts['blur']); cnFormatting::toBoolean($atts['sketchy']); cnFormatting::toBoolean($atts['sharpen']); // cnFormatting::toBoolean( $atts['crop'] ); cnFormatting::toBoolean($atts['crop_only']); $atts['sizes'] = explode('|', $atts['sizes']); array_map('trim', $atts['sizes']); array_map('absint', $atts['sizes']); if (empty($atts['sizes'])) { return __('No image sizes were supplied or supplied values were invalid.', 'connections'); } switch ($atts['type']) { case 'attachment': $source = wp_get_attachment_url(absint($atts['source'])); break; case 'featured': $source = wp_get_attachment_url(get_post_thumbnail_id()); break; case 'path': $source = $atts['source']; break; case 'url': $source = esc_url($atts['source']); break; case 'logo': $result = $instance->retrieve->entry(absint($atts['source'])); $entry = new cnEntry($result); $meta = $entry->getImageMeta(array('type' => 'logo')); if (is_wp_error($meta)) { // Display the error messages. return implode(PHP_EOL, $meta->get_error_messages()); } $source = $meta['url']; break; case 'photo': $result = $instance->retrieve->entry(absint($atts['source'])); $entry = new cnEntry($result); $meta = $entry->getImageMeta(array('type' => 'photo')); if (is_wp_error($meta)) { // Display the error messages. return implode(PHP_EOL, $meta->get_error_messages()); } $source = $meta['url']; break; } // Unset $atts['source'] because passing that $atts to cnImage::get() extracts and overwrite the $source var. unset($atts['source']); foreach ($atts['sizes'] as $width) { $atts['width'] = $width; $image = cnImage::get($source, $atts, 'data'); if (is_wp_error($image)) { // Display the error messages. return implode(PHP_EOL, $image->get_error_messages()); } elseif ($image === FALSE) { return __('An error has occured while creating the thumbnail.', 'connections'); } if (defined('WP_DEBUG') && WP_DEBUG === TRUE) { $log[] = '<pre>' . $image['log'] . '</pre>'; } $srcset[] = $image['url'] . ' ' . $width . 'w'; } $out = sprintf('<img class="cn-image" srcset="%1$s" sizes="100vw"%2$s />', implode(',', $srcset), empty($content) ? '' : ' alt="' . esc_attr($content) . '"'); if (defined('WP_DEBUG') && WP_DEBUG === TRUE) { $out .= implode('', $log); } return $out; }
/** * Process a logo upload, creating its size variation and caching it for later use. * * NOTE: The entry slug should be run thru rawurldecode() before being passed to this method. * * @access private * @since 8.1 * @static * @uses Connections_Directory() * @uses is_wp_error() * @uses cnMessage::set() * @uses cnImage::get() * @uses is_admin() * @param string $entrySlug The entry slug. * * @return array An associative array containing the the details about the uploaded logo. */ private static function processLogo($entrySlug) { if (!isset($_FILES['original_logo'])) { return FALSE; } // Grab an instance of the Connections object. $instance = Connections_Directory(); if (is_wp_error($img = cnImage::upload($_FILES['original_logo'], $entrySlug))) { cnMessage::set('error', implode('<br />', $img->get_error_messages())); return FALSE; } $cropMode = array(0 => 'none', 1 => 'crop', 2 => 'fill', 3 => 'fit'); $logo = cnImage::get($img['url'], array('crop_mode' => ($key = array_search(cnSettingsAPI::get('connections', 'image_logo', 'ratio'), $cropMode)) || $key === 0 ? $key : 2, 'width' => cnSettingsAPI::get('connections', 'image_logo', 'width'), 'height' => cnSettingsAPI::get('connections', 'image_logo', 'height'), 'quality' => cnSettingsAPI::get('connections', 'image_logo', 'quality'), 'sub_dir' => $entrySlug), 'data'); if (is_wp_error($logo)) { cnMessage::set('error', implode('<br />', $logo->get_error_messages())); } // Output the debug log. if ($instance->options->getDebug() && is_admin()) { if (isset($logo['log'])) { cnMessage::runtime('notice', 'Logo Image Process Log<br/> <pre>' . $logo['log'] . '</pre>'); } } return $img; }
/** * Return an array of image meta data. * * Accepted option for the $atts property are: * type (string) Valid options: logo | photo | custom. Default: photo * size (string) Valid options depend on `type`. * If `type` is `logo`: original | scaled. Default: original * If `type` is `photo`: original | thumbnail | medium | large. Default: original * If `type` is `custom`: Not used, use the `width` and `height` to set the custom size. * width (int) The width of the `custom` size. * height (int) The height of the `custom` size. * crop_mode (int) Which crop mode to utilize when rescaling the image. Valid range is 0–3. Default: 1 * 0 == Resize to Fit specified dimensions with no cropping. Aspect ratio will not be maintained. * 1 == Crop and resize to best fit dimensions maintaining aspect ration. Default. * 2 == Resize proportionally to fit entire image into specified dimensions, and add margins if required. * Use the canvas_color option to set the color to be used when adding margins. * 3 == Resize proportionally adjusting size of scaled image so there are no margins added. * quality (int) The image quality to be used when saving the image. Valid range is 1–100. Default: 80 * * The return array will contain the following keys and their value: * name => (string) The image name. * path => (string) The absolute image path. * url => (string) The image URL. * width => (int) The image width. * height => (int) The image height. * size => (string) The image size in a string, `height="yyy" width="xxx"`, that can be used directly in an img tag. * mime => (string) The image mime type. * type => (int) The IMAGETYPE_XXX constants indicating the type of the image. * * @access public * @since 8.1 * @uses apply_filters() * @uses wp_parse_args() * @uses self::getOriginalImageURL() * @uses self::getOriginalImagePath() * @uses cnImage::get() * @uses WP_Error * @uses is_wp_error() * @param array $atts * @return mixed array|WP_Error */ public function getImageMeta($atts = array()) { $cropMode = array(0 => 'none', 1 => 'crop', 2 => 'fill', 3 => 'fit'); $sizes = array('thumbnail', 'medium', 'large'); $meta = array(); $defaults = array('type' => 'photo', 'size' => 'original', 'width' => 0, 'height' => 0, 'crop_mode' => 1, 'quality' => 80); $defaults = apply_filters('cn_default_atts_image_meta', $defaults); $atts = wp_parse_args($atts, $defaults); if (empty($atts['type'])) { return $meta; } // The entry slug is saved in the db URL encoded, so it needs to be decoded. $slug = rawurldecode($this->getSlug()); if ('custom' == $atts['size']) { $meta = cnImage::get($this->getOriginalImageURL($atts['type']), array('crop_mode' => empty($atts['crop_mode']) && $atts['crop_mode'] !== 0 ? 1 : $atts['crop_mode'], 'width' => empty($atts['width']) ? NULL : $atts['width'], 'height' => empty($atts['height']) ? NULL : $atts['height'], 'quality' => $atts['quality'], 'sub_dir' => $slug), 'data'); if (!is_wp_error($meta)) { $meta['source'] = 'file'; } return $meta; } switch ($atts['type']) { case 'logo': switch ($atts['size']) { case 'original': $meta['path'] = $this->getOriginalImagePath($atts['type']); $meta['url'] = $this->getOriginalImageURL($atts['type']); if (isset($this->options['logo']['meta'])) { $meta = $this->options['logo']['meta']; // This needs to be here to ensure that the path and URL stored is updated // to the current path to account for users moving their site or changing // the site's folder structure. $meta['path'] = $this->getOriginalImagePath($atts['type']); $meta['url'] = $this->getOriginalImageURL($atts['type']); $meta['source'] = 'db'; } else { /** @noinspection PhpUsageOfSilenceOperatorInspection */ if (is_file($meta['path']) && ($image = @getimagesize($meta['path']))) { $meta['width'] = $image[0]; $meta['height'] = $image[1]; $meta['size'] = $image[3]; $meta['mime'] = $image['mime']; $meta['type'] = $image[2]; $meta['source'] = 'file'; } else { $meta = new WP_Error('image_not_found', __(sprintf('The file %s is not an image.', basename($meta['path'])), 'connections'), $meta['path']); } } break; default: $meta = cnImage::get($this->getOriginalImageURL($atts['type']), array('crop_mode' => ($key = array_search(cnSettingsAPI::get('connections', 'image_logo', 'ratio'), $cropMode)) || $key === 0 ? $key : 2, 'width' => cnSettingsAPI::get('connections', 'image_logo', 'width'), 'height' => cnSettingsAPI::get('connections', 'image_logo', 'height'), 'quality' => cnSettingsAPI::get('connections', 'image_logo', 'quality'), 'sub_dir' => $slug), 'data'); if (!is_wp_error($meta)) { $meta['source'] = 'file'; } break; } break; case 'photo': switch ($atts['size']) { case 'original': $meta['path'] = $this->getOriginalImagePath($atts['type']); $meta['url'] = $this->getOriginalImageURL($atts['type']); if (isset($this->options['image']['meta']['original'])) { $meta = $this->options['image']['meta']['original']; // This needs to be here to ensure that the path and URL stored is updated // to the current path to account for users moving their site or changing // the site's folder structure. $meta['path'] = $this->getOriginalImagePath($atts['type']); $meta['url'] = $this->getOriginalImageURL($atts['type']); $meta['source'] = 'db'; } else { /** @noinspection PhpUsageOfSilenceOperatorInspection */ if (is_file($meta['path']) && ($image = @getimagesize($meta['path']))) { $meta['width'] = $image[0]; $meta['height'] = $image[1]; $meta['size'] = $image[3]; $meta['mime'] = $image['mime']; $meta['type'] = $image[2]; $meta['source'] = 'file'; } else { $meta = new WP_Error('image_not_found', __(sprintf('The file %s is not an image.', basename($meta['path'])), 'connections'), $meta['path']); } } break; default: if (in_array($atts['size'], $sizes)) { $meta = cnImage::get($this->getOriginalImageURL($atts['type']), array('crop_mode' => ($key = array_search(cnSettingsAPI::get('connections', "image_{$atts['size']}", 'ratio'), $cropMode)) || $key === 0 ? $key : 2, 'width' => cnSettingsAPI::get('connections', "image_{$atts['size']}", 'width'), 'height' => cnSettingsAPI::get('connections', "image_{$atts['size']}", 'height'), 'quality' => cnSettingsAPI::get('connections', "image_{$atts['size']}", 'quality'), 'sub_dir' => $slug), 'data'); if (!is_wp_error($meta)) { $meta['source'] = 'file'; } } break; } break; } return $meta; }