GD Lib Qualitätsproblem und Frage zum Upload

Jan-Frederik Stieler

Monsterator
Moderator
Hi,
ich habe mir mit de GD Lib ein Imageresize für das Kirby CMS geschrieben.
Soweit hat das bisher auch funktioniert.
Jetzt habe ich aber zwei Probleme im aktiven Einsatz, beim upload von mehreren Dateien wird nur die erste skaliert und manchmal bekomm ich Bilder mit einer schlechten Qualität heraus.


PHP:
<?php
$kirbyVersion = $kirby::$version;

if(version_compare($kirbyVersion, '2', '>=') && version_compare($kirbyVersion, '3', '<')) {
    kirby()->hook('panel.file.upload', function($file) {
        $filePage = $file->page();
        $fileParts = pathinfo( $file->root() );

        //Gif will not work
        if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg' || $fileParts['extension'] == 'png' || $fileParts['extension'] == 'bmp' || $fileParts['extension'] == 'tiff' || $fileParts['extension'] == 'tif'){
            try {
                list($width, $height) = getimagesize($file->root());
                if ($width > $height){
                    // Landscape
                    shrinkImage($file->root());
                } else {
                    // Portrait or Square
                    createLandscape($file->root());
                }
            } catch (Exception $e) {
                throw new Exception($e->getMessage());
            }
        }
    });
}

function shrinkImage($filePath){
    $fileParts = pathinfo($filePath);
    $savePath = $filePath;  //full path to saved png, including filename and extension

    if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg'){
        $img = imagecreatefromjpeg($filePath);
    }

    if($fileParts['extension'] == 'png'){
        $img = imagecreatefrompng($filePath);
    }

    $imgWidth = imagesx($img);
    $imgHeight = imagesy($img);
    $width  = c::get('uploadresize.width', 800);
    $height = c::get('uploadresize.height', 534);
    $endPosX = ($width - $imgWidth) / 2;

    //calc the IMG aspectratio
    $ratioHeight = ($imgHeight * 1) / $imgWidth;
    $newHeight = ($imgWidth * $ratioHeight) / 1;

    //Resize the image
    $imgresize = imagescale($img, $width, $height);

    //save as jpg
    imagejpeg($imgresize, $savePath, 100);
}

function createLandscape($filePath){
    $fileParts = pathinfo($filePath);
    $savePath = $filePath;  //full path to saved png, including filename and extension
    $colorRgb = c::get('uploadresize.color', ['red' => 229, 'green' => 231, 'blue' => 232]);  //background color

    if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg'){
        $img = imagecreatefromjpeg($filePath);
    }

    if($fileParts['extension'] == 'png'){
        $img = imagecreatefrompng($filePath);
    }

    $imgWidth = imagesx($img);
    $imgHeight = imagesy($img);
    $width  = c::get('uploadresize.defaults.width', 800);
    $height = c::get('uploadresize.defaults.height', 534);

    //calc the IMG aspectratio
    $aspectRatioHeight = ($height * 1) / $width;
    $newAspectRatioWidth = ($height * $aspectRatioHeight) / 1;

    //calc middle position
    $endPosX = ($width - $newAspectRatioWidth) / 2;

    //Resize the image
    $imgresize = imagescale($img, $newAspectRatioWidth, $height);

    //create new image and fill with background color
    $backgroundImg = imagecreatetruecolor($width, $height);

    $color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
    imagefill($backgroundImg, 0, 0, $color);

    //copy original image to background
    imagecopy($backgroundImg, $imgresize, $endPosX, 0, 0, 0, $newAspectRatioWidth, $height);

    //save as jpg
    imagejpeg($backgroundImg, $savePath, 100);
}

Bildschirmfoto 2019-05-24 um 03.25.35.jpg
 
Zurück