0

I've written a wrapper class for Gearman as below:

class gearman {

    private $server_ip = '127.0.0.1';
    private $server_port = '4730';
    public function registerWorker($qName,$function){ // register function
        if (!($qName && $function)) return false;
        $qWorker = new GearmanWorker();
        $qWorker->addServer($this->$server_ip, $this->$server_port); // register worker in server
        $qWorker->addFunction($this->getQName($qName), $function);
        return $qWorker;
    }

    public function registerClient(){ //register client
        $qClient = new GearmanClient();
        $qClient->addServer($this->$server_ip, $this->$server_port); // send job to server
        return $qClient;
    }
public function doBg($function,$workload,$qClient = null){
    if (!$function) return false;
    if (!$qClient)
    {
        $qClient = $this->registerClient();
    }
    return $qClient->doBackground($this->getQName($function), json_encode($workload));
}
}

These are just part of my class. I have another class for upload. This is just the part that I save images in my server:

public function resize( $x, $y )
    {
        $new = imagecreatetruecolor($x, $y);
        imagealphablending($new, false);
        imagesavealpha($new, true);


        imagecopyresampled($new, $this->image, 0, 0, 0, 0, $x, $y, imagesx($this->image), imagesy($this->image));
        $this->image = $new;
    }
public function save( $location, $type='', $quality=100 )
    {
        $type = ( $type == '' ) ? $this->type : $type;

        if( $type == IMAGETYPE_JPEG ) 
        {
            if (!imagejpeg( $this->image, $location, $quality)) { // error happened
                    return false;
            }
        } 
        elseif( $type == IMAGETYPE_GIF ) 
        {
            if (!imagegif( $this->image, $location )) {
                return false;   
            }
        } 
        elseif( $type == IMAGETYPE_PNG ) 
        {
            if (!imagepng( $this->image, $location )) {
                    return false;
            }
        }
        return true;
    }

Should I instantiate Gearman in upload constructor? Where should I put worker? I don't exactly know where to put my gearman client to do the task of uploading or resizing.

I would appreciate if anyone could shed some light on the subject.

Alireza
  • 6,497
  • 13
  • 59
  • 132

1 Answers1

1

There's an example script on gearman website using ImageMagick. You might want to take a look at how they've done it.

Anticom
  • 975
  • 13
  • 29