/** * Resizes the image with the given id according to the configured max width and height settings * renders a json response indicating success/failure and dies */ function imsanity_resize_image() { imsanity_verify_permission(); global $wpdb; $id = intval($_POST['id']); if (!$id) { $results = array('success' => false, 'message' => __('Missing ID Parameter', 'imsanity')); echo json_encode($results); die; } // @TODO: probably doesn't need the join...? $query = $wpdb->prepare("select\n\t\t\t\t{$wpdb->posts}.ID as ID,\n\t\t\t\t{$wpdb->posts}.guid as guid,\n\t\t\t\t{$wpdb->postmeta}.meta_value as file_meta\n\t\t\t\tfrom {$wpdb->posts}\n\t\t\t\tinner join {$wpdb->postmeta} on {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id and {$wpdb->postmeta}.meta_key = %s\n\t\t\t\twhere {$wpdb->posts}.ID = %d\n\t\t\t\tand {$wpdb->posts}.post_type = %s\n\t\t\t\tand {$wpdb->posts}.post_mime_type like %s", array('_wp_attachment_metadata', $id, 'attachment', 'image%')); $images = $wpdb->get_results($query); if ($images) { $image = $images[0]; $meta = unserialize($image->file_meta); $uploads = wp_upload_dir(); $oldPath = $uploads['basedir'] . "/" . $meta['file']; $maxW = imsanity_get_option('imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH); $maxH = imsanity_get_option('imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT); // method one - slow but accurate, get file size from file itself // list($oldW, $oldH) = getimagesize( $oldPath ); // method two - get file size from meta, fast but resize will fail if meta is out of sync $oldW = $meta['width']; $oldH = $meta['height']; if ($oldW > $maxW && $maxW > 0 || $oldH > $maxH && $maxH > 0) { $quality = imsanity_get_option('imsanity_quality', IMSANITY_DEFAULT_QUALITY); list($newW, $newH) = wp_constrain_dimensions($oldW, $oldH, $maxW, $maxH); $resizeResult = imsanity_image_resize($oldPath, $newW, $newH, false, null, null, $quality); // $resizeResult = new WP_Error('invalid_image', __('Could not read image size'), $oldPath); // uncommend to debug fail condition if (!is_wp_error($resizeResult)) { $newPath = $resizeResult; if ($newPath != $oldPath) { // remove original and replace with re-sized image unlink($oldPath); rename($newPath, $oldPath); } $meta['width'] = $newW; $meta['height'] = $newH; // @TODO replace custom query with update_post_meta $update_query = $wpdb->prepare("update {$wpdb->postmeta}\n\t\t\t\t\t\tset {$wpdb->postmeta}.meta_value = %s\n\t\t\t\t\t\twhere {$wpdb->postmeta}.post_id = %d\n\t\t\t\t\t\tand {$wpdb->postmeta}.meta_key = %s", array(maybe_serialize($meta), $image->ID, '_wp_attachment_metadata')); $wpdb->query($update_query); $results = array('success' => true, 'id' => $id, 'message' => sprintf(__('OK: %s', 'imsanity'), $oldPath)); } else { $results = array('success' => false, 'id' => $id, 'message' => sprintf(__('ERROR: %s (%s)', 'imsanity'), $oldPath, htmlentities($resizeResult->get_error_message()))); } } else { $results = array('success' => true, 'id' => $id, 'message' => sprintf(__('SKIPPED: %s (Resize not required)', 'imsanity'), $oldPath)); } } else { $results = array('success' => false, 'id' => $id, 'message' => sprintf(__('ERROR: (Attachment with ID of %s not found) ', 'imsanity'), htmlentities($id))); } // if there is a quota we need to reset the directory size cache so it will re-calculate delete_transient('dirsize_cache'); echo json_encode($results); die; // required by wordpress }
/** * 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; }
/** * 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; }