Создание эскизов изображений c помощью PHP

Простая функция для создания эскизов изображений с сохранением пропорций.

function createThumbImg($sPath, $tPath, $tWidth, $tHeight) {
    if (strrpos($sPath, '.') !== FALSE)
        $imgType = strtolower(substr($sPath, strrpos($sPath, '.')+1));
    switch($imgType) {
        case 'jpg':
            $source = @ImageCreateFromJpeg($sPath);
            function image($image, $fileName) {
                ImageJpeg($image, $fileName);
            }
            break;
        case 'jpeg':
            $source = @ImageCreateFromJpeg($sPath);
            function image($image, $fileName) {
                ImageJpeg($image, $fileName);
            }
            break;
        case 'gif':
            $source = @ImageCreateFromGif($sPath);
            function image($image, $fileName) {
                ImageGif($image, $fileName);
            }
            break;
        case 'png':
            $source = @ImageCreateFromPng($sPath);
            function image($image, $fileName) {
                ImagePng($image, $fileName);
            }
            break;
    }
    if (!$source) return FALSE;
    $imgData = @getimagesize($sPath);
    if(!$imgData) return FALSE;
    if ($imgData[0] > $imgData[1]) {
            $ratio = floatval($tWidth / $imgData[0]);
    }
    else {
            $ratio = floatval($tHeight / $imgData[1]);
    }
    $tWidth = intval($ratio * $imgData[0]);
    $tHeight = intval($ratio * $imgData[1]);
    $thumb = @ImageCreateTrueColor($tWidth, $tHeight);
    if (!$thumb) return FALSE;
    @ImageCopyResampled($thumb, $source, 0, 0, 0, 0, $tWidth, $tHeight, $imgData[0], $imgData[1]);
    Image($thumb, $tPath);
    return TRUE;
}

© Copyright by San40us


.