Home >Backend Development >PHP Tutorial >Example sharing of PHP processing image size

Example sharing of PHP processing image size

*文
*文Original
2017-12-23 14:30:121486browse

Many times the pictures uploaded by users are very large, and returning the original pictures directly will occupy a lot of server bandwidth. At this time we need to process the image size, so how does PHP handle the image size? This article will share an example of using the GD library to process image size.

1. Use php method to modify and store image size

-------------------------- ---------

Preparation work: 1. Need a link to the original image 2. GD library support

-------------- ---------------------

Assume the original image link: $url;

Method operation:

Step one: $arrimg = getimagesize($url);

+++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++

$arrimg: The form of this is:

Array
(
    [0] => 350
    [1] => 318
    [2] => 2
    [3] => width="350" height="318"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

Description: $ The numbers in arrimg[2] represent the numerical index of different image types. View:

Return result description

Index 0 gives the pixel value of the image width

Index 1 gives the pixel value of the image height

Index 2 gives the type of image, and returns a number, where 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order)

, 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM

Index 3 gives a string with width and height, which can be directly used for HTMLdc0870658837139040642baa5555a380 tag

Index bits What is given is the number of bits for each color of the image. The binary format

index channels gives the channel value of the image. The default for RGB images is 3

#index mime gives the image. MIME information, this information can be used to send the correct information in the HTTP Content-type header information, such as:

header("Content-type: image/jpeg");

+ ++++++++++++++++++++++++++++++++++++++++++++++++ +++++

Step 2: Perform proportional processing according to width: $arrimg[0], length: $arrimg [1], and get the required image size according to the proportion

Here we temporarily set the size of the image we need to get: $newheight $newwidth

++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++

Step 3: $thumb = imagecreatetruecolor($newwidth, $newheight);

Description: imagecreatetruecolor() returns an image identifier, representing a black image with size $newwidth and $newheight

++++++++++ ++++++++++++++++++++++++++++++++++++++++++++

Step 4: $source = imagecreatefromjpeg($url);

Description: imagecreatefromjpeg() returns an image identifier, representing the image obtained from the given file name


++++++++++++++++++++++++++++++++++++++++ +++++++++++++

Step 5: ImageCopyResampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$arrimg[0] ,$arrimg[1]);

imagecopyresampled -- Resample and resize part of the image.

++++++++++++++

Step 6:

Determine what type and then store it

switch($arrimg[2]){
//支持jpg
case  2:
imagejpeg($thumb,'test.jpg');
break;
//支持png
case 3:
imagejpeg($thumb,'test.jpg');
break;
default:
echo "<script>alert(&#39;带开发其他类型&#39;);</script>";
break;
}

Related recommendations:

php implements an efficient method to obtain the size and size of remote images

php image processing image background, canvas operation

php Summary of several functions of GD library to generate images

The above is the detailed content of Example sharing of PHP processing image size. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn