我在图像标签中显示图像
<img src="image.jpg" width="100" height="50" />I用户上传大小为100x100的图像,我希望它像上面的标签那样显示
<img src="image.jpg" width="50" height="50" />如何做到这一点呢?
ps
如果这是服务器端的问题,我正在使用php。
发布于 2011-05-03 05:56:53
使用getimagesize()获取图像大小。然后执行此操作以缩小到适配,但保持纵横比:
if ($width > $height)
{
$ratio = (float)100 / $width;
}
else
{
$ratio = (float)100 / $height;
}
$width = (int)round($width * $ratio);
$height = (int)round($height * $ratio);发布于 2011-05-03 15:50:09
此代码将适合任何图像到maxWidth x maxHeight框,保持纵横比和填充与白色背景。
$img = new Imagick('orig.jpg');
$maxWidth = 100;
$maxHeight = 50;
$geom = $img->getImageGeometry();
$fitWidth = (($maxWidth/$geom['width'])<($maxHeight/$geom['height']))?true:false;
if($fitWidth){
$img->thumbnailImage($maxWidth,0,false);
}else{
$img->thumbnailImage(0,$maxHeight,false);
}
$canvas = new Imagick();
$canvas->newImage($maxWidth,$maxHeight,'white','jpg');
$geom = $img->getImageGeometry();
$x = ($maxWidth-$geom['width'])/2;
$y = ($maxHeight-$geom['height'])/2;
$canvas->compositeImage($img,imagick::COMPOSITE_OVER,$x,$y);
$canvas->writeImage('thumb.jpg');https://stackoverflow.com/questions/5862879
复制相似问题