Keyword

Watermark to K2 Item Image

  • felipe
  • felipe's Avatar Topic Author
  • Offline
  • New Member
More
9 years 11 months ago #132872 by felipe
Watermark to K2 Item Image was created by felipe
Hi, im tryiing to add a watermark to the k2 main image, somebody knows how to do this?

I was looking at forum and i found a post with a working code but with the image gallery. (SIGP)

[url=http://http://getk2.org/community/English-K2-Community/142770-Im-just-trying-to-add-a-png-watermark-on-image-upload?limit=20&start=20][/url]

I tried to make it work with the k2 main item image but I could not make it work.

Here is the code for the image gallery:

In '/administrator/components/com_k2/models/item.php' right after these lines:
                if (!JArchive::extract($savepath.DS.$handle->file_dst_name, $savepath.DS.$row->id))
                {
                    $mainframe->redirect('index.php?option=com_k2&view=items', JText::_('K2_GALLERY_UPLOAD_ERROR_CANNOT_EXTRACT_ARCHIVE'), 'error');
                }
                else
                {
                    $row->gallery = '{gallery}'.$row->id.'{/gallery}';
                }
                JFile::delete($savepath.DS.$handle->file_dst_name);
                $handle->Clean();

insert these:
//gallery watermark
            if ($dir = opendir($savepath.DS.$row->id)) {
                mkdir ($savepath.DS.$row->id.'tmp', 0777);
                $logfile = fopen ('/home/user/public_html/tmp/php-gd-test1.log','w');
                while (false !== ($file = readdir($dir))) {
                    if (is_file($savepath.DS.$row->id.'/'.$file)) {
                    // Load the image where the logo will be embeded into
                        $image = imagecreatefromjpeg($savepath.DS.$row->id.'/'.$file);
                        fwrite ($logfile, 'file: '.$savepath.DS.$row->id.'/'.$file.PHP_EOL);
                        if (!$image) fwrite ($logfile, 'image creation failed'.PHP_EOL);
                    // Load the logo image
                        $logoImage = imagecreatefrompng("/home/user/public_html/images/watermark.png");
                        if (!$logoImage) fwrite ($logfile, 'watermark creation failed'.PHP_EOL);
                        $is_ok = imagealphablending($logoImage, true);
                        if (!$is_ok) fwrite ($logfile, 'setting alpha blending failed'.PHP_EOL);
                    // Get dimensions
                        $imageWidth=imagesx($image);
                        $imageHeight=imagesy($image);
                        $logoWidth=imagesx($logoImage);
                        if ($logoWidth > $imageWidth) $logoWidth = $imageWidth;
                        $logoHeight=imagesy($logoImage);     
                        if ($logoHeight > $imageHeight) $logoHeight = $imageHeight;
                    // Paste the logo
                        $is_ok = imagecopy($image, $logoImage, 0, 0, 0, 0, $logoWidth, $logoHeight);
                        if (!$is_ok) fwrite ($logfile, 'image copying failed'.PHP_EOL);
                    // Set type of image and send the output
                        $is_ok = imagejpeg($image, $savepath.DS.$row->id.'tmp/'.$file);
                        if (!$is_ok) fwrite ($logfile, 'image saving failed'.PHP_EOL);
                    // Release memory
                        imagedestroy($image);
                        imagedestroy($logoImage);
                    }
                }
                fclose($logfile);
                closedir($dir);
                JFolder::delete($savepath.DS.$row->id);
                JFolder::copy($savepath.DS.$row->id.'tmp',$savepath.DS.$row->id);
                JFolder::delete($savepath.DS.$row->id.'tmp');
            }
//gallery watermark end


I appreciate any help, thanks.

Please Log in or Create an account to join the conversation.

More
9 years 11 months ago #132873 by Lefteris
Replied by Lefteris on topic Re: Watermark to K2 Item Image
Hi. Hacking K2 is not a good idea since you will lose your changes in future updates. I strongly suggest that you move your code to a K2 or content plugin. Regarding the actual code, the class that K2 uses for generating thumbnails has already support for watermarks. There is no need to write all this code. You can add a watermark when the image is resized by using the following code:
$handle->image_watermark = 'PATH_TO_THE_WATERMARK_FILE';
Note that you need to pass the absolute filesystem path to the watermark file. For example if your watermark is located at the images folder and is named "watermark.png" then you should use it like:
$handle->image_watermark = JPATH_SITE.'/images/watermark.png';
The class has several options regarding the watermark. You can find them at www.verot.net/php_class_upload_docs.htm .

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum