#1 2014-09-06 13:26:44

CodeStepper
Member
From: Poland
Registered: 2014-06-30
Posts: 53
Website

New watermark function

According to this post i wrote function with new watermarking features.

Function (libraries/Medias.php):

	public function embed_watermark( $filepath, $watermark )
	{
		self::$ci->load->library( 'image_lib' );
		
		// directory and default watermark
		$files_directory   = DOCPATH . Settings::get('files_path') . '/';
		$default_watermark = $files_directory . 'watermark.png';
		
		// just get the watermark info from square braces
		$info = array( );
		$data = array( );
		
		preg_match_all( '/\[([^\]]+)\]/', $watermark, $info );
		
		// are there any matches?
		$count = isset($info[0])
			? count($info[0])
			: 0;
		
		// without square braces
		if( !$count )
		{
			// ex. "bl,watermark.png" in attribute
			if( strpos($watermark, ',') )
			{
				$line = explode( ',', $watermark );
				$data = array( array($line[0], $files_directory . $line[1]) );
			}
			// ex. "bl" in attribute
			else
				$data = array( array($watermark, $default_watermark) );
		}
		else
		{
			// ex. [tl,bl] or only [tl] in attribute
			if( $count == 1 )
			{
				$line = explode( ',', $info[1][0] );
				
				foreach( $line as $position )
					$data[] = array( trim($position), trim($default_watermark) );
			}
			// ex. [tl,bl][watermark.png,watermark.png] in attribute
			// you must declare in this case for each position default watermark!
			// so, construction [tl,bl][watermark.png] is not allowed
			else
			{
				$position  = explode( ',', $info[1][0] );
				$watermark = explode( ',', $info[1][1] );
				
				if( count($position) != count($watermark) )
					return;
				
				for( $x = 0, $y = count($position); $x < $y; ++$x )
					$data[] = array( trim($position[$x]), $files_directory . trim($watermark[$x]) );
			}
		}
		
		// create watermark/watermarks
		foreach( $data as $watermark )
		{
			$vertical = 'middle';
			$horizontal = 'center';
			
			// vertical / horizontal
			$v = substr( $watermark[0], 0, 1 );
			$h = substr( $watermark[0], 1, 1 );
			
			if( $v == 't' )
				$vertical = 'top';
			else if( $v == 'b' )
				$vertical = 'bottom';
				
			if( $h == 'l' )
				$horizontal = 'left';
			else if( $h == 'r' )
				$horizontal = 'right';
				
			if( !is_file($watermark[1]) )
				return;
				
			// config... nothing special
			$config = array
			(
				'wm_type' => 'overlay',
				'source_image' => $filepath,
				'quality' => 90,
				'wm_vrt_alignment' => $vertical,
				'wm_hor_alignment' => $horizontal,
				'wm_overlay_path' => $watermark[1]
			);
			
			// crate watermark
			self::$ci->image_lib->clear( );
			self::$ci->image_lib->initialize( $config );
			self::$ci->image_lib->watermark( );
		}
	}

Usage:

Like normal embed_watermark function, so:
Avaliable positions:
Vertical:
- m : middle
- t : top
- b : bottom
Horizontal:
- c : center
- l : left
- r : right

Extra features:
you can pass your custom watermark, passing:

watermark="mc,mywatermark.png"

or more watermarks with positions:

watermark="[mc,lr][mywatermark1.png,mywatermark2.png]"

Also, if you want to pass more positions for default image (still watermark.png), you must place it in square braces:

watermark="[mc,lr]"

That's my suggestion for watermark function smile

PS:
There are no info about watermarks in ionize docs.

Redgards.

Last edited by CodeStepper (2014-09-06 13:32:06)

Offline

#2 2014-10-18 10:26:06

Michel-Ange
Ionize Core Team
From: Paris, France
Registered: 2010-03-22
Posts: 927
Website

Re: New watermark function

Did you made one pull request ?


Inspiration & Innovation : Websites by Partikule, Agence Web Paris

Offline

#3 2014-10-18 22:22:14

CodeStepper
Member
From: Poland
Registered: 2014-06-30
Posts: 53
Website

Re: New watermark function

Nope. I have no idea how to do it. smile

Edit:
-----------------------------
Ok. I did it! big_smile

Last edited by CodeStepper (2014-10-18 22:37:03)

Offline

What's Ionize ?

Ionize is an Open Source Content Management System created by webdesigners for webdesigners.

Created and maintained by Partikule and Toopixel, Ionize wants to be the easiest and most powerful CMS.

Can I help ?

Because talent is nothing without involvement, we are looking for motivated coders and webdesigners to join the project team.

Resources

Website : ionizecms.com
Documentation : doc.ionizecms.com

Development : Ionize on GitHub
Translations : Ionize Translations