#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2

    Watermarking - expecting resource error


    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:
    PHP Code:
    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:
    PHP Code:
    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$filename0000$width$height100);
        
    //centers watermark below image
        
    imagecopymerge($this->image1$watermark$x$y0020040100);
        
        
    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.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    You have to call imagecopymerged() with an image resource, not a filename.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Originally Posted by requinix
    You have to call imagecopymerged() with an image resource, not a filename.
    Thanks for getting back to me, but I'm a little confused. I thought the source was the image itself?

    What's the difference between an image resource and the image?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    Have you watched Adam Khoury's video on watermarking and image library that he has created.
    Its called php image watermark function.

    What made you change it from a fuction to a class. Is it more secure? I am working on doing this too.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Originally Posted by MT1
    Have you watched Adam Khoury's video on watermarking and image library that he has created.
    Its called php image watermark function.

    What made you change it from a fuction to a class. Is it more secure? I am working on doing this too.
    I'll have to check the video out in a sec. I already had the class written out to re-size images, so i just copied a chunk of it to use as a watermarking class.

    The thought behind the switch was that I'd be able to pass either a jpg or png through a single class, and hopefully use less conditionals in the process. It's also a bit cleaner in my opinion even though that code is a little messy atm, and I also wanted to keep the file types the same.
  10. #6
  11. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    Originally Posted by nbasso713
    Thanks for getting back to me, but I'm a little confused. I thought the source was the image itself?
    I'm not sure where this question is coming from so I'm not sure how to answer it.

    Originally Posted by nbasso713
    What's the difference between an image resource and the image?
    Alright, primer on resources.

    The filename is something which identifies the thing (image) on the filesystem. Compare that with, say, an ID number which might identify the thing in a database table. It's arbitrary and the image*() functions - from the GD extension - don't know how to deal with them.

    They do know how to deal with the binary data, so that's what you have to give to them. The extension provides a few ways of getting that binary data; imagecreatefrompng() being one you use in that code. But GD needs more than just the binary data: it'd be really inefficient to have to scan the data every time you wanted to do something with it. So instead of dealing with binary data it deals with some internal thing that doesn't really matter because you don't have to deal with it directly. imagecreatefrompng() returns an image resource, and this resource corresponds to something in memory (again, doesn't really matter what). That resource tells GD where to find all the information it needs.

    Now, as for the difference between the "image" and the "image resource", that depends on your definition of the former.

    So long story short you have to load the image into memory with something like imagecreatefrompng(), or create a blank one with imagecreatetruecolor(), before you can do anything to it.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Originally Posted by requinix
    So long story short you have to load the image into memory with something like imagecreatefrompng(), or create a blank one with imagecreatetruecolor(), before you can do anything to it.
    That really clarifies everything, thank you very much.

IMN logo majestic logo threadwatch logo seochat tools logo