TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   image upload script with watermark (http://www.talkphp.com/absolute-beginners/5952-image-upload-script-watermark.html)

hitchy 08-12-2011 08:58 AM

image upload script with watermark
 
hello... first off let me just say im sorry if this is in the wrong place i found it hard to pick weather its beginner or more advanced, PLEASE move this if it is in the wrong place.

ok with that out of the way, i have been editing a php login script for a while now and have created/pieced together an image uploader which works fine, i am now trying to add a watermark script to my uploader so it saves the images to my server with a watermark already on them.


here is the uploader script taken from inside a process page.
PHP Code:

   function upload($subtitle){
      global 
$database$form//The database, form and mailer object
      
    
$random $this->generateRandID();
    
$size $_FILES['uploaded']['size'];
    
$type $_FILES['uploaded']['type'];
    
$title $_POST['title'];    
    
$reference $random ."-"$_FILES['uploaded']['name'];
    
    
/*DATE start*/
    
$time date (DATE_RFC822);
    
$tz$this->userinfo['country'];
    
$dtzone = new DateTimeZone($tz);
    
$dtime= new DateTime ($time,$dtzone);
    
$date=$dtime->format ('U');

        
/*DATE end*/




    
      /* Name error checking */
      
$field "title";
      if(!
$subtitle || strlen($subtitle trim($subtitle)) == 0){
         
$form->setError($field"* Name not entered");
      } else {
         
$subtitle stripslashes($subtitle);
      }
      
      
        
$field "uploaded";
$allowed_types = array('image/gif','image/pjpeg','image/jpeg','image/PJPEG','image/JPEG','image/jpg','image/JPG','image/x-png');

if(!
in_array($type$allowed_types))
 { 
 
$form->setError($field"invalid file type"); 
 } else {
 if (
$size 307200)
{
$form->setError($field"Your file is to big, needs top be smaller"); 
 } }


      

      
      
      
/* Errors exist, have user correct them */
      
if($form->num_errors 0){
         return 
1;  //Errors with form
      
}
      
/* No errors, add the new account to the */
      
else{
      
$target "user_images";
    if(!
is_dir($target)) @mkdir($target);
    
$target $target ."/"$_SESSION['username'];
    if(!
is_dir($target)) @mkdir($target);
    
$target $target ."/"'pics';    
    if(!
is_dir($target)) @mkdir($target);
    
$target $target."/"$random ."-".basename($_FILES['uploaded']['name']) ;

$target2 "user_images";
    
$target2 $target2 ."/"$_SESSION['username'];
        
$target2 $target2 '/pics';
        
$target2 $target2 '/thumbs';
        if(!
is_dir($target2)) @mkdir($target2);
        
$target2 $target2."/"$random ."-".basename($_FILES['uploaded']['name']) ;
        

 function 
createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (
preg_match("/JPG|JPEG|jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (
preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}
if (
preg_match("/gif|GIF/",$system[1])){$src_img=imagecreatefromgif($name);}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if (
$old_x $old_y
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if (
$old_x $old_y
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if (
$old_x == $old_y
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

if     (
preg_match("/png/",$system[1])){imagepng($dst_img,$filename);} 
if    (
preg_match("/gif/",$system[1])){imagegif($dst_img,$filename);} 
else     {
imagejpeg($dst_img,$filename);}
    
    
// Clear the memory of the temporary image 
    
imagedestroy($dst_img);
    
imagedestroy($src_img);  
}
 
//Writes the photo to the server 
 

 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))  { 
  }
$target2 "user_images";
    
$target2 $target2 ."/"$_SESSION['username'];
        
$target2 $target2 '/pics';
        
$target2 $target2 '/thumbs';
        if(!
is_dir($target2)) @mkdir($target2);
        
$target2 $target2."/".basename($random ."-".$_FILES['uploaded']['name']) ;
        
createthumb($target,$target2,200,200);
           
      
         if(
$database->addUpload($subuser$size$subtitle$type$reference$date)){
            
            return 
0;  //New user added succesfully
         
}else{
            return 
2;  //Registration attempt failed
         
}
      } 
   } 

the script checks the title field has something in and checks the type and size of the image ($uploaded), then it creates the folder system i want and uploads the image as well as creating a thumbnail and saving that to its own folder.

all the tutorials i have seen for adding a watermark to an image either add the watermark as the image is viewed (not saving the watermark on the image) or they work in a way that i cant get to work with my script above.

i have a watermark script working
PHP Code:

$image_uploaded $reference;
$image_uploaded_temp $_FILES['uploaded']['tmp_name'];\

move_uploaded_file ($image_uploaded_temp"images/$image_uploaded");

function 
watermarkImage($location)
{
$watermark imagecreatefrompng("watermark.png");
$watermark_height imagesy($watermark);
$watermark_width imagesx($watermark);
$image imagecreatetruecolor($watermark_width$watermark_height);
$image imagecreatefromjpeg($location);
$size getimagesize($location);
$x_pos $size[0] - $watermark_width 10;
$y_pos $size[1] - $watermark_height 10;
imagecopymerge($image$watermark$x_pos$y_pos00$watermark_width$watermark_height100);
imagejpeg($image$location);
imagedestroy($image);
imagedestroy($watermark);
}
watermarkImage("images/$image_uploaded"); 

but i am unable to combine the 2 scripts to get it to work :'-(

please is there anyone that can help?
cheers

p.s i know my script isnt the easiest to follow and its most likely not the best way to do it either but thats ok for now.


All times are GMT. The time now is 11:29 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0