An algorithm for creating a center thumbnail image

Source: Internet
Author: User

Recently in the study of PHP, because the project needs to use the thumbnail, if the thumbnail is not cropped, you will find that the image becomes grotesque, not conducive to typesetting. So here's a quick introduction to the idea of creating a center thumbnail:

A picture 480*800

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/0B/wKioL1WQwCaQGkPPAAKRfEgF3mc371.jpg "title=" 1.png " alt= "Wkiol1wqwcaqgkppaakrfegf3mc371.jpg"/>

Now I'm going to cut it into a 240*240 picture.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/0B/wKioL1WQwGShBReHAACWxp1cPRw993.jpg "title=" x1.png "alt=" Wkiol1wqwgshbrehaacwxp1cprw993.jpg "/>

Similar to the one above.


1. First we need to calculate whether the zoom is the width first or high priority, if it is like above is the width first.

The scale is

Ratio = IMAGE_WIDTH_DST/IMAGE_WIDTH_SRC


2. Depending on the scale, the height of the reduction is

Image_height_pre = image_height_src * ratio


3. Get the height after zooming, minus the height that needs to be generated, that is, the remaining height after shrinking

Image_height_rem = IMAGE_HIEGHT_PRE-IMAGE_HEIGHT_DST


4. Finally calculate where to start cropping

Start_cut = Image_height_rem * IMAGE_WIDTH_SRC/IMAGE_WIDTG_DST/2


The above formula is not good to understand:


Summarize:

Formula for width precedence

Start clipping coordinates = (original height * (Target wide chart/Huantuquan)-Target graph height) * (former wide chart/target wide chart)/2


High-priority formula

Start clipping coordinates = (Original width * (target graph height/Huantugau)-Target wide chart) * (original height/target height)/2




Difficulties:

When you get to the remaining height of the crop, you also need to revert to the proportions of the original picture.

Divide by 2, to center, to empty the 2-terminal position.








This article is from "Maple Night" blog, please be sure to keep this source http://fengwan.blog.51cto.com/508652/1668970

An algorithm for creating a center thumbnail image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.