Php allows you to crop and scale images in the center
1
GetImageWidth (); 10 $ height = $ Image-> getImageHeight (); 11 // determines whether the length and width of the photo are equal to 12 if ($ width! = $ Height) {13 // if it is not equal, the minimum side is used as the length to capture the center part of the image. 14 if ($ width> $ height) {15 $ x = ($ width-$ height)/2; 16 $ y = 0; 17 $ width = $ height; 18} else {19 $ y = ($ height-$ width)/2; 20 $ x = 0; 21 $ height = $ width; 22} 23 $ Image-> crop ($ file, $ width, $ height, $ x, $ y, $ file ); 24} 25 26 // proportional scaling Image 27 $ dst_w = 100; 28 $ dst_h = 100; 29 $ Image-> reduce ($ file, $ dst_w, $ dst_h, $ file); 30?> 31
_ Width = $ info [0]; 40 $ this-> _ height = $ info [1]; 41 $ type = $ info ['Mime ']; 42 $ type = explode ('/', $ type); 43 $ this-> _ type = $ type [1]; 44} 45 // return Image type 46 public function getImageType () {47 return $ this-> _ type; 48} 49 // return Image width 50 public function getImageWidth () {51 return $ this-> _ width; 52} 53 // return the image height 54 public function getImageHeight () {55 return $ this-> _ height; 56} 57 58/* 59 ** Image cropping 60 ** $ tmp_image source file 61 ** $ dst_w: width of the cropped image 62 ** $ dst_h: height of the cropped image 63 ** $ x crop 64 at the $ x coordinate of the source image ** $ y crop 65 at the $ y coordinate of the source image */66 public function crop ($ tmp_image, $ dst_w, $ dst_h, $ x, $ y, $ path) {67 switch ($ this-> _ type) {68 case 'jpeg ': 69 $ src = imagecreatefromjpeg ($ tmp_image); 70 break; 71 case 'GIF': 72 $ src = imagecreatefromgif ($ tmp_image); 73 break; 74 case 'png ': 75 $ src = imagecreatefrompng ($ tmp_image); 76 break; 77} 78 79 $ dst = imagecreatetruecolor ($ dst_w, $ dst_h); 80 $ color = imagecolorallocate ($ dst, 255,255,255); 81 imagecolortransparent ($ dst, $ color); 82 imagefill ($ dst, $ color); 83 $ bool = imagecopyresampled ($ dst, $ src, $ x, $ y, $ dst_w, $ dst_h, $ dst_w, $ dst_h); 84 switch ($ this-> _ type) {85 case 'jpeg ': 86 imagejpeg ($ dst, $ path, 100); 87 break; 88 case 'GIF': 89 imagegif ($ dst, $ path); 90 break; 91 case 'PNG ': 92 imagepng ($ dst, $ path); 93 break; 94} 95 96 imagedestroy ($ src); 97 imagedestroy ($ dst); 98 return $ bool; 99} 100 101/* 102 ** proportional scaling Image 103 ** $ tmp_image source 04 ** $ dst_w scaled image width 105 ** $ dst_h scaled image height 106 */107 public function reduce ($ tmp_image, $ dst_w, $ dst_h, $ path) {108 switch ($ this-> _ type) {109 case 'jpeg ': 110 $ src = imagecreatefromjpeg ($ tmp_image); 111 break; 112 case 'GIF': 113 $ src = imagecreatefromgif ($ tmp_image); 114 break; 115 case 'PNG ': 116 $ src = imagecreatefrompng ($ tmp_image); 117 break; 118} 119 $ imagex = imagesx ($ src); 120 $ imagey = imagesy ($ src); 121 $ dst = imagecreatetruecolor ($ dst_w, $ dst_h ); 122 $ color = imagecolorallocate ($ dst, 255,255,255); 123 imagecolortransparent ($ dst, $ color); 124 imagefill ($ dst, $ color ); 125 $ bool = imagecopyresampled ($ dst, $ src, 126, $ dst_w, $ dst_h, $ imagex, $ imagey); switch ($ this-> _ type) {127 case 'jpeg ': 128 imagejpeg ($ dst, $ path, 100); 129 break; 130 case 'GIF': 131 imagegif ($ dst, $ path ); 132 break; 133 case 'PNG ': 134 imagepng ($ dst, $ path); 135 break; 136} 137 imagedestroy ($ src); 138 imagedestroy ($ dst ); 139 return $ bool; 140} 141}