phpimage-processinglowpass-filterhighpass-filter

Edges appear in image after resizing


I have implemented the following function:

private static function generatePicture($sizeKey, $src, $initialWidth, $initialHeight, $imageType) {
    $destination = AdImage::addSizeKey($sizeKey, $src);
    if (file_exists($destination)) {
        return;
    }
    $finalSize = AdImage::$sizes[$sizeKey];
    $initialRatio = $initialWidth / $initialHeight;
    $finalRatio = $finalSize["w"] / $finalSize["h"];
    $newImage = imagecreatetruecolor($finalSize["w"], $finalSize["h"]);
    imagealphablending( $newImage, false );
    imagesavealpha( $newImage, true );
    $oldImage = imagecreatefromstring(file_get_contents($src));
    if ($initialRatio >= $finalRatio) {
        $scale = $finalSize["h"] / $initialHeight;
        $surpluss = abs($initialWidth - ($initialHeight * $finalRatio));
        imagecopyresized($newImage, $oldImage, 0, 0, $surpluss / 2, 0, $finalSize["w"], $finalSize["h"], $initialWidth - $surpluss, $initialHeight);
    } else {
        $scale = $finalSize["w"] / $initialWidth;
        $surpluss = abs($initialHeight - ($initialWidth / $finalRatio));
        imagecopyresized($newImage, $oldImage, 0, 0, 0, $surpluss / 2, $finalSize["w"], $finalSize["h"], $initialWidth, $initialHeight - $surpluss);
    }
    switch ($imageType) {
        case IMAGETYPE_JPEG: {
            imagejpeg($newImage, $destination, 100);
            break;
        }
        case IMAGETYPE_PNG: {
            imagepng($newImage, $destination);
            break;
        }
        case IMAGETYPE_GIF: {
            imagegif($newImage, $destination);
            break;
        }
        default:
        {
            imagejpeg($newImage, $destination, 100);
            break;
        }
    }

}

However, edges appear, lines are not straight, even if they were originally straight. I believe that I can solve my problem with a combination of high-pass filter, low-pass filter and salt & piper filter, but I am not sure how I implement it for png, jpeg and gif. I would start by studying what is the pattern of these files and apply a filter separately. I wonder if there is a better solution.


Solution

  • I did not need image processing. I was using the wrong function.

    Instead of

    imagecopyresized
    

    I use now

    imagecopyresampled
    

    and the quality of the images has improved.