php使用GD创建保持宽高比缩略图的方法_php技巧
程序员文章站
2024-04-02 19:05:58
...
本文实例讲述了php使用GD创建保持宽高比缩略图的方法。分享给大家供大家参考。具体如下:
/** * 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
希望本文所述对大家的php程序设计有所帮助。