wp_save_image

Definition:
function wp_save_image($post_id) {}

Parameters

  • $post_id

Defined filters

  • admin_memory_limit
    apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT )

Source code

function wp_save_image($post_id) {

	$return = new stdClass;

	$success = $delete = $scaled = $nocrop = false;

	$post = get_post($post_id);

	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );

	$img = load_image_to_edit($post_id, $post->post_mime_type);



	if ( !is_resource($img) ) {

		$return->error = esc_js( __('Unable to create new image.') );

		return $return;

	}



	$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;

	$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;

	$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';

	$scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];



	if ( $scale && $fwidth > 0 && $fheight > 0 ) {

		$sX = imagesx($img);

		$sY = imagesy($img);



		// check if it has roughly the same w / h ratio

		$diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2);

		if ( -0.1 < $diff && $diff < 0.1 ) {

			// scale the full size image

			$dst = wp_imagecreatetruecolor($fwidth, $fheight);

			if ( imagecopyresampled( $dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY ) ) {

				imagedestroy($img);

				$img = $dst;

				$scaled = true;

			}

		}



		if ( !$scaled ) {

			$return->error = esc_js( __('Error while saving the scaled image. Please reload the page and try again.') );

			return $return;

		}

	} elseif ( !empty($_REQUEST['history']) ) {

		$changes = json_decode( stripslashes($_REQUEST['history']) );

		if ( $changes )

			$img = image_edit_apply_changes($img, $changes);

	} else {

		$return->error = esc_js( __('Nothing to save, the image has not changed.') );

		return $return;

	}



	$meta = wp_get_attachment_metadata($post_id);

	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );



	if ( !is_array($meta) ) {

		$return->error = esc_js( __('Image data does not exist. Please re-upload the image.') );

		return $return;

	}



	if ( !is_array($backup_sizes) )

		$backup_sizes = array();



	// generate new filename

	$path = get_attached_file($post_id);

	$path_parts = pathinfo( $path );

	$filename = $path_parts['filename'];

	$suffix = time() . rand(100, 999);



	if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&

		isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {



		if ( 'thumbnail' == $target )

			$new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";

4077

About these ads

7 Responses to “wp_save_image”

  1. I don’t understand the purpose of the random number it generates. See Line 105 and the following if/else.

    Why doesn’t WP just save over the existing file?

    Is there a way for me to force it to?

  2. This is an attempt to ensure that the uploaded file does not exist. If it exists a suffix is added. You should be able to override this behavior by defining IMAGE_EDIT_OVERWRITE = true

  3. I tried this:
    define(‘IMAGE_EDIT_OVERWRITE’, FALSE);

    as the last line in wp-config. It didn’t change anything. :(

  4. oops, I meant to type true just now. ha.
    but i also tried false just in case i misunderstood it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 181 other followers

%d bloggers like this: