/** * Returns a HQ_Image_Editor instance and loads file into it. * * @since 0.0.1 * * @param string $path Path to the file to load. * @param array $args Optional. Additional arguments for retrieving the image editor. * Default empty array. * @return HQ_Image_Editor|HQ_Error The HQ_Image_Editor object if successful, an HQ_Error * object otherwise. */ function hq_get_image_editor($path, $args = array()) { $args['path'] = $path; if (!isset($args['mime_type'])) { $file_info = hq_check_filetype($args['path']); // If $file_info['type'] is false, then we let the editor attempt to // figure out the file type, rather than forcing a failure based on extension. if (isset($file_info) && $file_info['type']) { $args['mime_type'] = $file_info['type']; } } $implementation = _hq_image_editor_choose($args); if ($implementation) { $editor = new $implementation($path); $loaded = $editor->load(); if (is_hq_error($loaded)) { return $loaded; } return $editor; } return new HQ_Error('image_no_editor', __('No editor could be selected.')); }
/** * Attempt to determine the real file type of a file. * * If unable to, the file name extension will be used to determine type. * * If it's determined that the extension does not match the file's real type, * then the "proper_filename" value will be set with a proper filename and extension. * * Currently this function only supports validating images known to getimagesize(). * * @since 3.0.0 * * @param string $file Full path to the file. * @param string $filename The name of the file (may differ from $file due to $file being * in a tmp directory). * @param array $mimes Optional. Key is the file extension with value as the mime type. * @return array Values for the extension, MIME, and either a corrected filename or false * if original $filename is valid. */ function hq_check_filetype_and_ext($file, $filename, $mimes = null) { $proper_filename = false; // Do basic extension validation and MIME mapping $hq_filetype = hq_check_filetype($filename, $mimes); $ext = $hq_filetype['ext']; $type = $hq_filetype['type']; // We can't do any further validation without a file to work with if (!file_exists($file)) { return compact('ext', 'type', 'proper_filename'); } // We're able to validate images using GD if ($type && 0 === strpos($type, 'image/') && function_exists('getimagesize')) { // Attempt to figure out what type of image it actually is $imgstats = @getimagesize($file); // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME if (!empty($imgstats['mime']) && $imgstats['mime'] != $type) { /** * Filter the list mapping image mime types to their respective extensions. * * @since 0.0.1 * * @param array $mime_to_ext Array of image mime types and their matching extensions. */ $mime_to_ext = apply_filters('getimagesize_mimes_to_exts', array('image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/tiff' => 'tif')); // Replace whatever is after the last period in the filename with the correct extension if (!empty($mime_to_ext[$imgstats['mime']])) { $filename_parts = explode('.', $filename); array_pop($filename_parts); $filename_parts[] = $mime_to_ext[$imgstats['mime']]; $new_filename = implode('.', $filename_parts); if ($new_filename != $filename) { $proper_filename = $new_filename; // Mark that it changed } // Redefine the extension / MIME $hq_filetype = hq_check_filetype($new_filename, $mimes); $ext = $hq_filetype['ext']; $type = $hq_filetype['type']; } } } /** * Filter the "real" file type of the given file. * * @since 0.0.1 * * @param array $hq_check_filetype_and_ext File data array containing 'ext', 'type', and * 'proper_filename' keys. * @param string $file Full path to the file. * @param string $filename The name of the file (may differ from $file due to * $file being in a tmp directory). * @param array $mimes Key is the file extension with value as the mime type. */ return apply_filters('hq_check_filetype_and_ext', compact('ext', 'type', 'proper_filename'), $file, $filename, $mimes); }
/** * Verifies an attachment is of a given type. * * @since 0.0.1 * * @param string $type Attachment type. Accepts 'image', 'audio', or 'video'. * @param int|HQ_Post $post_id Optional. Attachment ID. Default 0. * @return bool True if one of the accepted types, false otherwise. */ function hq_attachment_is($type, $post_id = 0) { if (!($post = get_post($post_id))) { return false; } if (!($file = get_attached_file($post->ID))) { return false; } if (0 === strpos($post->post_mime_type, $type . '/')) { return true; } $check = hq_check_filetype($file); if (empty($check['ext'])) { return false; } $ext = $check['ext']; if ('import' !== $post->post_mime_type) { return $type === $ext; } switch ($type) { case 'image': $image_exts = array('jpg', 'jpeg', 'jpe', 'gif', 'png'); return in_array($ext, $image_exts); case 'audio': return in_array($ext, hq_get_audio_extensions()); case 'video': return in_array($ext, hq_get_video_extensions()); default: return $type === $ext; } }