Sunday, 30 October 2011

Magic Square

My next post is about Magic Square :-

What is Magic Square?

The magic square is matrix of n*n elements in which the addition of any row elements, column elements or diagonal elements will produce the same result.

There are many ways to generate the Magic Square, and the logic i used is explained below :-

There are 3 arrays.

1st array contains the number written in array, for example if we have to generate the 3*3 square , then array 1 contain these elements :-

1     2     3
4     5     6
7     8     9

Array 2nd is written in such a fashion that the middle column occupy the elements in order and its adjacent rows are incremented/decremented order.

3     1     2
1     2     3
2     3     1

Array 3rd is exactly the image of array 2nd, so the array 3rd is

2     1     3
3     2     1
1     3     2

Now with the help of these 3 arrays, we are able to create our magic square.

Magic Square [row][column] = Array 1 [array 2 [row][column]][array 2 [row][column]

 Magic Square [1][1] = Array1[3][2]

because, array2 [1][1] = 3, and array3 [1][1] = 2

So, in this manner we are able to calculate all the results. :)

Thats it. The code will be in my next post.


Saturday, 8 October 2011

The Starting of Journey

Well, i am starting this blog from the first entry i created at IVAN's My friend blog related to Image WaterMarking using PHP to protect your images from others.

So here is my first code

example below to make image watermarking using php:

//// By aksshay sharma - to create watermark images (runtime) using php
// Telling browser that the content is of image type
header('content-type: image/jpeg');
// Setting our watermark image (company logo)
$watermark_image = imagecreatefrompng('watermark_logo.png');
// Calculating Dimension of our watermark
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
$your_image = imagecreatetruecolor($watermark_width, $watermark_height);
//Set the image on which watermark is to be made
$your_image = imagecreatefromjpeg("my_image.jpg");
$size = getimagesize("my_image.jpg");
$final_x = $size[0] - $watermark_width - 5;
$final_y = $size[1] - $watermark_height - 5;
imagecopymerge($your_image, $watermark_image, $final_x, $final_y, 0, 0, $watermark_width, $watermark_height, 100);
// Generating image having watermark
// Destroying temporary images

Example for watermark image with text using PHP:

//Using imagecopymerge() to create a translucent watermark
// Load the image on which watermark is to be applied
$original_image = imagecreatefromjpeg('pic.jpeg');

// First we create our watermark image manually from GD
$watermark = imagecreatetruecolor(100, 70);
$original_image = imagecreatefromjpeg('pic.jpeg');
//Set the hex color code for your watermark and dimension
imagestring($watermark, 5, 20, 20, 'Aksshay', 0xFFFFF);
imagestring($watermark, 3, 20, 40, 'Sharma', 0xFFFFF);

// Set the margins for the watermark and get the height/width of the watermark image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($watermark);
$sy = imagesy($watermark);

// Merge the watermark onto our photo with an opacity (transparency) of 50%
imagecopymerge($original_image, $watermark, imagesx($original_image) - $sx - $marge_right, imagesy($original_image) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark), 50);

// Save the image to file and free memory
imagepng($original_image, 'watermark_image.png');

The first code will watermark an image with another image, so it’s like to stamp the other image with your digital sign. And the second example, it will stamp your image with your text. Basically the text will converted to image then stamp to the original image.

To use the code don’t forget to enable php gd library in your php.ini:


Note: Your watermark image is of 8-bit PNG. To make your image 8-bit PNG you can use imagemagik.

Copy the complete code from here -

Complete Code