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

时间:2019-04-10 10:20:00

标签: php image thumbnails

在后端使用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文件中遇到的错误提示

  第157行上的

fcm.php \ nPHP消息:PHP注意:未定义的变量:   fcm.php中的file_ext,行160 \ nPHP消息:PHP注意:未定义   137行上的变量:file_ext fcm.php \ nPHP消息:PHP注意:   未定义的变量:file_ext

等,并想再次提及,这种情况仅发生在少数图像上,而不是全部图像上,其余图像成功上传并创建了缩略图。火车

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码来生成图像缩略图,而无需更改原始图像的纵横比。 $ 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;