How php uses GD to create thumbnails that maintain aspect ratio
This article mainly introduces how php uses GD to create thumbnails to maintain aspect ratio, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
The details are as follows:
/ * Create a thumbnail image from $inputFileName no taller or wider than* $maxSize. Returns the new image resource or false on error.* Author: mthorn.net*/function thumbnail ($inputFileName, $maxSize = 100) {$info = getimagesize ($inputFileName); $type = isset ($info ['type'])? $info [' type']: $info [2]; / / Check support of file type if (! (imagetypes () & $type)) {/ / Server does not support file type return false;} $width = isset ($info ['width'])? $info [' width']: $info [0] Height = isset ($info ['height'])? $info [' height']: $info [1]; / / Calculate aspect ratio $wRatio = $maxSize / $width; $hRatio = $maxSize / $height; / / Using imagecreatefromstring will automatically detect the file type $sourceImage = imagecreatefromstring (file_get_contents ($inputFileName)); / / Calculate a proportional width and height no larger than the max size. If ($width