【问题标题】:Error while creating thumbnailes only for some images using php仅使用 php 为某些图像创建缩略图时出错
【发布时间】:2019-08-31 18:27:41
【问题描述】:

我在后端使用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 在第 157 行\nPHP 消息:PHP 注意:未定义变量: 第 160 行 fcm.php 中的 file_ext\nPHP 消息:PHP 注意:未定义 变量:第 137 行的 file_ext fcm.php\nPHP 消息:PHP 注意: 未定义变量:file_ext

等等,我想再次提一下,这种情况只发生在少数图片上,而不是全部,其余的都成功上传并创建了缩略图。塔恩克斯

【问题讨论】:

  • 这可能是因为那些少数图像可能具有“区分大小写”的扩展名或 fpm 不支持的扩展名。如果某种类型的图像命中您的功能,请尝试在您的切换案例中调试您的逻辑所在的位置。您可以使用 echo / die 等。
  • 好的,谢谢,我现在就尝试实现它
  • 我尝试这样做,但这次没有错误不幸或幸运的是,但我注意到 Windows 上的一些东西,当我按类型应用排序时,这些没有上传创建缩略图的照片有点不同与窗口上的其他图像一起排序,即使它们具有所有相同的属性,这对我来说太奇怪了。
  • 通过print_r($source_url);检查您在 $_FILES 中获得的这些特定图像之一的内容

标签: php image thumbnails


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-03-25
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 2011-12-31
    • 2011-10-27
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多