1

このようなことをして、画像の幅と高さを取得しようとしています。

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

ファイルにこの2行しかなく、他のファイルにリンクしていない場合でも、このエラーが発生します。画像はサーバーにあり、何が問題なのかわかりません。誰でも助けてくれませんか?ありがとうございました。

警告: imagesx(): 指定された引数は .. の有効な画像リソースではありません
警告: imagesy(): 指定された引数は .. の有効な画像リソースではありません

4

2 に答える 2

2

imagesy()最初のパラメーターとして期待されるように、画像リソースを作成する必要があります。imagecreatefromjpeg()ファイル名から作成でき ます:

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

または、画像の幅と高さだけを取得する必要がある場合は、次のgetimagesize関数を利用できます。

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

すぐにファイル名を受け入れ、gd イメージ リソースを作成する必要はありません。

于 2012-09-27T03:51:06.753 に答える
-1

    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');
       */
    ?>
于 2014-12-03T11:57:23.107 に答える