PHP _php tutorial on how to resize images on the server side

Source: Internet
Author: User

PHP implementation method for resizing images on the server side


This article explains how PHP implements resizing images on the server side. Share to everyone for your reference. The specific analysis is as follows:

The resizing of the image on the server side is a lot more beneficial than the browser processing.
This article describes how PHP adjusts the image size on the server side.

The code consists of two parts:

①imageresizer () is used to process the image
②loadimage () inserts the image URL in a simpler format

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

function Imageresizer ($url, $width, $height) {

Header (' Content-type:image/jpeg ');

List ($width _orig, $height _orig) = getimagesize ($url);

$ratio _orig = $width _orig/$height _orig;

if ($width/$height > $ratio _orig) {

$width = $height * $ratio _orig;

} else {

$height = $width/$ratio _orig;

}

This resamples the image

$image _p = Imagecreatetruecolor ($width, $height);

$image = Imagecreatefromjpeg ($url);

Imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width, $height, $width _orig, $height _orig);

Output the image

Imagejpeg ($image _p, NULL, 100);

}

Works with both POST and GET

$method = $_server[' Request_method ');

if ($method = = ' GET ') {

Imageresize ($_get[' url '], $_get[' W '), $_get[' H ']);

} elseif ($method = = ' POST ') {

Imageresize ($_post[' url '], $_post[' W '), $_post[' H ']);

}

Makes the process simpler

function LoadImage ($url, $width, $height) {

Echo ' Image.php?url= ', UrlEncode ($url),

' &w= ', $width,

' &h= ', $height;

}

?>

Usage:

?

1

2

3

Above code would is in a file called image.php.

Images would is displayed like this:

"Alt=" "/>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1017136.html www.bkjia.com true http://www.bkjia.com/PHPjc/1017136.html techarticle PHP Implementation on the server side to adjust the size of the picture The example of this article describes the PHP implementation of the server-side adjustment of the image size method. Share to everyone for your reference. The specific analysis is as follows: ...

  • 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.