SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Watermark using semi-transparent text ?

    Anyone know of any tutorials for watermarking images using semi-transparent text ? Or even a class someone else for done that does it using GD or IM.

    If not, got any ideas of where to start ? i'm really not sure, the only image manipulation i've done is resizing in GD... And if i do have to do it myself from scratch... is it better to do with GD or IM ?

    I've been searching around looking for information about this for abit now
    I'd be really thankful for any help/advice ! : )

  2. #2
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could start from reading this article here at SitePoint.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, i've read it before though.

    What i'm confused about is dynamically putting text onto an image saying "*username* @ *website*.com", making the text semi-transparent and how to put the text at an angle.

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Some ImageMagick examples here: http://www.rubblewebs.co.uk/imagemagick/watermark.php click the image for the code.
    I am not sure how semi-transparent text works with GD.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -_DarkRanger_- View Post
    Thanks, i've read it before though.

    What i'm confused about is dynamically putting text onto an image saying "*username* @ *website*.com", making the text semi-transparent and how to put the text at an angle.
    1. Use imagerotate() to rotate the image.
    2. Use the last parameter of the function imagecopymerge() described in that article. If it is less than 100, it will give you semi-transparency for true color images.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    So to add a transparent text using GD you would first create your text on a background - can you make a transparent background or do you need to change a colour to transparent later ? , rotate the file then save this as a tempory file, merge the two images, delete the tempory image.

    I may have a play with this later.

    One thing is Imagemagick makes a much simpler job of this; specialy positioning the watermark. Although once you have written your code for GD if you add some way of calculating the position etc. you should be able to use the code over again.
    A lot of hosts do not use ImageMagick which can also have an effect on what you can use !

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    So this code sort of works but I am off to bed now !
    PHP Code:
    <?php
    $canvas 
    imagecreate200100 );

    $black imagecolorallocate$canvas00);
    $white imagecolorallocate$canvas255255255 );

    imagefilledrectangle$canvas9918989$white );

    $font "verdana.ttf";
    $text "Title";
    $size "30";
    $degrees "30";
    $photo"round_penion.jpg";

    $box imagettfbbox$size0$font$text );
    $x = (200 - ($box[2] - $box[0])) / 2;
    $y = (100 - ($box[1] - $box[7])) / 2;
    $y -= $box[7];

    imageTTFText$canvas$size0$x$y$black$font$text );

    imagecolortransparent $canvas$white );

    imagerotate$canvas$degrees0);

    imagepng$canvas"temp.png" );

    ImageDestroy$canvas );

    $watermark imagecreatefrompng('temp.png');  
    $watermark_width imagesx($watermark);  
    $watermark_height imagesy($watermark);  
    $image imagecreatetruecolor($watermark_width$watermark_height);  
    $image imagecreatefromjpeg($photo);  
    $size getimagesize($photo);  
    $dest_x $size[0] - $watermark_width 5;  
    $dest_y $size[1] - $watermark_height 5;  
    imagecopymerge($image$watermark$dest_x$dest_y00$watermark_width$watermark_height50);  
    imagejpeg($image'output.jpg');  
    imagedestroy($image);  
    imagedestroy($watermark);  

    unlink'temp.png');
    ?>

    <img src="output.jpg">
    Problems:
    1/ Rotate does not work - need to save another tempory image then rotate that ?
    2/ Need to modify the calculations on where to put the watermak on the image.
    3/ My text has a black box around the text as I just used another example bit of code for that to save time
    4/ The text has a "shadow" of the original background colour around the text, is there a way of improving this ?

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Imagemagic has really made it look pretty simple.
    I hope someone finishes it off in GD too. I need the one in GD coz my thumbnails are generated by GD.

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks guys !

    I tried the GD script and it was great ! i messed around with it abit. One thing i'm not sure on though is how to center text ? Because i put a line break (\n) in there and it's all left aligned.

    Tried the IM script as well, didn't get it working though Had a look around the website with examples, really interesting ! Much simplier than all the code needed for GD as well

    I really wanna see if i can get the IM thing working, because it's much simplier + i've heard alot of good things about IM and it's about time i learned how to do it

    I'm not even sure if IM is installed though. I've look for it in phpinfo() and can't find any mention of it.
    I didn't install PHP or apache on this computer, zends IDE program did. I dunno if it installed IM with it, how do i find out ?

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    I think IM is a lot simpler and gives better results; my example codes should have worked and I suspect IM is not installed. It does not show up on my phpinfo either - its an "external" program not like GD which is installed with php.

    Try either of these to see if its installed, it should display the info but may ask you to download a file.
    PHP Code:
    <?php 
      header
    ("Content-Type: text/plain"); 
      
    system("exec 2>&1; type convert");
    // Output: convert is /usr/local/bin/convert   
      
    system("exec 2>&1; convert -version"); 
    // Output: Version: ImageMagick 6.2.8 06/10/06 Q16http://www.imagemagick.org
    // Copyright: Copyright (C) 1999-2006 ImageMagick Studio LLC
    PHP Code:
    <?php
    exec
    (\'which convert\',$path);
    print_r($path); 
    ?>
    I have not installed IM myself on a server but installed it easily on a fedora 6 PC. I understand it can either be a straight forward install on a server or a pain in the ***

  11. #11
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did them, and it outputted nothing, so i guess it's not installed

    I just have to install it on my computer, i'll get the host to install it on the server when i get it

  12. #12
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure which to install, so i've just gone for the ImageMagick-6.3.3-10-Q16-windows-dll.exe one...

    Should i have gone for Q8 instead of Q16 ?

  13. #13
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    I think Q8 is 8bit and Q16 is 16bit ? anyway its better quality.

    I tried installing it on windows and did not know what I was doing and it didn't work. That is one reason I have got hold of an old PC and installed Linux on it.

    Here is the offical forum http://redux.imagemagick.org/discourse-server/index.php

  14. #14
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i installed it ! and it all went fine :d

    I tried the script again, and still nothing appeared... so i checked it, and it was because of the exec('which convert\',$path); with the \ escaping the '... I didn't notice before

    I took it out and got this "'exec' is not recognized as an internal or external command, operable program or batch file. 'exec' is not recognized as an internal or external command, operable program or batch file. Array ( )"

    :S

    So yer...

    And i know IM is installed properly because i tried on windows command prompt.

  15. #15
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    I think Q8 is 8bit and Q16 is 16bit ? anyway its better quality.

    I tried installing it on windows and did not know what I was doing and it didn't work. That is one reason I have got hold of an old PC and installed Linux on it.

    Here is the offical forum http://redux.imagemagick.org/discourse-server/index.php
    I wanna put linux on my old desktop in my room, but i havn't got a clue where to start !

    Or which version of linux to use... =/

    I think i may try putting something on it soon.

  16. #16
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    I put fedora 6 on as I was given some disc for that, I tried downloading them but the ISO image for disc 1 was always corrupted. I am told fedora is no longer supported. There are 6 disc's and I only needed disc 1 and 2 after spending hours downloading them !

    I was recomended CentOS by 2 people but didn't want to spend hours downloading that as well; I will get a friend to do it as he has unlimited download and 10Mb at certain times of the day.

    Try a simple resize and see what happens; I belive you will have to give the full path to the image at a guess something like c:image magick/image name.jpg

    So anyway try a resize without the exec part:
    PHP Code:
    convert path_to_original.jpg -thumbnail 100x100 path_to_save.jpg 
    I do not know where you put this as in Linux I use the terminal !

  17. #17
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Right according to the ImageMagick website putting convert -version
    in gives you the version and confirms all is installed OK.

    This works for me but nothing else will

    I have worked with Dos but that was years ago; I assume I need to get back from c:\Documents and settings \ User> to somewhere else but do not know how as whatever I try I get path not found etc.

  18. #18
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did that in window command prompt and it worked ^____^

    But it won't do it in php by system or exec

    :S

  19. #19
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys.

  20. #20
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    After searching Google I finaly did it as well using the comand prompt

    So on your PC you are running a server as well ?

    In that case I think ImageMagick needs compiling when installing php; I am lead to belive you can edit the php.ini file in some way to let php know the path to the imagemagick dll file but I have no idea how to do that. Again I did try that but I have my xaamp setup on a partition H and the ImageMagick dll on C and it all got to confusing so I just modify images etc. online.

  21. #21
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Right depending on what you are running look in php.ini under windows extensions there should be a extension=php_imagick.dll or similar comented out. Uncoment it restart apache then run this code in your browser
    PHP Code:
    <?php
    system
    convert -version );
    ?>
    This should prove IM is installed and running. This is where it my success ends; I will have to look at it again tomorrow as I can not get any further again !

    P.S.
    This should work:
    PHP Code:
    <?php
     system
    ("convert input.jpg -thumbnail 100x100 output.jpg");
      
    ?>
    That was hard work but I now have ImageMagick running on Linux, Windows XP AND XAMMP !!!!!!!
    Last edited by Rubble; May 2, 2007 at 13:44. Reason: Added P.S.

  22. #22
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I looked in my php.ini and couldn't find anything like that so i've added extension=php_imagick.dll.

    Gonna restart my computer now (i don't know how to restart apache without restarting my computer

  23. #23
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    I think XAMMP is basicly a compiled version of php, apache etc. and the php.ini file is posted with the code. You may need to find the dll file for ImageMagick on your PC and use the path to that instead. Again something like c:\program files\imagemagick\imagick.dll <- ONLY MADE UP AS I HAVE NO IDEA AT THE MOMENT WHAT IT SHOULD BE

    Anyway its time for bed, let me know how you get on.

  24. #24
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, it's me...

    Sorry for taking so long to reply !

    This is my other account that i created ages ago, was going to completely go over to and delete my other one (-_DarkRanger_-) but there was a problem, and this account wouldn't validate for some reason and i couldn't post, so i stuck with the other account.

    But i forgot the password for the other account (it's not the normal one i use) and it's saved as an auto password thing on my main desk top... But for some reason the internet has completely cut out on that (even though it's connected directly to the router, and all the other machines can get on the internet and they're through the same router but wirelessly... no idea what's wrong... the internet just stopped working on that computer after i restarted the computer to restart apache !) so i can't get on the other account at all =/ 'cause that account uses an old unused email as well

    So i had to contact sitepoint admins and get this account validated, did it on friday night though, so they've only just responded !

    Ah well !

    Anyway, i did manage to combine both GD and IM to (nearly) get what i want ! : D
    One problem i had was with spaces in the file path... i didn't realise for ages you could just quote it with "s and it'd be fine... 'Cause when i tried to quote a path in command line it hadn't worked, i later realised that was because when i pressed ' it put a ` (or something similar) instead, and that didn't work.
    I was stressed trying to figure out why and was kinda annoyed when it turned out to be something so small !

    There is one thing i still need help on though, and that's centering the text in gd.
    I was thinking that if there isn't a function to do it. I may have to compare the length of the two lines and give the smallest one spaces on the left to compensate for it ?

    But anyway, i'll try that tomorrow (well, today technically, seeing as it's 0:58am) when i'm back on the desktop (can't get on it atm because it's in the living room and the alarms on - taking the alarm off would wake my mum up and leave for one very p***ed off parent =/)

    Sorry about the post being so long !


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
  •