- function My_image_resize ($src _file, $dst _file, $new _width, $new _height)
- {
- $src _img=imagecreatefromjpeg ($src _file);
- $w =imagesx ($src _img);
- $h =imagesy ($src _img);
- $ratio _w=1.0 * $new _width/$w;
- $ratio _h=1.0 * $new _height/$h;
- $ratio = 1.0;
- The resulting image of the aspect ratio of the original is small, or all large, the principle is to take a large proportion of magnification, take a large proportion of the reduction (smaller proportion)
- if (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1))
- {
- if ($ratio _w < $ratio _h)
- {
- $ratio = $ratio _h; Case one, the width of the ratio is smaller than the height direction, according to the height of the proportional standard to cut or enlarge
- }else {
- $ratio = $ratio _w;
- }
- Defines an intermediate temporary image that meets the target's aspect ratio exactly.
- $inter _w= (int) ($new _width/$ratio);
- $inter _h= (int) ($new _height/$ratio);
- $inter _img=imagecreatetruecolor ($inter _w, $inter _h);
- Imagecopy ($inter _img, $src _img, 0,0,0,0, $inter _w, $inter _h);
- Generates a temporary image that is the size of the maximum edge length, which is the $ratio scale of the target image
- To define a new image
- $new _img=imagecreatetruecolor ($new _width, $new _height);
- Imagecopyresampled ($new _img, $inter _img,0,0,0,0, $new _width, $new _height, $inter _w, $inter _h);
- Imagejpeg ($new _img, $dst _file,100); Storing images
- }
- End If 1
- 2 One edge of the target image is larger than the original, one edge is smaller than the original, the flat image is enlarged, then cropped
- if (($ratio _w < 1 && $ratio _h > 1) | | ($ratio _w >1 && $ratio _h <1))
- Else
- {
- $ratio = $ratio _h> $ratio _w? $ratio _h: $ratio _w; Take a large proportion of that value
- Defines an intermediate large image that is equal in height or width of the target image, and then zooms in on the original image
- $inter _w= (int) ($W * $ratio);
- $inter _h= (int) ($H * $ratio);
- $inter _img=imagecreatetruecolor ($inter _w, $inter _h);
- Crop after scaling the original
- Imagecopyresampled ($inter _img, $src _img,0,0,0,0, $inter _w, $inter _h, $w, $h);
- To define a new image
- $new _img=imagecreatetruecolor ($new _width, $new _height);
- Imagecopy ($new _img, $inter _img, 0,0,0,0, $new _width, $new _height);
- }
- Imagejpeg ($new _img, $dst _file,100); Storing images
- }
- $filename = "110.jpg"; The path to the original picture
- $fileto = "120.jpg";//after the saved path
- $new the width of the _w=300;//set
- $new _h=350; Set the high
- My_image_resize ($filename, $fileto, $new _w, $new _h);
- ?>
Copy Code
|