php crop image steps: 1, create a PHP sample file; 2, use "function imageCropper(){...}" method to achieve the image does not change the cropping; 3, through the "function imageZoom (){...}" method to achieve proportional cropping of the image.

PHP to achieve the image does not change the type of crop and image proportional crop method

Image cropping without changing shape

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

<?php

/**

 * imageCropper

 * @param string $source_path

 * @param string $target_width

 * @param string $target_height

 */

function imageCropper($source_path, $target_width, $target_height){

  $source_info  = getimagesize($source_path);

  $source_width = $source_info[0];

  $source_height = $source_info[1];

  $source_mime  = $source_info['mime'];

  $source_ratio = $source_height / $source_width;

  $target_ratio = $target_height / $target_width;

  if ($source_ratio > $target_ratio){

    // image-to-height

    $cropped_width = $source_width;

    $cropped_height = $source_width * $target_ratio;

    $source_x = 0;

    $source_y = ($source_height - $cropped_height) / 2;

  }elseif ($source_ratio < $target_ratio){

    //image-to-widht

    $cropped_width = $source_height / $target_ratio;

    $cropped_height = $source_height;

    $source_x = ($source_width - $cropped_width) / 2;

    $source_y = 0;

  }else{

    //image-size-ok

    $cropped_width = $source_width;

    $cropped_height = $source_height;

    $source_x = 0;

    $source_y = 0;

  }

  switch ($source_mime){

    case 'image/gif':

      $source_image = imagecreatefromgif($source_path);

      break;

    case 'image/jpeg':

      $source_image = imagecreatefromjpeg($source_path);

      break;

    case 'image/png':

      $source_image = imagecreatefrompng($source_path);

      break;

    default:

      return ;

      break;

  }

  $target_image = imagecreatetruecolor($target_width, $target_height);

  $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

  // copy

  imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);

  // zoom

  imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

  header('Content-Type: image/jpeg');

  imagejpeg($target_image);

  imagedestroy($source_image);

  imagedestroy($target_image);

  imagedestroy($cropped_image);

}

$filename = "8fcb7a0831b79c61.jpg";

imageCropper($filename,200,200);

?>

Image cropping to scale

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

<?php

/**

 * imageZoom

 * @param string $file

 * @param double $zoom

 */

function imageZoom($filename,$zoom=0.6){

  //baseinfo

  $sourceImageInfo = getimagesize($filename);

  $sourceWidth = $sourceImageInfo[0];

  $sourceHeight = $sourceImageInfo[1];

  $sourceMine = $sourceImageInfo['mime'];

  $sourceRatio = $sourceWidth/$sourceHeight;

  $sourceX = 0;

  $sourceY = 0;

  //zoom

  $targetRatio = $zoom;

  //target-widht-height

  $targetWidth = $sourceWidth*$targetRatio;

  $targetHeight = $sourceHeight*$targetRatio;

  //init-params

  $sourceImage = null;

  switch($sourceMine){

    case 'image/gif':

      $sourceImage = imagecreatefromgif($filename);

      break;

    case 'image/jpeg':

      $sourceImage = imagecreatefromjpeg($filename);

      break;

    case 'image/png':

      $sourceImage = imagecreatefrompng($filename);

      break;

    default:

      return ;

      break;

  }

  //temp-target-image

  $tempSourceImage = imagecreatetruecolor($sourceWidth, $sourceHeight);

  $targetImage = imagecreatetruecolor($targetWidth,$targetHeight);

  //copy

  imagecopy($tempSourceImage, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight);

  //zoom

  imagecopyresampled($targetImage, $tempSourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);

  //header

  header('Content-Type: image/jpeg');

  //image-loading

  imagejpeg($targetImage);

  //destroy

  imagedestroy($tempSourceImage);

  imagedestroy($sourceImage);

  imagedestroy($targetImage);

}

$filename = "8fcb7a0831b79c61.jpg";

imageZoom($filename);

?> 

Related articles

How to replace alt with php regular?

php regular to achieve the replacement alt method: 1, create a PHP sample file; 2, get the content to be replaced; 3, through "<img.*?src=[\"|\'](. *?) [\"|\']. *? >" can be replaced by the regular implementation.

How to convert svg to svg in php?

php svg to jpg method: 1, create a PHP sample file; 2, by "public function svgtojpg(){$image =...}" method to achieve the conversion.

Two basic output methods for php

The two basic output methods of php are "echo" and "print". echo is used to output one or more strings, which can take multiple arguments and have no return value, with the syntax "echo($ str)"; and print is used to output a string that can only take one

How to display a mess of php images?

php image display a mess of solutions: 1, open the corresponding PHP code file; 2, in the page header plus "header ("Content-Type: image/jpg");" code to declare the image type can be.

php can't load js css

php can not load js css solution: 1, check the correct path to load; 2, prohibit the use of absolute path; 3, use relative path to load the file can be.

php how to check user ip

php query user ip method: 1, get user ip by "$_SERVER["REMOTE_ADDR"]" way; 2, get user ip by "($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];" to get the user ip and so on.

How to invert a string by word in php

Inversion methods: 1. use "explode(' ',$str)" statement to convert string to array; 2. use "array_reverse($arr)" statement to invert the array and return the array in the reverse order of elements; 3. use "implode(' ',$arr)" statement to convert the inver

php get maximum value in several numbers

In php, you can use the max() function to get the maximum value among several numbers. The function works by calculating and returning the maximum value among several specified values, with the syntax "max(value1,value2,...) ;".

How to convert php image to base64

php image to base64 method: 1, get an image address; 2, through the "chunk_split(base64_encode($gambar));" method to convert the image to base64 string format can be.

How to merge arrays in PHP

How to merge arrays in PHP? To do this we need to use the array_merge function and the array_merge_recursive function. Here we will introduce these two functions separately.