Tuesday, September 27, 2011

Joomla K2 watermark image

We need to edit file - item.php which exists in our administrator folder, full path:
\administrator\components\com_k2\models\item.php

first we need to define the variable:
$img_url_watermark = "../images/watermark.png"; //(line 202)

near the comment line
//Original image

Then we need to paste the lines after each image comment to which we want to add watermark (i recommand not to add to small and xsmall... images )
$handle->image_watermark = $img_url_watermark;
$handle->image_watermark_position = "TL";

Than we need to add the code:
//gallery watermark
if($dir = opendir($savepath.DS.$row->id)) {
while (false !== ($file = readdir($dir))) {
$handle = new Upload ($savepath.DS.$row->id.'/'.$file);
$handle->allowed = array('image/*');
$handle->file_auto_rename = false;
$handle->file_overwrite = true;
$handle->image_resize = ture;
$handle->image_x = 1024;
$handle->image_ratio_y = true;
$handle->image_watermark = $img_url_watermark;
$handle->image_watermark_position = "TL";
$handle->image_convert = "jpg";
$handle->jpeg_quality = 85;
$handle->Process($savepath.DS.$row->id);
$handle->Clean();
}
closedir($dir);
}
//end

After the code:

if (!JArchive::extract($savepath.DS.$handle->file_dst_name, $savepath.DS.$row->id)) {
$mainframe->redirect('index.php?option=com_k2&view=items', JText::_('Gallery upload error: Cannot extract archive!'), 'error');
} else {
$row->gallery = '{gallery}'.$row->id.'{/gallery}';
}
JFile::delete($savepath.DS.$handle->file_dst_name);
$handle->Clean();

Its about 430~ line

That is all.... done :)

p.s. some sources where the solution was fount

http://api.ning.com/files/HXT6DGp5WSJFHL5RzYp-Jb*WUL4Ep5eAy*opKr8s8KN4pt-DDD92wDPKlmNCmWeQd8msVcosqVIjDLKY3jJ-pZ*6PcJz56pR/diff1.gif
http://api.ning.com/files/*txNFL3oxugapDshDybYqwZ6pM1HRBera8DL7uZp5keDIc40jfg8UZf-SEvT3aVakQ35-yRj-RYTlZXBcGxHOa0R2gQPSkPN/diff2and3.gif
http://api.ning.com/files/2ryzVIrGkDjAz*ZMCXk1aYFdbHTiKTrxNSCfUK97YXCGDhmGv7azhnkMQ1cnJp7EdCivLSbtwdV1HmnwnnjnleMveePM*1LS/diff4.gif
http://community.getk2.org/forum/topics/im-just-trying-to-add-a-png

7 comments:

  1. Thanks a ton for this incredibly helpful post. I hope the K2 developers go on and implement this functionality soon.

    ReplyDelete
  2. i am sorry i think it works well but K2 admin panel gives an error log like "Gallery upload error: Cannot extract archive!"
    where did i mistake. thanks

    ReplyDelete
  3. Hello, followed this as best I could but still nothing.
    I presume one creates a png image (watermark.png) and uploads it to the joomla directory. Does it have to be a certain size/resolution?
    In my item.php file the following lines are so with the added info. Any tips would be greatly appreciated.

    "line 303 - $img_url_watermark = "../images/watermark.png";
    //Original image

    line 313 - $handle->image_watermark = $img_url_watermark;
    $handle->image_watermark_position = "TL";

    line 318- //gallery watermark
    if($dir = opendir($savepath.DS.$row->id)) {
    while (false !== ($file = readdir($dir))) {
    $handle = new Upload ($savepath.DS.$row->id.'/'.$file);
    $handle->allowed = array('image/*');
    $handle->file_auto_rename = false;
    $handle->file_overwrite = true;
    $handle->image_resize = ture;
    $handle->image_x = 1024;
    $handle->image_ratio_y = true;
    $handle->image_watermark = $img_url_watermark;
    $handle->image_watermark_position = "TL";
    $handle->image_convert = "jpg";
    $handle->jpeg_quality = 85;
    $handle->Process($savepath.DS.$row->id);
    $handle->Clean();
    }
    closedir($dir);
    }

    Line - 443 - if (!JArchive::extract($savepath.DS.$handle->file_dst_name, $savepath.DS.$row->id)) {
    $mainframe->redirect('index.php?option=com_k2&view=items', JText::_('Gallery upload error: Cannot extract archive!'), 'error');
    } else {
    $row->gallery = '{gallery}'.$row->id.'{/gallery}';
    }
    JFile::delete($savepath.DS.$handle->file_dst_name);
    $handle->Clean();

    ReplyDelete
  4. if solution didnt helped, try another one in internet. sorry not using joomla now and dont have any possible way to test it out..

    try to check if you uploaded to the directory... as it was mentioned - "../images/".

    format should be "png"

    ReplyDelete
  5. Great Solution. I like the way you did it. Thank you

    ReplyDelete