Jump to content

need help understanding object oriented related stuff


ndefontenay

Recommended Posts

Hi,

 

I wrote a classFile to handle files that looks like this:

 

 

class classfile {
    //path_parths is an array, use it this way
    //path_parts["filename"] -> filename
    //path_parts["dirname"] -> directory
    //path_parts["extension"]
    //path_parts["basename"]
    protected $path_parts;
    private $fullpath;

    function __construct($filename){
        if(file_exists($filename)){
            $this->fullpath = $filename;
            $this->path_parts= pathinfo($this->fullpath);
        }
        else{
            die("File or Folder doesn't exist");
        }
    }

    function move($destination){
        if (file_exists($destination)){
       rename($this->fullpath, $destination . "/" . $this->getfilename());
       //Update properties
       $this->setfullpath($destination . $this->getfilename());
       $this->setpath_parts();
        }
        else{
            die("The folder specified doesn't exist");
        }
    }

    function copy($destination){
            copy($this->fullpath, $destination);
    }

    function getpath(){
        return $this->path_parts["dirname"];
    }

    function getfilename(){
        return $this->path_parts["basename"];
    }

    function getextension(){
        return $this->path_parts["extension"];
    }

    function getfullpath(){
        return $this->fullpath;
    }

    function setfullpath($path){
        $this->fullpath = $path;
    }

    function setpath_parts(){
        $this->path_parts = pathinfo($this->fullpath);
    }

    function  __destruct() {
        ;
    }

}
?>

 

Then I extended it with a class to handle images that looks like this:

 

include 'c:/wamp/www/classfile.php';

class classimage extends classfile{
   var $image;
   var $image_info;   
   var $image_type;

   function __construct($filename){
       parent::__construct($filename);
      $this->image_info = getimagesize(parent::getfullpath());
      $this->image_type = $this->image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg(parent::getfullpath());
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif(parent::getfullpath());
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng(parent::getfullpath());
      }
   }

   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=80, $permissions=777) {
      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 $this->image_info[0];
   }

   function getHeight() {
      return $this->image_info[1];
   }

   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);
      echo "resized to:\n";
      echo $width . "\n";
      echo $height . "\n";
      echo "imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight())\n";
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }

   function format(){

       $width = $this->getWidth();
       $height = $this->getHeight();
       
       if ($width > $height){
           return "landscape";
       }

       if ($height > $width){
           return "portrait";
       }
       
       if ($height == $width){
            return "square";
       }
   }

   function isTooBig($maxsize){
        $width = $this->getWidth();
        $height = $this->getHeight();

        if ($width > $maxsize || $height > $maxsize){
            return true;
        }
        else{
            return false;
        }
   }

   function  __destruct() {
        ;
    }
}
?>

 

Now that I got this, I loop through images located in a folder that way:

 

if ($handle = opendir($directory)) {
    while (false !== ($file = readdir($handle))) {

//Instantiate the object
           $image = new classimage($directory . "/" . $file);

//And do quite a lot of verification and particularly this:
                             if ($image->format() == "landscape") {
                                  echo "width: " . $image->getWidth() . "\n";
                                  echo "height: " . $image->getHeight() . "\n";
                                  if ($image->isTooBig($maxsize_mini)) {
                                  $image->resizeToWidth($maxsize_mini);
                                  $image->save($directory . "/mini_" . $image->getfilename());
                            }
      }
}

 

And now to the problem:

 

I echo'ed the resize function to see what's being run and I got that:

picture1

imagecopyresampled(Resource id #26, Resource id #25, 0, 0, 0, 0, 608, 405.65, (), ())

picture2

imagecopyresampled(Resource id #27, Resource id #26, 0, 0, 0, 0, 250, 166.796875, (), ())

 

It looks like it's reusing an instantiation of the previous object and so on and so forth.

 

In my mind, the way it should look is:

 

picture1

imagecopyresampled(Resource id #2, Resource id #1, 0, 0, 0, 0, 608, 405.65, (), ())

picture2

imagecopyresampled(Resource id #2, Resource id #1, 0, 0, 0, 0, 250, 166.796875, (), ())

 

The previous object is destructed, the new one has the old id.

 

how does that work?

 

The reason I ask and wonder about this is because I'm having weird result for the resize such as a ridiculously small picture mini size in the actual mini size picture (250 px) except whatever is left is filled with black.

 

Link to comment
Share on other sites

That resource ID number is only for internal use. It means that PHP created that many resources of any type in the script. Even without knowing what happened in the code before what you posted,

if ($handle = opendir($directory)) {

right there you have a new resource. If anything, $handle would be resource #1, but even then you might not get 1 - a test script I just made has me starting at #5.

 

$this->image will be some resource number X. As long as you get $new_image as resource number X+1 then there's nothing wrong with that code. And even then, don't rely upon it necessarily being that way.

 

Now with that out of the way,

The reason I ask and wonder about this is because I'm having weird result for the resize such as a ridiculously small picture mini size in the actual mini size picture (250 px) except whatever is left is filled with black.

Maybe it's just me and my being tired but that sentence is confusing.

Link to comment
Share on other sites

Thanks for the quick reply.

 

Maybe it's just me and my being tired but that sentence is confusing.

 

True.

 

I'm sorry about that. It's a bit hard to explain without actually showing the picture.

I think a picture would explain better than a thousand words.

 

If you've seen the picture:

 

1) I got the same thing going on for medium size pictures.

2) Then I check for mini size pictures and perform the same task

 

For medium it works fine, for mini I got the weird result.

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.