PHP instance ———— picture Proportional scaling function

Source: Internet
Author: User
Tags add format manual functions variable

In the process of web development, often to some of the pictures to do some processing, such as scaling, cropping, watermark and so on. Of course, these functions can not only be used in the process of developing the site, usually need to do some processing of the picture, the function encapsulated into the function, to deal with the picture, according to their own requirements in the function to add the required parameters do not finish it. Although this for small white may suddenly come up on a point of difficulty, but a retrogradation two back to ripe well. All right, get to the point. Today we share an image scaling function. Students need to be able to modify a little, and then take them to use, the province of their own wrote (Great god please disregard).

1. First, learn more about the functions needed in image scaling

There is not very clear can use the PHP development manual to retrieve, the above is very clear, is a very good learning tool, whether novice or veteran.

2. Share the code, notes written very clear (feel good about yourself, the shortcomings of the hope that everyone correct)

 
  ($miny/$maxy)) {
		$scale = $miny/$maxy;
	} else{
		$scale = $minx/$maxx;	
	}

	A
	$minx =floor ($maxx * $scale) is taken for the evaluated value;
	$miny =floor ($maxy * $scale);
	
	Add a small figure
	$minim =imagecreatetruecolor ($minx, $miny);

	Zoom function
	imagecopyresampled ($minim, $maxim, 0,0,0,0, $minx, $miny, $maxx, $maxy);

	Small Figure output
	header ("content-type:{$MAXM}");

	Determine picture type
	switch ($maxt) {case
		1:
			$imgout = "Imagegif";
			break;
		Case 2:
			$imgout = "imagejpeg";
			break;
		Case 3:
			$imgout = "Imagepng";
			break;
	Variable function
	$imgout ($minim, ' copypic.jpg ');
	
	
	Release of resources
	Imagedestroy ($maxim);
	Imagedestroy ($minim);

Imageshrink ("Picture/apple.jpg", 50,50);
? >


3. There are several parts of the whole code that are not easy to understand.

(1) getimagesize () The return value of this function is an array containing information such as the size, format, etc. of the picture. Depending on the key value pair, you can get the value, assign it to a variable, and then pass it directly to the variable where you need it later . About functions, please read the manual.

(2) in the positive proportional scaling, we need to consider the scaling ratio, take the small size of the long and large graphs of the length of the comparison, select a small proportion as a scaling ratio. This is not particularly good understanding, you can try for yourself.

(3) Large-scale graphics, the format of the small map has not changed, so in the latter need function output image, the image format, the need to use variable function. To assign a function to a variable, the trick is to feel pretty good when used.

(4) The saved picture format can set itself, save the path with the original picture of the same path. Other parameters look at the personal needs.





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.