Support » Plugins » Hacks » Image Size Issue – (wp_get_attachment_image_src)/(wp_check_filetype_and_ext)

  • 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 “1024×680”.

    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.

Viewing 1 replies (of 1 total)
  • 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.

Viewing 1 replies (of 1 total)
  • The topic ‘Image Size Issue – (wp_get_attachment_image_src)/(wp_check_filetype_and_ext)’ is closed to new replies.