I am a bit of a PHP dabbler/beginner, and I am trying to write a script to watermark uploaded images. The watermark is variable, based on information supplied by the logged-in user.

What I need now should be relatively easy, assuming displayed images (or their paths) are stored in a variable that I can call. Or is there an easier way?


 global $user;
 $name = $user->name;
 $uid = $user->uid;
 $filename = $name;

// first name query and variable
 $fquery = "SELECT value FROM profile_values WHERE uid='$user->uid' AND fid='3'";
 $result = mysql_query($fquery);

 while ($row = mysql_fetch_object($result)) {
    $fname = "$row->value";
 }

// last name query and variable
 $lquery = "SELECT value FROM profile_values WHERE uid='$user->uid' AND fid='4'";
 $result = mysql_query($lquery);

 while ($row = mysql_fetch_object($result)) {
    $lname = "$row->value";
 }

// concatenate the full name
 $fullname = $fname . " " . $lname;

// create an image with width 'x', height 'y'
 $image = imagecreate(200, 20);

// create a background color and then set it to transparent
 $background = imagecolorallocate($image, 255, 255, 255);
 ImageColorTransparent($image, $background);
 ImageInterlace($image, false);

// set black color for the text
 $black = imagecolorallocate($image, 0, 0, 0);

// write the variable string to the image
// the top left of the text is at the position (10, 2)
 imagestring($image, 4, 10, 2, $fullname, $black);

// output the image as a png
 imagepng($image, "tmp/" . $filename . ".png");
 print ("<img src=\"tmp/" . $filename . ".png\">");

// create the watermark image
 $watermark = imagecreatefrompng("tmp/" . $filename . ".png");

// calculate the watermark dimensions
 $watermark_width = imagesx($watermark);
 $watermark_height = imagesy($watermark);
 $image = imagecreatetruecolor($watermark_width, $watermark_height);
 $image = imagecreatefromjpeg($_GET[$need_variable_source]);

// calculate source image  size
 $size = getimagesize($_GET[$need_variable_source]);

// position the watermark on the source image
 $dest_x = $size[0] - $watermark_width - 5;
 $dest_y = $size[1] - $watermark_height - 5;

// put them together 
 imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
 imagejpeg($image);

 print ("<img src=\"files/images/" . $image . ".png\">");

// cleanup
 imagedestroy($image);
 imagedestroy($watermark);

Right now, it all works fine up to the imagecopymerge and imagejpeg functions if I set a static image source. I need variable source, and I need the script ... well, I need it to work. :-)

I appreciate any help I can get.

Comments

sinfield’s picture

Ok, I found the 'files' table in the database, which apparently stores uploaded file locations and the nodes with which they are associated. I guess it is time for me to try to put together another query.

Sorry for the mess above.

Anyone have a quick query to get the active node's image file location and store it to a variable?
I have this, but do not know how to pull the current node:

// fileloc query
 $filequery = "SELECT filepath FROM files WHERE nid='$current_node' AND filename='preview'";
 $result = mysql_query($filequery);

 while ($row = mysql_fetch_object($result)) {
    $fileloc = "$row->filepath";
 }
 
// echo $fileloc;
nedjo’s picture

See the documentation http://drupaldocs.org/api/head/function/hook_nodeapi

In a _nodeapi hook you have access to the $node object.

See also the various _nodeapi functions in the core Drupal modules.

I guess you'd use the 'insert' (or 'update') ops.

It's also a good idea to use Drupal's database abstraction calls (e.g., db_query instead of mysql_query, db_fetch_object rather than mysql_fetch_object), so you can start to collaborate with others. Table names should be in braces, e.g., {filepath} (this means the SQL will work with dbs that have prefixes.)

sinfield’s picture

If the script that I wrote is contained in a block, would it not already be under the nodeapi hook, as well as all the other core hooks?

nedjo’s picture

No, the block doesn't go through nodeapi. You could load the node, using a node id, if the page you're on is in the form node/76 where 76 is the node id. The parts of the page address are accessed by the arg() function.


if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
  $node = node_load(arg(1));
  ....
}