Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_save_image [ WordPress Function ]

wp_save_image ( $post_id )
Defined at:



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 && $fheight ) {
        
$sX imagesx($img);
        
$sY imagesy($img);

        
// check if it has roughly the same w / h ratio
        
$diff round($sX $sY2) - round($fwidth $fheight2);
        if ( -
0.1 $diff && $diff 0.1 ) {
            
// scale the full size image
            
$dst wp_imagecreatetruecolor($fwidth$fheight);
            if ( 
imagecopyresampled$dst$img0000$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_decodestripslashes($_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(100999);

    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'], 12896);
        
$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_pathget_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_joindirname($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

Meer ...

Gebruikersdiscussies [ wordpress.org ]

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics