使用 php 仅为某些图像创建缩略图时出错

Error while creating thumbnailes only for some images using php

我在后端使用php7.2-fpm和apache2服务器上传多张图片时出错,很多图片中只有几张图片,所以我想知道我的代码是否有它有什么问题,或者真正的问题是什么。 这是导致错误的代码:

function compress_image($source_url, $destination_url, $quality) {

        $info = getimagesize($source_url);

        if ($info['mime'] == 'image/jpeg')
              $image = imagecreatefromjpeg($source_url);

        else if ($info['mime'] == 'image/gif')
              $image = imagecreatefromgif($source_url);

        else if ($info['mime'] == 'image/png')
              $image = imagecreatefrompng($source_url);

        imagejpeg($image, $destination_url, $quality);
        return $destination_url;
    }

    //Create Thumb Image
    function create_thumb_image($target_folder ='',$thumb_folder = '', $thumb_width = '',$thumb_height = '') {  
     //folder path setup
         $target_path = $target_folder;
         $thumb_path = $thumb_folder;  

         $thumbnail = $thumb_path;
         $upload_image = $target_path;

            list($width,$height) = getimagesize($upload_image);
            $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
            switch($file_ext) {
                case 'jpg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'jpeg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'png':
                    $source = imagecreatefrompng($upload_image);
                    break;
                case 'gif':
                    $source = imagecreatefromgif($upload_image);
                     break;
                default:
                    $source = imagecreatefromjpeg($upload_image);
            }
       imagecopyresized($thumb_create, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width,$height);
            switch($file_ext){
                case 'jpg' || 'jpeg':
                    imagejpeg($thumb_create,$thumbnail,80);
                    break;
                case 'png':
                    imagepng($thumb_create,$thumbnail,80);
                    break;
                case 'gif':
                    imagegif($thumb_create,$thumbnail,80);
                     break;
                default:
                    imagejpeg($thumb_create,$thumbnail,80);
            }
    }

这是我在 error.log 文件

中得到的错误提示

fcm.php on line 157\nPHP message: PHP Notice: Undefined variable: file_ext in fcm.php on line 160\nPHP message: PHP Notice: Undefined variable: file_ext fcm.php on line 137\nPHP message: PHP Notice: Undefined variable: file_ext

等,并想再次提及,这种情况只发生在少数图像上,而不是所有图像上,其余图像已成功上传并创建缩略图。坦克斯

您可以在不改变原图纵横比的情况下使用以下代码生成图片缩略图。 而这里的$img是存放原图的图片路径

            $sourceAppImgPath = $this->images->absPath($img);
            $file_dimensions = getimagesize($sourceAppImgPath);
            $ImageType = strtolower($file_dimensions['mime']);

                switch(strtolower($ImageType))
                {
                    case 'image/png':
                        $image = imagecreatefrompng($sourceAppImgPath);
                        break;
                    case 'image/gif':
                        $image = imagecreatefromgif($sourceAppImgPath);
                        break;
                    case 'image/jpeg':
                        $image = imagecreatefromjpeg($sourceAppImgPath);
                        break;
                    default:
                        return false; //output error
                }

                    $origWidth = imagesx($image);
                    $origHeight = imagesy($image);
                    $maxWidth = 300;
                    $maxHeight =300;

                    if ($maxWidth == 0)
                        $maxWidth  = $origWidth;

                    if ($maxHeight == 0)
                        $maxHeight = $origHeight;

                    $widthRatio = $maxWidth / $origWidth;
                    $heightRatio = $maxHeight / $origHeight;
                    $ratio = min($widthRatio, $heightRatio);
                    $thumb_width  = (int)$origWidth  * $ratio;
                    $thumb_height = (int)$origHeight * $ratio;