How can I find the width of an image using PHP?

前端 未结 7 1648
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 15:40

I was wondering how can I find the width of an image using php.

相关标签:
7条回答
  • 2020-12-10 16:27

    In addition to getimagesize() you can get the dimensions from an image resource using imagesx() and imagesy().

    1. First of all you will have to download the image data using for example file_get_contents()

    2. Then you create the image resource using imagecreatefromstring()

    3. Finally get the image width with imagesx() and the image height with imagesy().

      function get_image_dimensions($img_url=""){
      
              $image_dimensions=array();
              $image_dimensions['w']=0;
              $image_dimensions['h']=0;
      
              if($image_data=custom_file_get_contents($img_url)){
      
                  $image_resource = imagecreatefromstring($image_data);
                  $image_dimensions['w']=imagesx($image_resource);
                  $image_dimensions['h']=imagesy($image_resource);
                  imagedestroy($image_resource);
              }
      
              return $image_dimensions;
      }
      
    0 讨论(0)
提交回复
热议问题