Skip to content

Conversation

@stoyan0v
Copy link

@stoyan0v stoyan0v commented Jun 6, 2023

@ghost-of-the-mind
Copy link

Hey, I am kinda new to this whole "contributing thing". So, I have no idea if this is the best way to contribute, yet, I was struggling with a similar issue with SVG instead of ICO files and wanted to share it here.

  1. I added SVG support and wanted to upload a .svg file as the Site Icon (Appearance >> Customize >> Site Identity).
  2. I uploaded the .svg first. That went well since I had enabled SVG support inside functions.php.
  3. I wanted to select the uploaded .svg as a Site Icon. I selected the image, but WP asked me to crop the image. I could not do that since WP is not set up to crop vector images. I clicked on "Crop" anyway and WP gave me an error, that the image can't be cropped. Suprise, surprise...

To fix the issue, I added the following code in the same file /wp-admin/includes/ajax-actions.php

function wp_ajax_crop_image() {

//...rest of the code

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

// THE CODE I ADDED BELLOW

// Check if the attachment is an SVG file
    $mime_type = get_post_mime_type( $attachment_id );
    if ( 'image/svg+xml' === $mime_type ) {
        // For SVG files, send the original image data without cropping
        wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) );
    }

// THE CODE I ADDED ABOVE

$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'] );

//...rest of the code
}

What it does, is it checks if the image that is going to be cropped is an SVG, if it is and you click on "Crop" it does not bother to crop it at all and returns the image as it is.

This way I was able to designate the SVG file I uploaded as a Site Icon.

I am not saying that this is a perfect solution, but I do not see the point in cropping an SVG at all. So it works for me...

@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @ghost-of-the-mind.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sstoqnov.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants