Jump to content

having trouble with watermarking an image


iNko

Recommended Posts

Hello again, i need to do a form that uploads an image, makes a thumbnail of it and puts a watermark image and text  on the thumbnail..

Iv managed to do the uploading part, making the thumbnail, then making a watermarked thumbnail (with image) , and i need some help with the watermark text part..

 

What im thinking of - generate a text image, then apply it to the watermark, then add the watermark with text to the thumbnail.

 

This is the function i used for the watermark image:

<?php
function watermark($original_image,$original_watermark,$destination="")
	{
		$image=imagecreatefromjpeg($original_image);
		list($imagewidth,$imageheight)=getimagesize($original_image);

		$watermark 	= 	imagecreatefrompng($original_watermark); 			
		list($watermarkwidth,$watermarkheight)=getimagesize($original_watermark);

		if($watermarkwidth>$imagewidth || $watermarkheight>$imageheight)
		{
			$water_resize_factor = $imagewidth / $watermarkwidth;
			$new_watermarkwidth  = $watermarkwidth * $water_resize_factor;
			$new_watermarkheight = $watermarkheight * $water_resize_factor;

			$new_watermark = imagecreatetruecolor($new_watermarkwidth , $new_watermarkheight);

			imagealphablending($new_watermark , false);
			imagecopyresampled($new_watermark , $watermark, 0, 0, 0, 0, $new_watermarkwidth, $new_watermarkheight, $watermarkwidth, $watermarkheight);

			$watermarkwidth  = $new_watermarkwidth; 
			$watermarkheight = $new_watermarkheight; 
			$watermark       = $new_watermark;
		}
		$startwidth 	= 	($imagewidth 	- 	$watermarkwidth)  / 2; 
		$startheight 	= 	($imageheight 	- 	$watermarkheight) / 2;

		imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); 
		if(!empty($destination))
			imagejpeg($image,$destination);
		else 
			imagejpeg($image);
	}	
?>

 

Can i insert in this function something like this? (the "Text" would appear on top of the watermark image in the middle) :

<?php
// Create the canvas
$canvas = imagecreate( 150, 80 );
// First colour - this will be the default colour for the canvas 
// $light_blue = imagecolorallocate( $canvas, 176, 226, 255 );
// The second colour - to be used for the text
$black = imagecolorallocate( $canvas, 0, 0, 0 );
// Path to the font you are going to use
$font = "verdana.ttf";
// Text to write
$text = "Text";
// Font size
$size = "40";
// Add the text to the canvas
imageTTFText( $canvas, $size, 0, 15, 60, $black, $font, $text );
// Save as Text.jpg
imagejpeg( $canvas, "Text.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $canvas );
?>

 

Thx in advance.

Link to comment
Share on other sites

i tried but it just shows some weird symbols on my browser, ignores the text part completely, and just uploads with image watermark only

 

this is where i tried to write the code, idk if this is where i need to write smth, or i need to create a new function or smth..

           $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $thumbnailheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor ($nw, $nh);

				////////////THE TEXT WATERMARK PART/////////////////////
				$textim = imagecreatetruecolor ($nw, $nh); //creates image
				//$white = imagecolorallocate($textim, 255, 255, 255);
				//$grey = imagecolorallocate($textim, 128, 128, 128);
				$black = imagecolorallocate($textim, 0, 0, 0); //creates black color
				$start_x = $nh / 2; //possition where the text will be placed (in this case middle of thumbnail)
				$start_y = $nw / 2; 
				//imagefilledrectangle($textim, 0, 0, 399, 29, $white);
				//$text = 'Testing';
				//$font = 'Dunkin.ttf';
				//imagettftext($textim, 20, 0, 11, 21, $grey, $font, $text);
				imagettftext($textim, 5, 0, $start_x, $start_y, $black, 'Dunkin.ttf', "text"); //adds the text, with size 5, possition middle of thumbnail, black color, font 'dunkin', and text "text"
				//imagettftext($textim, 20, 0, 10, 20, $black, $font, $text);
				imagepng($textim);  //?????
				imagedestroy($textim);  //?????
				///////////////////////////////////////////			


                    imagecopyresampled ($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh) ;
                    $thumbfilename = $thumbnailfolder.$uploadfilename ;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.