• From the action “add_attachment” I call a function that gets run after the upload of an image.

    In this function I use wp_get_attachment image.
    Here wp_get_attachment_image returns the filename of the uploaded image in full resolution, even when I request the “medium”, and width=”1″ height=”1″ and ‘class=”attachment-medium”‘

    When I look into the upload-directory, the thumbnail sizes are correctly created.

    Could this be a timing problem where the data is not yet written to the database at the time I request it so shortly after the upload?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter springm

    (@springm)

    To further clarify the problem, here is the code snippet:

    function change_post_content( $post_content, $attachment_id ) {
     $medium  = wp_get_attachment_image($attachment_id, 'medium');
     $post_content .= $medium;
     return $post_content;
    }

    which inserts the following string in the new post:
    <img width="1" height="1" src="https://zenbrett.fritz.box/wp-content/uploads/2014/05/20140501-131342mws1.jpg" class="attachment-medium" alt="Maypole and St Florian statue" />

    where the image is exactly the image I was uploading

    I ran into this issue as well. It could be the server making an erroneous calculation or because we’re running WP Super Cache and using a CDN—not really sure. Anyway, here’s what I did to fix the issue, which will work if you have a fixed width:

    function change_post_content( $post_content, $attachment_id ) {
       $medium  = wp_get_attachment_image($attachment_id, 'medium');
       $medium = str_replace( 'width="1"', 'width="100"', $medium );
       $medium = str_replace( ' height="1"', '', $medium );
       $post_content .= $medium;
       return $post_content;
    }

    The two new lines of code finds and replaces the width and height settings if they equal “1”. Just update width=”100″ on line 3 to whatever size you’re trying to achieve. Let me know if that helps or if you’ve created an alternate solution.

    -PaulMighty

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_get_attachment image returns 'width="1" height="1"'’ is closed to new replies.