here is the php code
$id = wp_insert_attachment($attachment, $file, $post);
$imagesize = getimagesize($file);
$imagedata['width'] = $imagesize['0'];
$imagedata['height'] = $imagesize['1'];
list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
$imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
$imagedata['file'] = $file;
add_post_meta($id, '_wp_attachment_metadata', $imagedata);
$url = str_replace(get_bloginfo('url'),'',$url);
$MaxWidth = 182; //设置图片宽度界限
$MaxHeight = 110; //设置图片高度界限
if(($imagedata['width']/$imagedata['height'])>($MaxWidth/$MaxHeight)){
$maxsize=($imagedata['width']*$MaxHeight)/$imagedata['height'];
}else{
$thumbnailwidth=$MaxWidth;
$thumbnailheight=$MaxWidth*$imagedata['height']/$imagedata['width'];
if($thumbnailwidth>$thumbnailheight)
$maxsize=$thumbnailwidth;
else
$maxsize=$thumbnailheight;
}


Sign In
Create Account

Back to top









