Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic picture clipping with PHP

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "PHP how to achieve automatic picture cutting", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "PHP how to achieve automatic picture cutting" bar!

If you have been a friend of that kind of portal station, you must know that a picture may be displayed in different places with different sizes and proportions.

If you use only one picture, it will certainly be deformed, and it is too wasteful to link large images where small ones are displayed. Using thumbnails to deal with it is not perfect, because the proportion that appears in each place may be different, for example!

Please look at the picture above.

In this place, what is actually transferred out is a list, but the size of the picture is different, how wide it is and how narrow it is. What do you do when you encounter such a situation? if you directly use the original address, it will certainly be deformed. If the thumbnail is not reliable, this transfer is automatically transferred, you have no idea which picture needs how wide and high.

-

Let's get to the point:

I have been using a method, that is, PHP automatic cutting. Compared to you have seen similar kind of picture address, / aaaa/abc_200_100.jpg or / aaaa/abc_200*100.jpg?

My way is to convert the image address to an address similar to the one above where I need it, and then direct it to a processor through apache's rewrite. Generate a picture based on width and height and save it

There are several advantages to this:

First, it is very flexible. where there are pictures, you can control how wide and high you need, will not deform, and the program will always display the most content of the picture.

Second, when the picture is generated once, apache will not redirect to the program next time, because there is a judgment in front of the rule! d! F, which means it will be directed only when the current file does not exist, and the next time the picture exists, it will not come out directly as the real picture.

The downside is that more pictures may be generated and take up more space, but if it is your own server, it doesn't matter, you can sort it out.

OK presents the code, so let's take discuz as an example.

The copy code is as follows:

Function crop_img ($img, $width = 200, $height = 200) {

$img_info = parse_url ($img)

/ * external links directly return the image address * /

If (! empty ($img_info ['host']) & & $img_info [' host']! = $_ SERVER ['HTTP_HOST']) {

Return $img

} else {

$pos = strrpos ($img,'.')

$img = substr ($img, 0, $pos). '_'. $width. '_'. $height. Substr ($img, $pos)

Return $img

}

}

Function img ($img,$width,$height) {

$img_info = parse_url ($img)

/ * external links directly return the image address * /

If (! empty ($img_info ['host']) & & $img_info [' host']! = $_ SERVER ['HTTP_HOST']) {

Return $img

} else {

$pos = strrpos ($img,'.')

$img = substr ($img, 0, $pos). '_'. $width. '_'. $height. Substr ($img, $pos)

Echo'

'

Return

}

}

The use of the function crop_img ('original address', 'width', 'height'); this function returns the address of the processed picture, and the img function returns the image tag string directly, such as calling this function {eval img ($pic,200100)} in the discuz template.

So the returned address is / data/attachment/forum/aaaaaa_200_100.jpg. At present, this picture does not exist, so look at the second step.

The second step is to add the rewrite rule of apache

The copy code is as follows:

RewriteEngine on

RewriteCond% {REQUEST_FILENAME}!-d

RewriteCond% {REQUEST_FILENAME}!-f

RewriteRule ^ data/attachment/ (. *) $images.php?url=$1 [L]

The above means that files that do not exist at the beginning of the address data/attachement/ are directed to image.php, and url is passed as a parameter

The third part is in the code of image.php.

The copy code is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report