【问题标题】:How to get image size from base 64 string in php如何从 php 中的 base 64 字符串获取图像大小
【发布时间】:2020-08-12 20:48:50
【问题描述】:

我正在获取图像字符串的 base 64 我必须在文件夹中移动并将图像路径存储在数据库中,但我必须限制文件大小。我该怎么做:

我从 base 64 字符串生成图像的代码如下:

/** if image is attached with request **/
 $Image = "MyBase64StringHere";
 list($type, $Image) = explode(';', $Image);
 list(, $Image)      = explode(',', $Image);

/** decode the base 64 image **/
 $Image = base64_decode($Image);

I have tried to get image size like:
$size =  getimagesize($Image);

但是从中获取文件的宽度和高度。有人可以告诉我如何从中获取文件大小。谢谢

【问题讨论】:

  • 您的$path 包含相对路径,请您尝试使用绝对路径 - 比如http://www.example.com/uploads/....jpg
  • strlen($Image) 可以吗?
  • @Lashane, strlen 用于获取字符串长度。 OP 需要图片尺寸。
  • @prava OP 想要文件大小,在这种情况下等于字符串长度
  • 但是我想在移动到文件夹之前验证图像大小,可以吗?

标签: php file-upload base64


【解决方案1】:

请检查:

解码后试试这个:getimagesizefromstring(),如果你用PHP 5.4,如果用PHP 5.3,那么你可以用下面的方法检查。

<?php
if (!function_exists('getimagesizefromstring')) {
    function getimagesizefromstring($data)
    {
        $uri = 'data://application/octet-stream;base64,' . base64_encode($data);
        return getimagesize($uri);
    }
}

【讨论】:

    【解决方案2】:

    getimagesize() 函数也应该与 Data URI 一起使用:

    $Image = "MyBase64StringHere";
    $size = getimagesize($Image);
    

    【讨论】:

      【解决方案3】:

      在移动到文件夹之前获取图像大小的想法不好。然后我决定将图像移动到一个临时文件夹并使用图像路径获取大小,然后验证图像大小。这样我可以轻松验证图像大小并防止如果超出大小限制,则将其存储在数据库中。感谢大家的时间。

      我更新后的代码是这样的:

      /** if image is attached with request **/
      $Image = "Yourbase64StringHere";
      list($type, $Image) = explode(';', $Image);
      list(, $Image)      = explode(',', $Image);
      
      /** decode the base 64 image **/
      $Image = base64_decode($Image);
      
      /* move image to temp folder */
      $TempPath = 'temp/'.time().".jpg";
      file_put_contents($TempPath, $Image);
      
      $ImageSize = filesize($TempPath);/* get the image size */
      
      if($ImageSize < 83889000){ /* limit size to 10 mb */
      
      /** move the uploaded image **/
      $path = 'uploads/'.time().".jpg";
      file_put_contents($path, $Image);
      
      $Image = $path;
      /** get the image path and store in database **/
      
      unlink($TempPath);/* delete the temporay file */
      
      }else{
      
      unlink($TempPath);/* delete the temporay file */
      
      /** image size limit exceded **/
      
      }
      

      【讨论】:

        【解决方案4】:

        试试这个以字节、KB、MB 为单位获取文件大小..

        public function getBase64ImageSize($base64Image){ //return memory size in B, KB, MB
            try{
                $size_in_bytes = (int) (strlen(rtrim($base64Image, '=')) * 3 / 4);
                $size_in_kb    = $size_in_bytes / 1024;
                $size_in_mb    = $size_in_kb / 1024;
        
                return $size_in_mb;
            }
            catch(Exception $e){
                return $e;
            }
        }
        

        【讨论】:

          【解决方案5】:

          由于base-64算法增加图像尺寸高达33%,我强烈建议您这样使用

          $size = (int)(strlen(rtrim($image, '=')) * 0.75); // in byte
          

          【讨论】:

          • 这是一个非常简单的解决方案,谢谢!
          猜你喜欢
          • 2020-01-18
          • 1970-01-01
          • 2017-10-24
          • 1970-01-01
          • 2022-01-04
          • 2017-05-17
          • 2019-03-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多