Watermarking - expecting resource error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nbasso713
Forum Commoner
Posts: 29
Joined: Fri Nov 04, 2011 7:51 am

Watermarking - expecting resource error

Post by nbasso713 »

So i've been bashing my head against the wall for hours trying to figure this out. Originally i started with a function, but ended up copying an altering my image resizing class to create a class for watermarking images.

The Problem: I keep receiving the error "Warning: imagecopymerge() expects parameter 2 to be resource"
Note: I have tried using both the global $_FILES and a saved image.

The Call:

Code: Select all

   if($extension == 'jpg' || $extension == 'png' || $extension == 'jpeg' && $width >= 200){
   $image = new WatermarkImage();
   $image->load($_FILES['image']['tmp_name']);
   $image->save($_FILES['image']['tmp_name']);
	}  
The Class:

Code: Select all

class WatermarkImage {
 
   var $image1;
   var $image_type;
 
   function load($filename) {
 
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
          
          $watermark = imagecreatefrompng("classes/watermark.png");
    list($width, $height) = getimagesize($filename);
    
    //set new image height (image height + watermark height) 
    $newHeight = $height + 40;
    
    //create canvas
    $this->image1 = imagecreate($width, $newHeight);
    
    //choose RGB color
    $white = imagecolorallocate($this->image1,233,234,235);
    
    //fill canvas
    imagefill($this->image1,0,0,$white);
    
    //calculate watermark cords
    $y = $newHeight-40;
    $x = ($width - 200)/2;
    
    //tosses primary image at the top of canvas
    imagecopymerge($this->image1, $filename, 0, 0, 0, 0, $width, $height, 100);
    //centers watermark below image
    imagecopymerge($this->image1, $watermark, $x, $y, 0, 0, 200, 40, 100);
    
    imagedestroy($watermark);
    
         $this->image1 = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
         $this->image1 = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
         $this->image1 = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $permissions=null) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image1,$filename);
         imagedestroy($this->image1);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image1,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image1,$filename);
      }
      if( $permissions != null) {
 
         chmod($filename,$permissions);
      }
   }
}
The Goal:I would like to place the watermark below the image, so i thought it would be best to place the picture and watermark on a new canvas. Sorry if the code's sloppy, I'm still learning and have been testing a bunch of different things trying to get this working.
nbasso713
Forum Commoner
Posts: 29
Joined: Fri Nov 04, 2011 7:51 am

Re: Watermarking - expecting resource error

Post by nbasso713 »

SOLVED
Post Reply