The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-Image - Watermarking - expecting resource error
Discuss Watermarking - expecting resource error in the PHP Development forum on Dev Shed. Watermarking - expecting resource error PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 25th, 2012, 12:44 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
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, $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.
|

December 25th, 2012, 02:59 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
You have to call imagecopymerged() with an image resource, not a filename.
|

December 25th, 2012, 03:39 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
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?
|

December 25th, 2012, 03:46 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 10
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.
|

December 25th, 2012, 03:56 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
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.
|

December 25th, 2012, 05:09 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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.
|

December 25th, 2012, 05:18 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|