wp_crop_image example


            do_action( 'wp_create_file_in_uploads', $file$attachment_id ); // For replication.
            return $this->finished();
        } elseif ( $width > $max_width ) {
            $oitar = $width / $max_width;

            $image = wp_crop_image(
                $attachment_id,
                0,
                0,
                $width,
                $height,
                $max_width,
                $height / $oitar,
                false,
                str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file )$file )
            );

            
function wp_ajax_crop_image() {
    $attachment_id = absint( $_POST['id'] );

    check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' );

    if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) {
        wp_send_json_error();
    }

    $context = str_replace( '_', '-', $_POST['context'] );
    $data    = array_map( 'absint', $_POST['cropDetails'] );
    $cropped = wp_crop_image( $attachment_id$data['x1']$data['y1']$data['width']$data['height']$data['dst_width']$data['dst_height'] );

    if ( ! $cropped || is_wp_error( $cropped ) ) {
        wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) );
    }

    switch ( $context ) {
        case 'site-icon':
            require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php';
            $wp_site_icon = new WP_Site_Icon();

            // Skip creating a new attachment if the attachment is a Site Icon.
Home | Imprint | This part of the site doesn't use cookies.