Exemplo n.º 1
0
/**
 * 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 bulk_resize_resize_image()
{
    bulk_resize_verify_permission();
    global $wpdb;
    $id = intval($_POST['id']);
    if (!$id) {
        $results = array('success' => false, 'message' => 'Missing ID Parameter');
        echo json_encode($results);
        die;
    }
    // @TODO: probably doesn't need the join...?
    $query = $wpdb->prepare("select\n\t\t\t\t\t{$wpdb->posts}.ID as ID,\n\t\t\t\t\t{$wpdb->posts}.guid as guid,\n\t\t\t\t\t{$wpdb->postmeta}.meta_value as file_meta\n\t\t\t\t\tfrom {$wpdb->posts}\n\t\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\t\twhere  {$wpdb->posts}.ID = %d\n\t\t\t\t\tand {$wpdb->posts}.post_type = %s\n\t\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 = bulk_resize_get_option('bulk_resize_max_width', _DEFAULT_MAX_WIDTH);
        $maxH = bulk_resize_get_option('bulk_resize_max_height', _DEFAULT_MAX_HEIGHT);
        if (file_exists($oldPath)) {
            list($oldW, $oldH) = @getimagesize($oldPath);
        }
        if ($oldW > $maxW && $maxW > 0 || $oldH > $maxH && $maxH > 0) {
            $quality = bulk_resize_get_option('bulk_resize_quality', _DEFAULT_QUALITY);
            list($newW, $newH) = wp_constrain_dimensions($oldW, $oldH, $maxW, $maxH);
            $resizeResult = 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 && file_exists($oldPath) && !is_dir($oldPath)) {
                    // remove original and replace with re-sized image
                    unlink($oldPath);
                    rename($newPath, $oldPath);
                    $meta['width'] = $newW;
                    $meta['height'] = $newH;
                    update_post_meta($image->ID, '_wp_attachment_metadata', $meta);
                    $results = array('success' => true, 'id' => $id, 'message' => '<strong>OK:</strong> ' . $oldPath);
                }
            } else {
                $results = array('success' => false, 'id' => $id, 'message' => '<strong>ERROR:</strong> ' . $oldPath . ' (' . htmlentities($resizeResult->get_error_message()) . ')');
            }
        } else {
            $results = array('success' => true, 'id' => $id, 'message' => '<strong>SKIPPED:</strong> <span style="color:#777;">' . $oldPath . '</span> <em style="color:#21759B">(Resize not required)</em>');
        }
    } else {
        $results = array('success' => false, 'id' => $id, 'message' => '<strong>ERROR:</strong> (Attachment with ID of ' . htmlentities($id) . ' not found)');
    }
    echo json_encode($results);
    die;
    // required by wordpress
}
Exemplo n.º 2
0
/**
 * Render the site settings form.  This is processed by
 * WordPress built-in options persistance mechanism
 */
function bulk_resize_settings_page_form()
{
    ?>
		<form method="post" action="options.php" style="width:300px; padding: 0 20px; margin: 20px 20px 0 0 ; float: left; background: #f6f6f6; border: 1px solid #e5e5e5; ">
			
			<h2 style="margin-top: 0px;">Settings</h2>
			
			<?php 
    settings_fields('bulk-resize-settings-group');
    ?>
			
			<h4>Posts, Pages, Custom Post Types</h4>

			W: <input type="text" style="width:40px;" name="bulk_resize_max_width" value="<?php 
    echo bulk_resize_get_option('bulk_resize_max_width', _DEFAULT_MAX_WIDTH);
    ?>
" />
			/ H: <input type="text" style="width:40px;" name="bulk_resize_max_height" value="<?php 
    echo bulk_resize_get_option('bulk_resize_max_height', _DEFAULT_MAX_HEIGHT);
    ?>
" /> (Enter 0 to disable)
			
			<h4>Images uploaded directly to the Media Library</h4>
			
			W: <input type="text" style="width:40px;" name="bulk_resize_max_width_library" value="<?php 
    echo bulk_resize_get_option('bulk_resize_max_width_library', _DEFAULT_MAX_WIDTH);
    ?>
" />
			/ H: <input type="text" style="width:40px;" name="bulk_resize_max_height_library" value="<?php 
    echo bulk_resize_get_option('bulk_resize_max_height_library', _DEFAULT_MAX_HEIGHT);
    ?>
" /> (Enter 0 to disable)
			
	
			<h4>Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)</h4>
			
			W: <input type="text" style="width:40px;" name="bulk_resize_max_width_other" value="<?php 
    echo bulk_resize_get_option('bulk_resize_max_width_other', _DEFAULT_MAX_WIDTH);
    ?>
" />
			/ H: <input type="text" style="width:40px;" name="bulk_resize_max_height_other" value="<?php 
    echo bulk_resize_get_option('bulk_resize_max_height_other', _DEFAULT_MAX_HEIGHT);
    ?>
" /> (Enter 0 to disable)
	
	
			<h4>JPG image quality</h4>
			<select name="bulk_resize_quality">
				<?php 
    $q = bulk_resize_get_option('bulk_resize_quality', _DEFAULT_QUALITY);
    for ($x = 50; $x <= 100; $x = $x + 10) {
        echo "<option" . ($q == $x ? " selected='selected'" : "") . ">{$x}</option>";
    }
    ?>
			</select> (WordPress default is 90)
			
			<h4>Convert BMP To JPG</h4>
			<select name="bulk_resize_bmp_to_jpg">
				<option <?php 
    if (bulk_resize_get_option('bulk_resize_bmp_to_jpg', _DEFAULT_BMP_TO_JPG) == "1") {
        echo "selected='selected'";
    }
    ?>
 value="1">Yes</option>
				<option <?php 
    if (bulk_resize_get_option('bulk_resize_bmp_to_jpg', _DEFAULT_BMP_TO_JPG) == "0") {
        echo "selected='selected'";
    }
    ?>
 value="0">No</option>
			</select>
		
			<p class="submit"><input type="submit" class="button-primary" value="<?php 
    _e('Save Changes');
    ?>
" /></p>
		
		</form>
		<?php 
}
Exemplo n.º 3
0
/**
 * 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;
}