- 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 height and width of the generated image are smaller than the original one , or both are large, the principle is to use a large ratio to enlarge, and use a large ratio to reduce (the reduced ratio will be smaller)
- if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1))
- {
- if($ratio_w < $ratio_h)
- {
- $ratio = $ratio_h ; // Case 1, the width ratio is smaller than the height direction, crop according to the height ratio standard or Zoom in
- }else {
- $ratio = $ratio_w ;
- }
- // Define an intermediate temporary image whose aspect ratio just meets the target requirements
- $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) ;
- // Generate a temporary image with the maximum side length as the size of the target image $ratio
- // 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); // Store image
- }
- // end if 1
- // 2 One side of the target image is larger than the original image, and one side is smaller than the original image. First enlarge the ordinary image, and then crop it
- //if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w > ;1 && $ratio_h <1) )
- else
- {
- $ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //Take the value with the larger ratio
- //Define a large image in the middle, the image The height or width is equal to the target image, and then enlarge the original image
- $inter_w=(int)($w * $ratio);
- $inter_h=(int) ($h * $ratio);
-
- $inter_img=imagecreatetruecolor ($inter_w , $inter_h);
- //Crop the original image after scaling it
- imagecopyresampled($inter_img,$src_img,0,0,0,0,$inter_w,$inter_h,$w,$h);
- / / 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); //Storage image
- }
-
- $filename = "110.jpg"; //The path of the original image
- $fileto="120.jpg";//The path to save after taking the screenshot
- $new_w =300;//Set width
- $new_h=350; //Set height
- my_image_resize($filename,$fileto,$new_w,$new_h);
- ?>
-
Copy code
|