Just to keep things simple, let’s start by creating our image watermarking class. This class, called “watermarking”, should be created inside a file named “api.watermark.php”. Go ahead and create that file now, and insert the following lines of code: <?php class watermark{ # given two images, return a blended watermarked image function create_watermark() { } # average two colors given an alpha function _get_ave_color() { } # return closest pallette-color match for RGB values function _get_image_color() { } } # END watermark API ?> So far we’ve just created a shell into which we can insert our code. Let’s begin by taking a look at our “create_watermark” function. Go ahead and replace our placeholder “create_watermark” function with the following code: # given two images, return a blended watermarked image function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) { $alpha_level /= 100; # convert 0-100 (%) alpha to decimal # calculate our images dimensions $main_img_obj_w = imagesx( $main_img_obj ); $main_img_obj_h = imagesy( $main_img_obj ); $watermark_img_obj_w = imagesx( $watermark_img_obj ); $watermark_img_obj_h = imagesy( $watermark_img_obj ); # determine center position coordinates $main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) ); $main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) ); $main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) ); $main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) ); # create new image to hold merged changes $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h ); # walk through main image # ADD CODE HERE # return the resulting, watermarked image for display return $return_img; } # END create_watermark() The first thing we did, as you may have noticed, was expand our function to receive 3 incoming parameters: $main_img_obj # plain image which our script should watermark $watermark_img_obj # watermark image, can be alpha-transparent $alpha_level # watermark alpha value, (0-100, default = 100) (It is important to note at this time that our function is expecting image objects as incoming parameters, not simply paths to an image – but we will discuss this in greater detail shortly.) Next we gathered some simple width and height information about each of our incoming images. We then used that information to determine X and Y coordinates for center aligning our watermark on top of our main image. Next our function creates a new, true color image object with the same dimensions as our main image object. This new image, “return_img”, will be used to store the merged image information from our other two images. Our next major step is to walk through the images and merge them together, but we aren’t quite ready for that yet. Instead we’ve simply inserted a placeholder comment, “ADD CODE HERE”, until we are ready to return and complete that block of code. Finally we simply return our modified image for display by the page that’s calling it. So far what we’ve done has been pretty basic. Before we move on to the core of our application though, let’s take a look at one more thing: our helper functions.
blog comments powered by Disqus |
|
|
|
|
|
|
|