SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot abstraktmedia's Avatar
    Join Date
    Feb 2004
    Location
    Ljubljana
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding a watermark to uploading image..

    Hi there...

    I was reading the PHP Anthology 1 and I found "How to add a watermark to an image" part....

    I have users uploading pictures and would like to know how can I add a watermark to uploading image....meaning before the pic is actually saved to server to add watermark to image....is this possible or do I have to go around...meaning...upload..add watermark...save..delete the source image....

    Any help would be appriciated!

    Thanx in advance ...
    exit(0);

  2. #2
    SitePoint Zealot prashidi's Avatar
    Join Date
    Mar 2001
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are some links I found using google and query: php watermark image.

    http://www.google.com/search?sourcei...atermark+image

    http://devbox.cb.fxsonet.com/view.php?a=7
    http://www.oztheory.com/scripts/1

    It seems pretty simple.

  3. #3
    SitePoint Zealot abstraktmedia's Avatar
    Join Date
    Feb 2004
    Location
    Ljubljana
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx...

    I already have the code for adding watermark to an image...I was asking about doing that to uploading images...is there a way to do it "on the fly" between uploading and saving to server or do I have to go around(see my first post)

    ....
    exit(0);

  4. #4
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it can be done. When PHP receives a file upload, it stores in in the web server's "tmp" directory and makes the name of the file available via the $_FILES array under $_FILES['imagefile']['tmp_name'].

    You can apply the tranformation directly to that file. The variable $_FILES['imagefile']['type'] will contain the content type (jpeg and png will be possible).

    If you look at the Web Page Elements chapter - in example 9.php, it shows how to upload an image using QuickForm, in case you want to use it, while example 6.php in the Images chapter shows how to add a "watermark".

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice one Harry.

    Don't have your book(s) yet

  6. #6
    SitePoint Zealot abstraktmedia's Avatar
    Join Date
    Feb 2004
    Location
    Ljubljana
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I wanted to hear and from a person I wanted to hear that from

    Thanx Harry...I got both books and they are very handy I must say...I wasn't expecting that the 70% of the advices (especially 2 book) will be for more advanced programmers wich is very very refreshing...

    The only parts which I (want say didn't like because I liked everything) couldn't use so much are the ones where you use classes for creating tables and HTML....

    I always use Smarty so I have HTML and PHP code far apart becuse I have a very lasy designer who doesn't want to look at the PHP )

    overall these are the best books about PHP I have bought (and over the years I've bought many..)
    exit(0);

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...lasy designer who doesn't want to look at the PHP
    Fire the designer.

  8. #8
    SitePoint Zealot abstraktmedia's Avatar
    Join Date
    Feb 2004
    Location
    Ljubljana
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice one....

    He's not that bad....for now....
    exit(0);

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only parts which I (want say didn't like because I liked everything) couldn't use so much are the ones where you use classes for creating tables and HTML....

    I always use Smarty so I have HTML and PHP code far apart becuse I have a very lasy designer who doesn't want to look at the PHP )
    Understand that point. One thing I didn't want to get into was too much advocacy of particular approach to building PHP apps so using a class to generate a table is presented as a choice - in some instances (perhaps where a designer is not involved) it may be quicker to build an app this way. Or perhaps you define a custom function in Smarty that allows the look and feel of the table to be modified ?

    overall these are the best books about PHP I have bought (and over the years I've bought many..)
    Many thanks. That means alot.

  10. #10
    SitePoint Zealot abstraktmedia's Avatar
    Join Date
    Feb 2004
    Location
    Ljubljana
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome Harry...

    I just want to go back again to my problem...I tried this...

    I made addWatermark($picture, $watermark) function from the code in the book and at the end just did the return imagejpeg($image)...

    I'm also using fileUpload class which has the following syntax:

    PHP Code:
    if ($uploader->upload($upload_file_name))
             
    $uploader->save_file($path$mode); 
    where $uploader_file_name is the name of input=file in form field....

    the $_FILE array is stored in the $uploader->file[] array...
    So I'm trying to pass the $uploader->file['tmp_name'] as $picture argument to addWatermark function (and logo ofcourse) but the return value is the image itself and I don't know what do I have to do to change the data of $uploader->file['tmp_name'] to the adWatermark return value before doing move_uploaded_file()....

    any pointers would be great.....

    thanx...
    exit(0);

  11. #11
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It could be you need to return by reference. Try;

    PHP Code:
    function & addWatermark($picture$watermark) {
        
    // Stuff here

    Notice the & before the func name.

    Otherwise best post the fileUpload class and the addWatermark function.

  12. #12
    SitePoint Zealot abstraktmedia's Avatar
    Join Date
    Feb 2004
    Location
    Ljubljana
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I removed the class and the function from here because I solved it...

    There is one little function in PHP which I overlooked

    Thanx for everything
    Last edited by abstraktmedia; Mar 10, 2004 at 10:01.
    exit(0);

  13. #13
    SitePoint Member nickthibert's Avatar
    Join Date
    May 2004
    Location
    Ottawa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    where would i incoporate this function?

    ive been wanting to do this as well, and im wondering where i would incoporate the watermarking in my code, i treid a few different options, but it didnt save the files at all and came up with several errors. here's my code for my upload page:

    PHP Code:
    <?PHP
     
    include("config.inc.php");
     
    // initialization
     
    $result_final "";
     
    $counter 0;
     
    // List of our known photo types
     
    $known_photo_types = array( 
          
    'image/pjpeg' => 'jpg',
          
    'image/jpeg' => 'jpg',
          
    'image/gif' => 'gif',
          
    'image/bmp' => 'bmp',
          
    'image/x-png' => 'png'
         
    );
     
     
    // GD Function List
     
    $gd_function_suffix = array( 
          
    'image/pjpeg' => 'JPEG',
          
    'image/jpeg' => 'JPEG',
          
    'image/gif' => 'GIF',
          
    'image/bmp' => 'WBMP',
          
    'image/x-png' => 'PNG'
         
    );
     
    // Fetch the photo array sent by preupload.php
     
    $photos_uploaded $_FILES['photo_filename'];
     
    // Fetch the photo caption array
     
    $photo_caption $_POST['photo_caption'];
     while( 
    $counter <= count($photos_uploaded) )
     {
      if(
    $photos_uploaded['size'][$counter] > 0.00001)
      {
       if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
       {
        
    $result_final .= "File ".($counter+1)." is not a photo<br />";
       }
       else
       {
        
    mysql_query"INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );
        
    $new_id mysql_insert_id();
        
    $filetype $photos_uploaded['type'][$counter];
        
    $extention $known_photo_types[$filetype];
        
    $filename $new_id.".".$extention;
        
    mysql_query"UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );
        
    // Store the orignal file
        
        
    copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);
        
    // Let's get the Thumbnail size
        
    $size GetImageSize$images_dir."/".$filename );
        if(
    $size[0] > $size[1])
        {
         
    $thumbnail_width 100;
         
    $thumbnail_height = (int)(100 $size[1] / $size[0]);
        }
        else
        {
         
    $thumbnail_width = (int)(100 $size[0] / $size[1]);
         
    $thumbnail_height 100;
        }
       
        
    // Build Thumbnail with GD 1.x.x, you can use the other described methods too
        
    $function_suffix $gd_function_suffix[$filetype];
        
    $function_to_read "ImageCreateFrom".$function_suffix;
        
    $function_to_write "Image".$function_suffix;
        
    // Read the source file
        
    $source_handle $function_to_read $images_dir."/".$filename ); 
        
        if(
    $source_handle)
        {
         
    // Let's create an blank image for the thumbnail
              
    $destination_handle ImageCreate $thumbnail_width$thumbnail_height );
        
         
    // Now we resize it
              
    ImageCopyResized$destination_handle$source_handle0000$thumbnail_width$thumbnail_height$size[0], $size[1] );
        }
        
    // Let's save the thumbnail
        
    $function_to_write$destination_handle$images_dir."/tb_".$filename );
        
    ImageDestroy($destination_handle );
        
    //
        
    $result_final .= "<img src='".$images_dir"/tb_".$filename."' /> <br> File Added<br />";
       }
      }
     
    $counter++;
     }
     
    // Print Result
    echo <<<__HTML_END
    <html>
    <head>
    </head>
    <body>
    <table width='348' height='300' border='0' align='center' valign='middle'>
    <tr align='center'>
    <td>
     
    $result_final
     </td>
     </tr>
     </table>
    </body>
    </html>
    __HTML_END;
    ?>
    and im trying to figure out how to incoporate this code:

    PHP Code:
    [color=black]<?php 

    header
    ("Content-type: image/jpeg"); 

    $insertfile imageCreateFromPNG('watermark.png'); 
    $insertfile_width imagesx($insertfile); 
    $insertfile_height imagesy($insertfile); 
    $newimage imagecreateTrueColor($insertfile_width$insertfile_height); 
    $newimage ImageCreateFromJpeg($_GET['src']); 
    $size getimagesize($_GET['src']); 
    $dest_x $size[0] - $insertfile_width 5
    $dest_y $size[1] - $insertfile_height 5
    imageCopyMerge($newimage$insertfile,$dest_x,$dest_y,0,0,$insertfile_width,$insertfile_height,100); 
    imagejpeg($newimage); 
    imagedestroy($newimage); 
    imagedestroy($insertfile); 

    ?>[/color]
    [color=black][/color]


    into it... any ideas, or will i have to make some kind of outside function that is called upon? any help is great!

  14. #14
    SitePoint Member nickthibert's Avatar
    Join Date
    May 2004
    Location
    Ottawa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im new to this kind of stuff, so help would be greatly appreciated


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •