dttwois6098 2012-03-24 21:41
浏览 58
已采纳

如何捕获上传图像的中心部分? (制作缩略图)

I have to handle the images uploaded by users and want to make thumbnails out of them, in order to create an image gallery.

The problem is that the images uploaded by the user differ in dimensions, so constraining the dimensions of the thumbnail will make disproportioned images as thumbnails.

Do you know how I can capture the center portion (or truncate the sides) of an uploaded image? Or do you know any script that will handle that?

Thanks a lot

Regards

EDIT:

echo '<div class="pic" style="background-image: url(upload/photos/'.$photo1.')"></div>';

and

    .pic {

    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
}
  • 写回答

3条回答 默认 最新

  • doufeng1249 2012-03-24 21:45
    关注

    Instead of cropping the images, you can also use a normal resized thumbnail that is big enough to fit the box you want it for completely, and set it as a background image.

    So if your box is 100x100 pixels, your thumbnail images need to be resized to 100px width for a vertical image and 100px height for a horizontal image.

    By the way, for modern browsers you can use:

    background-size: cover;
    

    So that the image completely covers the box, maintaining its aspect ratio.

    Edit: So in your php you would do something like:

    echo '<div style="background-image: url(' . $path_to_image . ');"></div>';
    

    and in the css:

    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端