【问题标题】:How to solve imagesx() imagexy() error如何解决 imagesx() imagexy() 错误
【发布时间】:2012-09-27 03:18:46
【问题描述】:

我正在尝试通过这样做来获取图像的宽度和高度。

$width  = imagesx("abc.jpg");
$height = imagesy("abc.jpg");

即使我在文件中只有这两行而没有链接到任何其他文件,我仍然收到此错误。图像在服务器中,我不知道出了什么问题。任何人都可以帮忙吗?谢谢你。

警告:imagesx():提供的参数不是 ..
中的有效图像资源 警告:imagesy():提供的参数不是..中的有效图像资源。

【问题讨论】:

    标签: php php-gd


    【解决方案1】:

    您需要创建一个图像资源,因为imagesy() 期望它作为第一个参数。可以使用您的文件名中的imagecreatefromjpeg() 创建:

    $image = imagecreatefromjpeg("abc.jpg");
    if ($image) {
        $height = imagesy($image);
        imagedestroy($image);
    }
    

    或者,如果您只需要获取图像宽度和高度,您可以使用getimagesize 函数:

    list($width, $height) = getimagesize("abc.jpg");
    

    它直接接受文件名,不需要创建 gd 图像资源。

    【讨论】:

      【解决方案2】:

          class SimpleImage {
      
            var $image;
            var $image_type;
            var $location;
      
             function load($filename) {
                $image_info = getimagesize($filename);
                $this->image_type = $image_info[2];
                if( $this->image_type == IMAGETYPE_JPEG ) {
                   $this->image = imagecreatefromjpeg($filename);
                } elseif( $this->image_type == IMAGETYPE_GIF ) {
                   $this->image = imagecreatefromgif($filename);
                } elseif( $this->image_type == IMAGETYPE_PNG ) {
                   $this->image = imagecreatefrompng($filename);
                }
             }
      
      
      
             function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
                if( $image_type == IMAGETYPE_JPEG ) {
                   imagejpeg($this->image,$filename,$compression);
                } elseif( $image_type == IMAGETYPE_GIF ) {
                   imagegif($this->image,$filename);         
                } elseif( $image_type == IMAGETYPE_PNG ) {
                   imagepng($this->image,$filename);
                }   
                if( $permissions != null) {
                   chmod($filename,$permissions);
                }
             }
             function output($image_type=IMAGETYPE_JPEG) {
                if( $image_type == IMAGETYPE_JPEG ) {
                   imagejpeg($this->image);
                } elseif( $image_type == IMAGETYPE_GIF ) {
                   imagegif($this->image);         
                } elseif( $image_type == IMAGETYPE_PNG ) {
                   imagepng($this->image);
                }   
             }
             function getWidth() {
                return imagesx($this->image);
             }
             function getHeight() {
                return imagesy($this->image);
             }
             function resizeToHeight($height) {
                $ratio = $height / $this->getHeight();
                $width = $this->getWidth() * $ratio;
                $this->resize($width,$height);
             }
             function resizeToWidth($width) {
                $ratio = $width / $this->getWidth();
                $height = $this->getheight() * $ratio;
                $this->resize($width,$height);
             }
             function scale($scale) {
                $width = $this->getWidth() * $scale/100;
                $height = $this->getheight() * $scale/100; 
                $this->resize($width,$height);
             }
             function resize($width,$height) {
                $new_image = imagecreatetruecolor($width, $height);
                imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
                $this->image = $new_image;   
             }
      
          //// own stuff added 14/06/2010
      
              function getType($filetype) {
                if( $filetype == 'image/jpeg' ) {
                   $ext = 'jpg';
                } elseif( $filetype == 'image/pjpeg' ) {
               $ext = 'jpg';
                } elseif( $filetype == 'image/gif'  ) {
                   $ext = 'gif';
                } elseif( $filetype == 'image/png' ) {
                   $ext = 'png';
                }
                return $ext;
             }
      
      
             function randomise(){
      
              // Get a random set of 3 chars which we will append to the filename to prevent duplicate file names.
              $keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
              $length = 3;
              $randkey = "";
              for ($i=0;$i<$length;$i++)  $randkey .= substr($keychars, rand(1, strlen($keychars) ), 1); 
      
              // Set the name of the file  (current time + the random value + . + the file extension)
              $filename = time().$randkey;
      
              return $filename;
      
             }
      
      
             function uploadimage($location, $filename, $filetype, $i) {
             /*
                print "location: ".$location;
                print "name: ".$filename;
                print "type: ".$filetype;
                print "num: ".$i;
                die('11');
             */   
                 $ext = $this->getType($filetype);
                 $newfilename = $this->randomise();
      
                      if($ext <> ""){
      
                          $file = $newfilename.".".$ext;
                          $uploadfile = $location.$file;
      
                              // Move the file to the server.  If move is successful store the file info to the database
                              if ((move_uploaded_file($_FILES["image"]["tmp_name"][$i], $uploadfile))or die("Couldn't copy the file!".$_FILES["image"]["tmp_name"][$i])){
      
                                  return $file;
      
                              }               
                      }               
      
              }      
          }
      

      ?>

          <?php
          // user like this
          /*
             include('SimpleImage.php');
             $image = new SimpleImage();
             $image->load('picture.jpg');
             $image->resize(250,400);
             $image->resizeToWidth(250);
             $image->scale(50);
              $image->resizeToHeight(500);
             $image->save('picture2.jpg');
             */
          ?>
      

      【讨论】:

      • 请提供一些细节
      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2018-06-30
      • 2012-02-18
      • 2011-08-30
      • 2014-06-13
      • 2011-07-06
      • 2018-09-17
      • 2019-08-14
      相关资源
      最近更新 更多