Ajax Updates |
- Image Watermark on Uploaded Images
- Javascript Wait While Loading Page Display Image
- Mod Rewrite for Subdomains
- Ajax Count Words in Forms Script
| Image Watermark on Uploaded Images Posted: 26 Dec 2009 01:20 PM PST This tutorial shows you how you can add a watermark image over images that are uploaded though a form. The following is a html code as simple as it can be with an image type input and a Submit button: <form name="submit_image" method="post" enctype="multipart/form-data" action=""> <input type="file" name="image"/> <input name="Submit" type="submit" value="Upload Picture"/> </form> This is the function that applies the watermark over the uploaded picture and uploads the picture on the server. It considers that the location where the images are to be uploaded is a folder named “images”: function copy_marked($img_name,$newname,$type) { // check image type. The image type is sent as parameter 3 ($type). Note that for every image type a different function is used to create an image in memory from the uploaded file. if(!strcmp("image/jpg",$type) || !strcmp("image/jpeg",$type) || !strcmp("image/pjpeg",$type)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("image/png",$type)) $src_img=imagecreatefrompng($img_name); if(!strcmp("image/gif",$type)) $src_img=imagecreatefromgif($img_name); // get image size, we'll need it later $old_x=imageSX($src_img); $old_y=imageSY($src_img); // create destination image $dst_img=ImageCreateTrueColor($old_x,$old_y); imagecopyresampled($dst_img,$src_img,0,0,0,0,$old_x,$old_y,$old_x,$old_y); // on this demo, the watermark image will be named watermark.gif and will be located in images folder. If you intend to use other file, you will have to change this $watermark_file='images/watermark.gif'; // you can setup the transparency used for watermark image. $transparency=50; // just in case you don't know what extension the watermark file has. This function is listed lower $wext=getFileExtension('images/watermark.gif'); // create an image from watermark file if(!strcmp("jpg",$wext) || !strcmp("jpeg",$wext)) $watermark=imagecreatefromjpeg($watermark_file); if(!strcmp("png",$wext)) $watermark=imagecreatefrompng($watermark_file); if(!strcmp("gif",$wext)) $watermark=imagecreatefromgif($watermark_file); // get watermark width $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); // place watermark image on the right left of the main image $dest_x = $old_x - $watermark_width - 5; $dest_y = $old_y - $watermark_height - 5; // uncomment these lines and comment the ones above if you want to place the watermark in the very center of the image // $dest_x = (($thumb_w - $watermark_width)/2); // $dest_y = (($thumb_h - $watermark_height)/2); // merge the two images imagecopymerge($dst_img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $transparency); // copy the new created image to the destination file, in the images folder if(!strcmp("image/png",$type)) imagepng($dst_img,$newname); else if(!strcmp("image/gif",$type)) imagegif($dst_img,$newname); else imagejpeg($dst_img,$newname); // delete the images from memory imagedestroy($dst_img); imagedestroy($src_img); } Function getFileExtension(), needed to get the file extension: function getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } Having all these functions, here is how you verify if the form was submitted and call the copy_marked() function to upload the file and place the watermark: if($_SERVER['REQUEST_METHOD']=='POST') { if ($_FILES['image']['name']) { $image=$_FILES['image']['name']; $newname="images/".$image; copy_marked($_FILES['image']['tmp_name'],$newname,$_FILES['image']['type']); } } ![]() Related Listings:
|
| Javascript Wait While Loading Page Display Image Posted: 26 Dec 2009 01:00 PM PST If you have a page that takes long time to display it is a good idea to display a “wait until the page loads” image. This tutorial show you how to implement this in your webpage. To implement this you will need to: 1. Every time your page loads a “init()” function will load. <body onLoad="init()"> 2. Define a div named “loading” right after < body > section. <div id="loading" style="position:absolute; width:100%; text-align:center; top:300px;"> <img src="loading.gif" border=0/></div> The loading.gif image should be an animated gif that suggests that the page is still loading. 3. Place this javascript code right after you define the div. <script> var ld=(document.all); var ns4=document.layers; var ns6=document.getElementById&&!document.all; var ie4=document.all; if (ns4) ld=document.loading; else if (ns6) ld=document.getElementById("loading").style; else if (ie4) ld=document.all.loading.style; function init() { if(ns4){ld.visibility="hidden";} else if (ns6||ie4) ld.display="none"; } </script> |


0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.