If you have ever done that kind of portal site, you must know that a picture may be displayed in different places and sizes. Different, the proportions are also different,
If you only use one picture, it will definitely be deformed, and it is too wasteful to link the large picture where the small picture is displayed... It is not perfect to use thumbnails, because each The proportions appearing in different places may be different, here is an example!
Please see the picture above.
In this place, what is actually transferred is a list, but the size of the pictures is different, some are wide and some are narrow. What should you do when you encounter such a situation? If you use the original address directly, It will definitely be deformed. It’s unreliable to use thumbnails. This adjustment is automatically done. You don’t know what width and height a picture needs.
-------------------------------------------------- -------------------------------------------------- ---------------
Let’s get to the point:
I have always used a method, which is PHP automatic cropping...compared to the image address you have seen similar to /aaaa/abc_200_100.jpg or /aaaa/abc_200*100.jpg
My method is to convert the image address into an address similar to the one above where the image is needed, and then direct it to a processing program through apache's rewrite. Generate an image based on the width and height and then save it,
There are several advantages to doing this:
First, it is very flexible. Wherever there is a picture, you can control it as wide or as high as you want without deformation, and the program will always display the maximum picture content
Second, when the image is generated once, apache will not redirect to the program next time, because there is a judgment of !d !f in front of the rule, which means that the current file will not be redirected until it does not exist. Next time the picture exists, it will not appear again and it will be the real picture
The disadvantage is that there may be more pictures generated and take up more space, but if it is your own server, it doesn’t matter. You can categorize and sort it out
OK, here’s the code, let’s take discuz as an example
Function usage crop_img('original image address', 'width', 'height'); This function returns the processed image address. The img function directly returns the image label string. For example, call this function in the discuz template {eval img( $pic,200,100)}
The returned address is /data/attachment/forum/aaaaaa_200_100.jpg. Currently, this image does not exist. Then look at the second step
Step 2: You need to add apache’s rewrite rules
The above means that files that do not exist at the beginning of the address data/attachement/ are directed to image.php for processing, and the url is passed as a parameter
The third part is the code in image.php
thumb 函数 可以控制 裁切方式,scale 为等比缩放,不裁切,不够的地方 用白色填充,crop 为裁切,如果要求的宽高比 大于原图宽高比,那么就保持最大显示宽度,居中裁切上下多余部分,如果要求宽高比小于原图宽高比,那么就保持最大高度,居中裁切左右多余部分,总而言之,在保持不变形的前提下 ,把图片缩小,而且最大保留图片的内容.哈哈 这个代码有多叼,试试知道了,,,当然你需要支持rewrite功能和GD2 支持