Skip to content

Instantly share code, notes, and snippets.

@Lizdo
Created February 28, 2010 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lizdo/317609 to your computer and use it in GitHub Desktop.
Save Lizdo/317609 to your computer and use it in GitHub Desktop.
<?php
function post_image($post, $targetwidth = 100, $targetheight = 100, $resize = true){
$key="post-image";
$post_image = get_post_meta($post->ID, $key, true);
if ($post_image != null) {
//get image size
$width = get_post_meta($post->ID, "post-image-width", true);
$height = get_post_meta($post->ID, "post-image-height", true);
if ($width == null || $height == null){
//only calculates the size for the first time.
list($width, $height, $type, $attr) = getimagesize($post_image);
if(!update_post_meta($post->ID, "post-image-width", $width))
add_post_meta($post->ID, "post-image-width", $width, true);
if(!update_post_meta($post->ID, "post-image-height", $height))
add_post_meta($post->ID, "post-image-height", $height, true);
}
if (!$resize){
printf ('<img src="%s" height=%d />', $post_image, $targetheight);
return;
}
else if (($width/$height) < ($targetwidth/$targetheight)){
printf ('<img src="%s" width=%d />', $post_image, $targetwidth);
}else{
printf ('<img src="%s" height=%d />', $post_image, $targetheight);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment