/** * create_jpg_from_bmp Create a JPG image from a BMP file * * @since 1.0.0 * @param Mixed $params * @return Array Array containing settings from newly created image */ private function create_jpg_from_bmp($params) { $bmp = $this->image_create_from_bmp($params['file']); $oldPath = $params['file']; $uploads = wp_upload_dir(); $oldFileName = basename($params['file']); $newFileName = basename(str_ireplace(".bmp", ".jpg", $oldFileName)); $newFileName = wp_unique_filename($uploads['path'], $newFileName); // Remove old bmp file unlink($params['file']); if (imagejpeg($bmp, trailingslashit($uploads['path']) . $newFileName, $this->bmp_conversion_quality)) { $params['file'] = $uploads['path'] . '/' . $newFileName; $params['url'] = $uploads['url'] . '/' . $newFileName; $params['type'] = 'image/jpeg'; return $params; } return wp_handle_upload_error($oldPath, __('We couldn\'t convert the BMP to JPG. Please try again. If you continue to see this error you may need tot disable the BMP-To_JPG feauture.', $this->plugin_slug)); }
/** * If the uploaded image is a bmp this function handles the details of converting * the bmp to a jpg, saves the new file and adjusts the params array as necessary * * @param array $params */ function imsanity_bmp_to_jpg($params) { // read in the bmp file and then save as a new jpg file. // if successful, remove the original bmp and alter the return // parameters to return the new jpg instead of the bmp include_once 'libs/imagecreatefrombmp.php'; $bmp = imagecreatefrombmp($params['file']); // we need to change the extension from .bmp to .jpg so we have to ensure it will be a unique filename $uploads = wp_upload_dir(); $oldFileName = basename($params['file']); $newFileName = basename(str_ireplace(".bmp", ".jpg", $oldFileName)); $newFileName = wp_unique_filename($uploads['path'], $newFileName); $quality = imsanity_get_option('imsanity_quality', IMSANITY_DEFAULT_QUALITY); if (imagejpeg($bmp, $uploads['path'] . '/' . $newFileName, $quality)) { // conversion succeeded. remove the original bmp & remap the params unlink($params['file']); $params['file'] = $uploads['path'] . '/' . $newFileName; $params['url'] = $uploads['url'] . '/' . $newFileName; $params['type'] = 'image/jpeg'; } else { unlink($params['file']); return wp_handle_upload_error($oldPath, __("Oh Snap! Imsanity was Unable to process the BMP file. " . "If you continue to see this error you may need to disable the BMP-To-JPG " . "feature in Imsanity settings.", 'imsanity')); } return $params; }
/** * Handler after a file has been uploaded. If the file is an image, check the size * to see if it is too big and, if so, resize and overwrite the original * @param Array $params */ function ncf_imsanity_handle_upload($params) { $options = ncf_get_options(); for ($i = 1; $i <= $options['ncf_forms']; $i++) { if (isset($_FILES['ncf_pic_' . $i]) && $_FILES['ncf_pic_' . $i]['size'] > 0) { /* debug logging... */ // file_put_contents ( "debug.txt" , print_r($params,1) . "\n" ); /* $option_convert_bmp = IMSANITY_DEFAULT_BMP_TO_JPG; if ($params['type'] == 'image/bmp' && $option_convert_bmp) { $params = imsanity_bmp_to_jpg($params); }*/ // make sure this is a type of image that we want to convert and that it exists // @TODO when uploads occur via RPC the image may not exist at this location $oldPath = $params['file']; if (!is_wp_error($params) && file_exists($oldPath) && in_array($params['type'], array('image/png', 'image/gif', 'image/jpeg'))) { // figure out where the upload is coming from $source = imsanity_get_source(); list($maxW, $maxH) = imsanity_get_max_width_height($source); list($oldW, $oldH) = getimagesize($oldPath); /* HACK: if getimagesize returns an incorrect value (sometimes due to bad EXIF data..?) $img = imagecreatefromjpeg ($oldPath); $oldW = imagesx ($img); $oldH = imagesy ($img); imagedestroy ($img); //*/ /* HACK: an animated gif may have different frame sizes. to get the "screen" size $data = ''; // TODO: convert file to binary $header = unpack('@6/vwidth/vheight', $data ); $oldW = $header['width']; $oldH = $header['width']; //*/ if ($oldW > $maxW && $maxW > 0 || $oldH > $maxH && $maxH > 0) { $quality = IMSANITY_DEFAULT_QUALITY; list($newW, $newH) = wp_constrain_dimensions($oldW, $oldH, $maxW, $maxH); // this is wordpress prior to 3.5 (image_resize deprecated as of 3.5) //$resizeResult = imsanity_image_resize( $oldPath, $newW, $newH, true, null, null, $quality); $resizeResult = imsanity_image_resize($oldPath, $maxW, $maxH, true, null, null, $quality); /* uncomment to debug error handling code: */ // $resizeResult = new WP_Error('invalid_image', __(print_r($_REQUEST)), $oldPath); // regardless of success/fail we're going to remove the original upload unlink($oldPath); if (!is_wp_error($resizeResult)) { $newPath = $resizeResult; // remove original and replace with re-sized image rename($newPath, $oldPath); } else { // resize didn't work, likely because the image processing libraries are missing $params = wp_handle_upload_error($oldPath, sprintf(__("Oh Snap! Imsanity was unable to resize this image " . "for the following reason: '%s'\n\t\t\t\t\t\t. If you continue to see this error message, you may need to either install missing server" . " components or disable the Imsanity plugin." . " If you think you have discovered a bug, please report it on the Imsanity support forum.", 'imsanity'), $resizeResult->get_error_message())); } } } } } return $params; }
/** * read in the image file from the params and then save as a new jpg file. * if successful, remove the original image and alter the return * parameters to return the new jpg instead of the original * * @param string 'bmp' or 'png' * @param array $params * @return array altered params */ function imsanity_convert_to_jpg($type, $params) { $img = null; if ($type == 'bmp') { include_once 'libs/imagecreatefrombmp.php'; $img = imagecreatefrombmp($params['file']); } elseif ($type == 'png') { if (!function_exists('imagecreatefrompng')) { return wp_handle_upload_error($params['file'], 'imsanity_convert_to_jpg requires gd library enabled'); } $img = imagecreatefrompng($params['file']); } else { return wp_handle_upload_error($params['file'], 'Unknown image type specified in imsanity_convert_to_jpg'); } // we need to change the extension from the original to .jpg so we have to ensure it will be a unique filename $uploads = wp_upload_dir(); $oldFileName = basename($params['file']); $newFileName = basename(str_ireplace("." . $type, ".jpg", $oldFileName)); $newFileName = wp_unique_filename($uploads['path'], $newFileName); $quality = imsanity_get_option('imsanity_quality', IMSANITY_DEFAULT_QUALITY); if (imagejpeg($img, $uploads['path'] . '/' . $newFileName, $quality)) { // conversion succeeded. remove the original bmp & remap the params unlink($params['file']); $params['file'] = $uploads['path'] . '/' . $newFileName; $params['url'] = $uploads['url'] . '/' . $newFileName; $params['type'] = 'image/jpeg'; } else { unlink($params['file']); return wp_handle_upload_error($oldPath, __("Oh Snap! Imsanity was Unable to process the {$type} file. " . "If you continue to see this error you may need to disable the {$type}-To-JPG " . "feature in Imsanity settings.", 'imsanity')); } return $params; }
/** * If the uploaded image is a bmp this function handles the details of converting * the bmp to a jpg, saves the new file and adjusts the params array as necessary * * @param array $params */ function bulk_resize_bmp_to_jpg($params) { include_once 'functions/imagecreatefrombmp.php'; $bmp = imagecreatefrombmp($params['file']); // we need to change the extension from .bmp to .jpg so we have to ensure it will be a unique filename $uploads = wp_upload_dir(); $oldFileName = basename($params['file']); $newFileName = basename(str_ireplace(".bmp", ".jpg", $oldFileName)); $newFileName = wp_unique_filename($uploads['path'], $newFileName); $quality = bulk_resize_get_option('bulk_resize_quality', _DEFAULT_QUALITY); if (imagejpeg($bmp, $uploads['path'] . '/' . $newFileName, $quality)) { // conversion succeeded. remove the original bmp & remap the params unlink($params['file']); $params['file'] = $uploads['path'] . '/' . $newFileName; $params['url'] = $uploads['url'] . '/' . $newFileName; $params['type'] = 'image/jpeg'; } else { unlink($params['file']); return wp_handle_upload_error($oldPath, "Oh Snap! Bulk Resize Media was Unable to process the BMP file. " . "If you continue to see this error you may need to disable the BMP-To-JPG " . "feature in Bulk Resize Media settings."); } return $params; }