Page 1 of 1

Joomla 1.0.xx mambot to insert watermarked image...help?

Posted: Sat Feb 09, 2008 12:34 am
by Tim_Myth
I'm trying to code a mambot for my Joomla! 1.0.13 install that will display a watermarked image for guests and a non-watermarkedd image for registered users. This is my code:

Code: Select all

function botSecureWM( $published, &$row, &$params, $page=0 ) {

   // simple performance check to determine whether bot should process further
   if ( strpos( $row->text, 'securewm' ) === false ) {
      return true;
   }

   // define the regular expression for the bot
   $regex = "#{securewm}(.*?){/securewm}#s";
//    $regex = '/{(securewm)\s*(.*?)}/i';

   // check whether mosimage has been disabled for page
   // check whether mambot has been unpublished
   if (!$published || !$params->get( 'image' )) {
      $row->text = preg_replace( $regex, '', $row->text );
      return;
   }

   $row->text = preg_replace_callback( $regex, 'botSecureWM_replacer', $row->text );

   return true;
}

function botSecureWM_replacer( &$matches ) {
   global $my;

   //Check for the usertype
   if (!isset($my->usertype) or $my->usertype=="")
   {

      //Guests see a watermarked image
      header('content-type: image/jpeg');
      $src='/homepages/7/d116823254/htdocs/mwpics/'.$matches[0];
      $watermark = imagecreatefrompng('MW_WaterMark.png');
      $dest_x = 15;
      $dest_y = 80;
      $watermark_width  = imagesx($watermark);
      $watermark_height = imagesy($watermark);
      $image = imagecreatefromjpeg($src);
      $size = getimagesize($src);
      imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
      imagejpeg($image);
      imagedestroy($image);
      imagedestroy($watermark);

   } else {

      //Members see the non-watermarked image
      header('content-type: image/jpeg');
      $src='/homepages/7/d116823254/htdocs/mwpics/'.$matches[0];
      $image = imagecreatefromjpeg($src);
      $size = getimagesize($src);
      imagejpeg($image);
      imagedestroy($image);

   }

   return "";
}

The images are organized in folders outside my www folder so they can not be directly accessed via the web. They are not watermarked normally and should only be watermarked on the fly when viewed by a guest. My code is adapted from code originally used in a Smarty template, and the watermarking code worked. I had hoped only a few modifications would make it work as a mambot. My intention was to use the mambot like this:

Code: Select all

<img src="{securewm}/path/to/image.jpg{/securewm}" />

However, as is, it displays a broken image placeholder and nothing more. Any ideas?

Re: Joomla 1.0.xx mambot to insert watermarked image...help?

Posted: Wed Feb 27, 2008 12:14 am
by koltz
Just wondering how you are progressing with this mambot? I have been looking for one just like it and haven't found anything until I saw your post.

Thanks,

Corey

Re: Joomla 1.0.xx mambot to insert watermarked image...help?

Posted: Wed Feb 27, 2008 12:53 am
by Tim_Myth
I gave up on creating a mambot to do this. I ended up using the addphp and rokaccess plugins to start a session when a user is logged in. My image.php script then checks for a session variable and watermarks the image if it is not set. The content item looks something like this:

{rokaccess !guest}
{addphp src=startsession.php}
{/rockaccess}
<img src="display_image.php?id=1234" />

Start session is a simple little script that simplay starts a session and sets a variable.
Display_image is a simple image script that checks for the session variable. If !variable then merge the image and the watermark.

Re: Joomla 1.0.xx mambot to insert watermarked image...help?

Posted: Sat Mar 01, 2008 2:06 am
by koltz
Thanks for the update. Dang was really hoping for a good working bot that would do this.

Re: Joomla 1.0.xx mambot to insert watermarked image...help?

Posted: Sat Mar 01, 2008 1:56 pm
by Tim_Myth
I suppose it would be very easy to create a mambot that automated this stuff by combining the functions of addphp and rokaccess. The problem is that you need the image src to output the content-type as an image, so a simple text replacement doesn't work (becausse the page content type is text/html). So, however one went about solving this, you'd still need a seperate php script that set the content type to image/png for example. The only way to change the content type is to make an additional page, which is always going to be a valid url target (that is they will alway be able to browse to http://www.mysite.com/image.php). So, you're image.php script has to have some way to determine if it should show the watermarked image or not. Checking for a valid mos mighht work, but I don't understand the inner workings of joomla well enough to know that for sure. The best you'd be able to hope for from a mambot (which simply acts upon existing content as far as I know) would be to make a tag that a parameter like {watermark src=path/to/image}. The mambot would then checked if they are logged in (like rokaccess does) and start the session or set a cookie for logged in users (like addphp does). The image.php script would then check for that session/cookie variable and display the appropriate image which could vary by watermark and/or quality and/or dimensions. If you know a smattering of php, you should be able to figure out how to do the mambot portion just by examining the addphp and rokaccess mambots. The image.php file can be created from any of the excellent tutorials already available on the web.