001 | function ImageResize($srcFile, $toW, $toH, $toFile = "" ) { |
002 | global $cfg_photo_type; |
007 | $srcInfo = GetImageSize($srcFile, $info); |
008 | switch ($srcInfo[2]) { |
010 | if (!$cfg_photo_type[ 'gif' ]) { |
013 | $im = imagecreatefromgif($srcFile); |
016 | if (!$cfg_photo_type[ 'jpeg' ]) { |
019 | $im = imagecreatefromjpeg($srcFile); |
022 | if (!$cfg_photo_type[ 'png' ]) { |
025 | $im = imagecreatefrompng($srcFile); |
028 | if (!$cfg_photo_type[ 'bmp' ]) { |
031 | $im = imagecreatefromwbmp($srcFile); |
034 | $srcW = ImageSX($im); |
035 | $srcH = ImageSY($im); |
036 | if ($srcW <= $toW && $srcH <= $toH) { |
040 | $newW = $toH * $srcW / $srcH; |
041 | $newH = $toW * $srcH / $srcW; |
049 | if ($srcW > $toW || $srcH > $toH) { |
050 | if (function_exists( "imagecreatetruecolor" )) { |
051 | @$ni = imagecreatetruecolor($ftoW, $ftoH); |
053 | imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
055 | $ni = imagecreate($ftoW, $ftoH); |
056 | imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
059 | $ni = imagecreate($ftoW, $ftoH); |
060 | imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
063 | $new_imgx = imagecreatetruecolor($toW, $toH); |
065 | imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH); |
067 | imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH); |
069 | switch ($srcInfo[2]) { |
071 | imagegif($new_imgx, $toFile); |
074 | imagejpeg($new_imgx, $toFile, 85); |
077 | imagepng($new_imgx, $toFile); |
080 | imagebmp($new_imgx, $toFile); |
085 | imagedestroy($new_imgx); |
091 | //[2020-11-04]:解决缩略图缩放变形问题(宽度、高度为后台设置宽高) |
092 | function ImageResize($srcFile, $toW, $toH, $toFile = "" ) { |
093 | global $cfg_photo_type; |
098 | $srcInfo = GetImageSize($srcFile, $info); |
099 | switch ($srcInfo[2]) { |
101 | if (!$cfg_photo_type[ 'gif' ]) { |
104 | $im = imagecreatefromgif($srcFile); |
107 | if (!$cfg_photo_type[ 'jpeg' ]) { |
110 | $im = imagecreatefromjpeg($srcFile); |
113 | if (!$cfg_photo_type[ 'png' ]) { |
116 | $im = imagecreatefrompng($srcFile); |
119 | if (!$cfg_photo_type[ 'bmp' ]) { |
122 | $im = imagecreatefromwbmp($srcFile); |
125 | $srcW = ImageSX($im); |
126 | $srcH = ImageSY($im); |
127 | if ($srcW <= $toW && $srcH <= $toH) { |
131 | $newW = $toH * $srcW / $srcH; |
132 | $newH = $toW * $srcH / $srcW; |
140 | if ($srcW > $toW || $srcH > $toH) { |
141 | if (function_exists( "imagecreatetruecolor" )) { |
142 | @$ni = imagecreatetruecolor($ftoW, $ftoH); |
144 | imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
146 | $ni = imagecreate($ftoW, $ftoH); |
147 | imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
150 | $ni = imagecreate($ftoW, $ftoH); |
151 | imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
154 | $new_imgx = imagecreatetruecolor($toW, $toH); |
156 | imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH); |
158 | imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH); |
160 | switch ($srcInfo[2]) { |
162 | imagegif($new_imgx, $toFile); |
165 | imagejpeg($new_imgx, $toFile, 85); |
168 | imagepng($new_imgx, $toFile); |
171 | imagebmp($new_imgx, $toFile); |
176 | imagedestroy($new_imgx); |
183 | dedecms5.7版image.helper.php修改方法: |
185 | if (!function_exists( 'ImageResize' )) { |
186 | function ImageResize($srcFile, $toW, $toH, $toFile = "" ) { |
187 | global $cfg_photo_type; |
192 | $srcInfo = GetImageSize($srcFile, $info); |
193 | switch ($srcInfo[2]) { |
195 | if (!$cfg_photo_type[ 'gif' ]) { |
198 | $im = imagecreatefromgif($srcFile); |
201 | if (!$cfg_photo_type[ 'jpeg' ]) { |
204 | $im = imagecreatefromjpeg($srcFile); |
207 | if (!$cfg_photo_type[ 'png' ]) { |
210 | $im = imagecreatefrompng($srcFile); |
213 | if (!$cfg_photo_type[ 'bmp' ]) { |
216 | $im = imagecreatefromwbmp($srcFile); |
219 | $srcW = ImageSX($im); |
220 | $srcH = ImageSY($im); |
221 | if ($srcW <= $toW && $srcH <= $toH) { |
225 | $newW = $toH * $srcW / $srcH; |
226 | $newH = $toW * $srcH / $srcW; |
234 | if ($srcW > $toW || $srcH > $toH) { |
235 | if (function_exists( "imagecreatetruecolor" )) { |
236 | @$ni = imagecreatetruecolor($ftoW, $ftoH); |
238 | imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
240 | $ni = imagecreate($ftoW, $ftoH); |
241 | imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
244 | $ni = imagecreate($ftoW, $ftoH); |
245 | imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); |
248 | $new_imgx = imagecreatetruecolor($toW, $toH); |
250 | imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH); |
252 | imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH); |
254 | switch ($srcInfo[2]) { |
256 | imagegif($new_imgx, $toFile); |
259 | imagejpeg($new_imgx, $toFile, 85); |
262 | imagepng($new_imgx, $toFile); |
265 | imagebmp($new_imgx, $toFile); |
270 | imagedestroy($new_imgx); |
|