wp_save_image [ WordPress Function ]
wp_save_image ( $post_id )
| Defined at: |
|
Soorgelijke functies: wp_save_image_file, wp_stream_image, wp_load_image, wp_restore_image, wp_crop_image
No description yet.
Source
<?php
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']}";
else
$new_path = $path;
} else {
while( true ) {
$filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$path_parts['extension']}";
$new_path = "{$path_parts['dirname']}/$new_filename";
if ( file_exists($new_path) )
$suffix++;
else
break;
}
}
// save the full-size file, also needed to create sub-sizes
if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) {
$return->error = esc_js( __('Unable to save the image.') );
return $return;
}
if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) {
$tag = false;
if ( isset($backup_sizes['full-orig']) ) {
if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
$tag = "full-$suffix";
} else {
$tag = 'full-orig';
}
if ( $tag )
$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
$success = update_attached_file($post_id, $new_path);
$meta['file'] = _wp_relative_upload_path($new_path);
$meta['width'] = imagesx($img);
$meta['height'] = imagesy($img);
list ( $uwidth, $uheight ) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96);
$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
if ( $success && ('nothumb' == $target || 'all' == $target) ) {
$sizes = get_intermediate_image_sizes();
if ( 'nothumb' == $target )
$sizes = array_diff( $sizes, array('thumbnail') );
}
$return->fw = $meta['width'];
$return->fh = $meta['height'];
} elseif ( 'thumbnail' == $target ) {
$sizes = array( 'thumbnail' );
$success = $delete = $nocrop = true;
}
if ( isset($sizes) ) {
foreach ( $sizes as $size ) {
$tag = false;
if ( isset($meta['sizes'][$size]) ) {
if ( isset($backup_sizes["$size-orig"]) ) {
if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
$tag = "$size-$suffix";
} else {
$tag = "$size-orig";
}
if ( $tag )
$backup_sizes[$tag] = $meta['sizes'][$size];
}
$crop = $nocrop ? false : get_option("{$size}_crop");
$resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop );
if ( $resized )
$meta['sizes'][$size] = $resized;
else
unset($meta['sizes'][$size]);
}
}
if ( $success ) {
wp_update_attachment_metadata($post_id, $meta);
update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
$file_url = wp_get_attachment_url($post_id);
if ( $thumb = $meta['sizes']['thumbnail'] )
$return->thumbnail = path_join( dirname($file_url), $thumb['file'] );
else
$return->thumbnail = "$file_url?w=128&h=128";
}
} else {
$delete = true;
}
if ( $delete ) {
$delpath = apply_filters('wp_delete_file', $new_path);
@unlink($delpath);
}
imagedestroy($img);
$return->msg = esc_js( __('Image saved') );
return $return;
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- wp_save_image (WordPress Function) - WPSeek.com
WordPress lookup for wp_save_image, a WordPress Function. wpseek.com is a WordPress-centric search tool for developers and theme authors.
wpseek.com - wp_save_image | A HitchHackers guide through WordPress
Feb 12, 2011 ... function wp_save_image($post_id) { $return = new stdClass; $success = $delete = $scaled = $nocrop = false; $post = get_post($post_id); ...
hitchhackerguide.com - Docs for page image-edit.php
$post_id. wp_save_image (line 455). void wp_save_image ( $post_id). $post_id. wp_save_image_file (line 218). void wp_save_image_file ( $filename, $image, ...
phpdoc.wordpress.org - #19889 (Image Editor doesn't apply changes to custom image sizes ...
Specifically, in the wp_save_image function in image-edit.php, this code is incorrect: $crop = $nocrop ? false : get_option("{$size}_crop"); $resized ...
core.trac.wordpress.org