WordPress.org

Ready to get started?Download WordPress

Forums

Image Size Issue - (wp_get_attachment_image_src)/(wp_check_filetype_and_ext) (2 posts)

  1. Cedric McPherson
    Member
    Posted 1 year ago #

    First post ever, so bare with me.
    What I was trying to accomplish was getting all the images with-in a post through the wp_get_attachment_image_src function. This function returns the original image file location, width and height through an array.

    The Problem-
    The width returned for the large file size is always 625 when it should be 624.
    Also the first image file location from the post includes the size as "1024x680".

    The Solution-
    Here is what I came up with for hacking these problems:

    $args = array(
    	'order' => 'ASC',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID,
    	'post_type' => 'attachment',
    	);
    
    $attachments = get_posts( $args );
    
    if ( $attachments ) {
    	foreach ( $attachments as $attachment ) {
    	$image_large = wp_get_attachment_image_src( $attachment->ID, 'large' );
    
    	preg_match('#((?!-\d+x\d+)(?!\.jpg+).)*#',$image_large[0],$match);
    
    	$place = $match[0].'-'. $image_large[1] .'x'. $image_large[2] . '.jpg';
    	preg_match('#(.*)/(.*)#',$place,$place_name);
    
    	if(wp_check_filetype_and_ext($place,$place_name[2])) $place = $match[0].'-'. ($image_large[1] - 1).'x'. $image_large[2] . '.jpg'; //end if
    	else {
    	$place = $match[0].'-'. ($image_large[1]-1) .'x'. $image_large[2] . '.jpg';
    	preg_match('#(.*)/(.*)#',$place,$place_name);
    	echo $place_name[2];
    	if(wp_check_filetype_and_ext($place,$place_name[2])) echo ' = ' . $place; //end if
    	else{
    	$place = $match[0].'-'. ($image_large[1]-1) .'x'. ($image_large[2]-1) . '.jpg';
    	preg_match('#(.*)/(.*)#',$place,$place_name);
    	echo $place_name[2];
    	if(wp_check_filetype_and_ext($place,$place_name[2])) echo ' = ' . $place; //end if
    	}//end else
    	}//end else
    
    	?>
        <img src="<?=wp_get_attachment_thumb_url( $attachment->ID )?>" class="thumbnail" place="<?=$place?>" alt="" />
        <?
    	}//end for each
    }//end if attachments

    Another Problem Found-
    When I was checking the file location and the size (since the size is saved in the file name on upload). I found that the wp_check_filetype_and_ext validated the incorrect file name that was using the incorrect image size "625", for the width. The solution I have also fixes this problem as well.

    Note-
    The solution is included within get_posts so that I could get the post by category and then subsequently the post ID to get the attachments.

  2. Cedric McPherson
    Member
    Posted 1 year ago #

    Replace:

    if(wp_check_filetype_and_ext($place,$place_name[2])) $place = $match[0].'-'. ($image_large[1] - 1).'x'. $image_large[2] . '.jpg'; //end if
    	else {
    	$place = $match[0].'-'. ($image_large[1]-1) .'x'. $image_large[2] . '.jpg';
    	preg_match('#(.*)/(.*)#',$place,$place_name);
    	echo $place_name[2];
    	if(wp_check_filetype_and_ext($place,$place_name[2])) echo ' = ' . $place; //end if
    	else{
    	$place = $match[0].'-'. ($image_large[1]-1) .'x'. ($image_large[2]-1) . '.jpg';
    	preg_match('#(.*)/(.*)#',$place,$place_name);
    	echo $place_name[2];
    	if(wp_check_filetype_and_ext($place,$place_name[2])) echo ' = ' . $place; //end if
    	}//end else
    	}//end else

    With:

    if(fopen($place,'r') == true) $place = $match[0].'-'. ($image_large[1] - 1).'x'. $image_large[2] . '.jpg'; //end if
    else {
    $place = $match[0].'-'. ($image_large[1] -1).'x'. ($image_large[2] - 1) . '.jpg';
    }//end else

    You will get an error if the image is incorrect at first, but it will find it the second time. Make sure your wp-config.php is set to false for debugging so the user doesn't see the error.

Topic Closed

This topic has been closed to new replies.

About this Topic