PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 25th, 2012, 12:44 AM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
PHP-Image - 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.

Reply With Quote
  #2  
Old December 25th, 2012, 02:59 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 29 m 55 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
You have to call imagecopymerged() with an image resource, not a filename.

Reply With Quote
  #3  
Old December 25th, 2012, 03:39 AM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
Quote:
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?

Reply With Quote
  #4  
Old December 25th, 2012, 03:46 AM
MT1 MT1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 10 MT1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 40 m 37 sec
Reputation 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.

Reply With Quote
  #5  
Old December 25th, 2012, 03:56 AM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
Quote:
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.

Reply With Quote
  #6  
Old December 25th, 2012, 05:09 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 29 m 55 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
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.

Quote:
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.

Reply With Quote
  #7  
Old December 25th, 2012, 05:18 AM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-Image - Watermarking - expecting resource error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap