graham23s Posted November 27, 2007 Share Posted November 27, 2007 Hi Guys, i did some pretty basic testing with GD , this code should overlay a watermark/text on uploaded images but im getting a few errors: part of the code: <?php $uploaddirectory = "uploads/".$renamedimage; // Get identifier for white $white = imagecolorallocate($uploaddirectory, 255, 255, 255); // Add text to image imagestring($uploaddirectory, 3, 5, imagesy($uploaddirectory)-20, 'www.myliveaudition.com', $white); ?> but i'm getting these errors: Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/.castle/graham23s/www.site.com/uploadpic.php on line 78 Warning: imagesy(): supplied argument is not a valid Image resource in /home/.castle/graham23s/www.site.com/uploadpic.php on line 81 Warning: imagestring(): supplied argument is not a valid Image resource in /home/.castle/graham23s/www.site.com/uploadpic.php on line 81 not sure what to make of the errors exactly, does it mean the image isn't getting picked up by the gd functions? cheers guys Graham Quote Link to comment Share on other sites More sharing options...
boushley Posted November 27, 2007 Share Posted November 27, 2007 Well, the imagecolorallocate function doesn't want a string thtas the path to the image... but it wants a resource handler. On php.net under imagecolorallocate it says... int imagecolorallocate ( resource $image , int $red , int $green , int $blue ) An image resource, returned by one of the image creation functions, such as imagecreatetruecolor(). So fixing that... will help a lot. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 27, 2007 Share Posted November 27, 2007 Does $renamedimage contain the image name and extension? Is that the image you trying to load? If so, you'll need to use one of the imagecreatefromgif/jpeg/png functions depending on the type. Once yo've done that, you can then use the other gd functions. Quote Link to comment Share on other sites More sharing options...
helraizer Posted November 27, 2007 Share Posted November 27, 2007 <?php $uploaddirectory = "uploads/".$renamedimage; // Get identifier for white $white = imagecolorallocate($uploaddirectory, 255, 255, 255); // Add text to image imagestring($uploaddirectory, 3, 5, imagesy($uploaddirectory)-20, 'www.myliveaudition.com', $white); ?> At the moment you don't have an image.. you need: <?php //$image = ImageCreate(800,600); $image = CreateImageFromJpeg("uploads".$renamedimage); // file extension must be .jpg $white = imagecolorallocate($image, 255, 255, 255); imagestring($image, 3, 5, imagesy($uploaddirectory)-20, 'www.myliveaudition.com', $white); ?> In theory, that should work. Quote Link to comment Share on other sites More sharing options...
graham23s Posted November 27, 2007 Author Share Posted November 27, 2007 i see so i need to actually load the image be it whatever extension, CreateImageFromJpeg essentially recreates the exact same image to work with is that right? also can the text be written to the already saved image? or would i need to re-save it again? thanks a lot guys Graham Quote Link to comment Share on other sites More sharing options...
helraizer Posted November 27, 2007 Share Posted November 27, 2007 i see so i need to actually load the image be it whatever extension, CreateImageFromJpeg essentially recreates the exact same image to work with is that right? Yes, but no not as such.. jpg images must be loaded through CreateImageFromJpeg(); png images through CreateImageFromPng(); gif images through CreateImageFromGif(); As far as I'm aware if you load a jpg image through CreateImageFromGif(), it won't work. (Or that's how it is in theory anyway) also can the text be written to the already saved image? or would i need to re-save it again? It won't automatically save it again. That's a part I missed off before. You need to add to the bottom of your code. <?php header("Content-Type: image/jpeg"); // tell the browser what we're going to give it - jpeg image, in this case. ImageJpeg($image); // paint the image in browser - this doesn't save the file but shows it in the browser ImageJpeg($image, "./newimage.jpg"); //export as jpg file - saves it as the filename you give it ImageDestroy($image); // clean up resources ?> Hope that helps? Quote Link to comment Share on other sites More sharing options...
graham23s Posted November 27, 2007 Author Share Posted November 27, 2007 it does indeed mate i am definately getting there i tried to make this a function: calling the function: <?php ## watermark function watermark_image($filename,$uploaddirectory); ?> This passes the $filename of the uploaded file to get the extension function: <?php function watermark_image($filename,$uploaddirectory) { ## Using GD ## Find out the files extension $extension = explode(".", $filename); $extension = $extension[count($extension)-1]; ## Make all filenames lowercase $extension = strtolower($extension); ## Generate image accordingly if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg") { $image = imagecreatefromjpeg($uploaddirectory); } elseif($extension == "png") { $image = imagecreatefrompng($uploaddirectory); } elseif($extension == "gif") { $image = imagecreatefromgif($uploaddirectory); } $white = imagecolorallocate($image, 255, 255, 255); imagestring($image, 3, 5, imagesy($image)-20, 'www.myliveaudition.com', $white); echo $extension; } ?> this all looks ok , but when i test it, it just uploads without any errors (but doesn't overlay the etxt) so theres no errors to speak of, if i echo out the extension it's fine can you see any errors at all? thanks mate Graham Quote Link to comment Share on other sites More sharing options...
helraizer Posted November 27, 2007 Share Posted November 27, 2007 Looks good. I can't see any errors myself, but I may have overlooked some. If you add error_reporting(E_ALL); ini_set('display_errors', true); to the top of your page. PHP will display any errors for you. Sam Quote Link to comment Share on other sites More sharing options...
graham23s Posted November 28, 2007 Author Share Posted November 28, 2007 solved it thanks mate:) Graham Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.