Watermarking - expecting resource error

images php coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
nbasso713
New php-forum User
New php-forum User
Posts: 18
Joined: Sat Dec 10, 2011 4:00 pm

Watermarking - expecting resource error

Post by nbasso713 » Mon Dec 24, 2012 10:32 pm

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
New php-forum User
New php-forum User
Posts: 18
Joined: Sat Dec 10, 2011 4:00 pm

Re: Watermarking - expecting resource error

Post by nbasso713 » Tue Dec 25, 2012 4:05 am

SOLVED

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Watermarking - expecting resource error

Post by seandisanti » Thu Mar 28, 2013 10:33 pm

Posting the solution even if you've figured it out yourself helps make sure others that encounter the same problem may get exactly the help they're looking for. Thanks in advance for elaborating on your 'SOLVED' response.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests