Home  >  Article  >  Backend Development  >  How to use php to modify the image size and share the storage method

How to use php to modify the image size and share the storage method

黄舟
黄舟Original
2017-07-26 13:45:112207browse

1. Use php method to modify and store image size
-------------------------------- ---
Preparation work: 1. Need the link of the original picture 2.GD library support
-------------------------- ---------
Assume the original image link: $url;
Method operation:

The first step: $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 picture 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 The dc0870658837139040642baa5555a380 tag used in HTML
Index bits gives the number of bits for each color of the image. In binary format
Index channels gives the channel value of the image. The default for RGB images is 3
Index mime gives the MIME information of the image. This information can be used to send the correct information in the HTTP Content-type header information, such as:
header("Content-type: image/jpeg");
++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
The second step: 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 to be obtained as: $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 copy part of the image and resize it.
++++++++++++++
Step 6:
Determine what type and 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;
}


The above is the detailed content of How to use php to modify the image size and share the storage method. 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