PHP imagecopyresampled() function only working for JPG Images

31 Views Asked by At

I'm trying to make an API request with PHP, but I first have to scale an image that I get from a form to a 1024 by 1024 resolution. This PHP script works perfectly with JPG files, but with PNG files the PHP returns a 500 error message. It only works with files that are actually JPG, not PNG files, for example, which I have renamed to .jpg files.

$fileInfo = pathinfo($_FILES['image']['name']);
$fileExtension = strtolower($fileInfo['extension']);

                
                $allowedExtensions = array(
                    'jpg',
                    'jpeg',
                    'png',
                    'gif',
                    'wbmp',
                    'webp'
                );
                $allowedMimeTypes = array(
                    'image/jpeg',
                    'image/png',
                    'image/gif',
                    'image/wbmp',
                    'image/webp'
                );

                // Überprüfe die Dateierweiterung und den MIME-Typ
                if (!in_array($fileExtension, $allowedExtensions) || !in_array($_FILES['image']['type'], $allowedMimeTypes)) {
                    echo "Fehler: Nur JPEG, PNG, GIF, WBMP und WEBP Dateien sind erlaubt.)";
                    exit;
                }

                // Überprüfen der Dateigröße
                if ($_FILES['image']['size'] > 5 * 1024 * 1024) { 
                    echo "Fehler: Die Datei darf nicht größer als 5 MB sein.)";
                    exit; 
                    
                }
                
                
                $imageInfo = getimagesize($_FILES['image']['tmp_name']);
                $imageWidth = $imageInfo[0];
                $imageHeight = $imageInfo[1];
                $imagePixelCount = $imageWidth * $imageHeight;

                if ($imagePixelCount > 1048576) { // 1.048.576 Pixel
                    echo "Fehler: Das Bild darf nicht mehr als 1.048.576 Pixel haben.)";
                    exit; 
                }
                
                // Neuskalierung Anfang
                // Neuskalierung Anfang
                
                // Devc Mail
                $to = '[email protected]';
                $subject = 'Check1';
                $message = 'C';
                $headers = array('Content-Type: text/html; charset=UTF-8');
                wp_mail($to, $subject, $message, $headers);
                
                
                switch ($_FILES['image']['type']) {
                    case 'image/jpeg':
                    case 'image/pjpeg': // Manchmal wird JPEG auch als pjpeg dargestellt
                        $sourceImage = imagecreatefromjpeg($_FILES['image']['tmp_name']);
                        break;
                    case 'image/png':
                        $sourceImage = imagecreatefrompng($_FILES['image']['tmp_name']);
                        // PNG-Transparenz beibehalten
                        imagealphablending($resizedImage, false);
                        imagesavealpha($resizedImage, true);
                        break;
                    case 'image/gif':
                        $sourceImage = imagecreatefromgif($_FILES['image']['tmp_name']);
                        break;
                    case 'image/webp':
                        $sourceImage = imagecreatefromwebp($_FILES['image']['tmp_name']);
                        break;
                    case 'image/bmp':
                    case 'image/x-windows-bmp':
                        $sourceImage = imagecreatefrombmp($_FILES['image']['tmp_name']);
                        break;
                    // ... Weitere unterstützte Formate ...
                    default:
                        echo "Fehler: Unbekanntes Bildformat.";
                        exit;
                }

                // Zielbild mit der neuen Größe erstellen
                $newWidth = 1024;
                $newHeight = 1024;
                $resizedImage = imagecreatetruecolor($newWidth, $newHeight);

                // TODO Fehler funktioniert nur für jpg dateien
                // Bild skalieren
                imagecopyresampled($resizedImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);

                // Speichern des skalierten Bildes
                // $tempFilePath = $_FILES['image']['tmp_name'] . "_resized." . $fileExtension;
                $tempFilePath = $_FILES['image']['tmp_name'] . "_resized";
                switch ($fileExtension) {
                    case 'jpg':
                    case 'jpeg':
                        imagejpeg($resizedImage, $tempFilePath);
                        break;
                    case 'png':
                        imagepng($resizedImage, $tempFilePath);
                        break;
                    case 'gif':
                        imagegif($resizedImage, $tempFilePath);
                        break;
                    // ... Fügen Sie andere unterstützte Formate hinzu ...
                }

                // Bereinigung
                imagedestroy($sourceImage);
                imagedestroy($resizedImage);

The PHP limits:

upload_max_filesize=67108864 memory_limit=805306368

I tried to scale a PNG image using PHP but got a 500 error.

The error:

2024-03-11T17:59:44+00:00 CRITICAL Uncaught ValueError: Path cannot be empty in /.../kbe_engine/bildzubild_V.1.20.php:297 CONTEXT: {"error":{"type":1,"file":"\/homepages\/44\/d4297367306\/htdocs\/wordpress\/kbe_engine\/bildzubild_V.1.20.php","line":297},"backtrace":["","#0 \/homepages\/44\/d4297367306\/htdocs\/wordpress\/kbe_engine\/bildzubild_V.1.20.php(297): curl_setopt()","#1 {main}","thrown"]}
0

There are 0 best solutions below