ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Image Moo - Image manipulation library

July 20, 2010 11:10am

Subscribe [38]
  • #1 / Jul 20, 2010 11:10am

    Mat-Moo

    350 posts

    Updated to 0.9.2, fixed a couple of minor bugs :-
    Full details and download now at
    http://www.matmoo.com/codeigniter/image_moo/

    The CI library for image manipulation is great, but I found it awkward to use when doing multiple processes. So I wrote my own library which I’m happy for you to play with and send feedback. It is limited to PHP 5 and GD2 only, sorry, but that suits my needs.
    Example :-

    $this->load->library('image_moo');
        // single thumbnail
        $this->image_moo
            ->load("myfile.x")
            ->resize(200,200)
            ->save("thumb.x");
        if ($this->image_moo->error) print $this->image_moo->display_errors();
    
        // thumbnail and large, large watermarked
        $this->image_moo
            ->load("myfile.x")
            ->resize(240,200)
            ->save("thumb.x")
            ->resize(1024,768)
            ->save("medium.x")
            ->resize(1600,1024)
            ->save("large.x");
        if ($this->image_moo->error) print $this->image_moo->display_errors();
    
        // add watermarks
        $this->image_moo
            ->load("myfile.x")
            ->load_watermark("image.x")
            ->resize(240,200)
            ->save("thumb.x")
            ->resize(1024,768)
            ->watermark(2)
            ->save("medium.x")
            ->resize(1600,1024)
            ->watermark(8)
            ->save("large.x");
        if ($this->image_moo->error) print $this->image_moo->display_errors();
    
        // create watermark text
        $this->image_moo
            ->load("myfile.x")
            ->make_watermark_text("copyright me","font.ttf")
            ->resize(1024,768)
            ->watermark(2)
            ->save("medium.x");
        if ($this->image_moo->error) print $this->image_moo->display_errors();

    Those are a couple of basic uses but it has a couple of extras for resize 😊
    resize($max_width,$max_height,$pad=FALSE)
    So resize will work as normal, e.g. an image of 1000 x 600. resize(200,200) your output image would be 200 x 120. Sometimes you want this as a square image, so set pad and the returned image will be 200 x 200 and the image centralised on to it. The background colour can be set with $this->image_moo->set_background_colour($html_colour=”#ffffff”) (default is white #ffffff.
    There is also another resize, resize_crop($x,$y) which is similar to resize but truncates the original image to get the largest part in the resize. e.g. a 1000 x 600 image resize_crop(200,200) returns a 200 x 200 image, but the source will be the centralised 600 x 600 part of the main image (does that sound right?!)

    There are no docs at the moment as this is just version 0.7.2 while I continue the project I need this for, but all feedback is welcome.
    ——————————————————————————————
    load($filename)
    Loads a master image in for manipulation
    save($filename, $overwrite=FALSE)
    Saves the altered image (original if applicable) to file, will only overwrite if $overwrite=TRUE
    save_dynamic($filename=”“)
    Send the altered image to the screen, use filename to specify how it should be written, default is jpg
    load_watermark($filename)
    Loads a file to use as a watermark
    ——————————————————————————————
    TBC

    Edit : updated to 0.8.9

  • #2 / Jul 20, 2010 11:12am

    Mat-Moo

    350 posts

    Ah yea watermark(x) work with the num pad key system, so 789 position it across the top, left to right, 456, middle and 123 at the bottom. You can also set transparency level $this->image_moo->set_watermark_transparency(50). You can also set the jpg quality for image save, $this->image_moo->set_jpeg_quality(75);

  • #3 / Jul 24, 2010 7:01pm

    Mat-Moo

    350 posts

    Updated to 0.7.2 added dynamic output 😊

  • #4 / Jul 24, 2010 7:23pm

    Laith99

    4 posts

    THANKS A LOT FOR THIS LIBRARY ... IT’S SUPERLATIVE

  • #5 / Jul 25, 2010 4:09am

    tkyy

    67 posts

    good work mat, this library is practical. i have an algorithm i created to create thumbnails while resizing the image to specific height and width dimensions (it actually cuts some of the image off, but does it in an “intelligent” way as to take a best guess at where the content of the image is). would you be interested in incorporating this into your library? let me know and i can send you the code.

    i have successfully incorporated it into several social media sites that i have worked on.

  • #6 / Jul 25, 2010 4:28am

    Mat-Moo

    350 posts

    That sounds interesting, could add it as an option to the resize_crop function (Add Tru for best guess of content location)

  • #7 / Jul 25, 2010 10:54pm

    tkyy

    67 posts

    yeah it is great for social media sites where you need the thumbnails to be the EXACT same size, like 60x60 pixels for instance so they can line up neatly next to eachother. alternatively you would usually just maintain the aspect ratio (width, usually) and it will mess up the page’s layout significantly. i can grab the code from one of my projects soon and post it for you.

    what it does is resizes it to 60x while maintain the aspect ratio of the longest dimension. after that it takes the other dimension and positions the cropped region in the center of the image. it produces thumbnails that are comparable to facebook (really, i have compared it with facebook’s automatic cropping and the images almost always crop at the same spot so i am sure they are using a similar algo).

    i’ll unzip this big archive (like 50gb, its an entire social media application i wrote with the whole database in tact, around 150 thousand users, so it will take a fair bit of time) and find the library that does it in a moment.

  • #8 / Jul 26, 2010 5:09am

    Mat-Moo

    350 posts

    Yep, my resize_crop does the same thing (exact size, largest crop and resize), except it centralises the crop location and does not work out the best position for it. Would be a great addition though!

  • #9 / Jul 26, 2010 7:32am

    tkyy

    67 posts

    damn you mat-moo, that i what mine does as well. i wonder if it would actually be possible to detect a skin-tone (like a variation of brown) and try to narrow down where the content of the image is that way with either imagemagick or the codeigniter library 😊

  • #10 / Jul 27, 2010 7:06pm

    Mat-Moo

    350 posts

    Updated to 0.9.2, fixed a couple of minor bugs :-
    Full details at
    http://www.matmoo.com/codeigniter/image_moo/
    ——————————————————————————————————————-
    Any option that requires a colour can be sent as a html value,e.g. #rrggbb or as a set of values, e.g. array(rr,gg,bb)

    border($width, $colour=”#000”)
    Draws a frame $width pixels around the output in colour #000

    border_3d($width, $rot=0, $opacity=30)
    Creates a 3d border $width pixels wide around the output. Use rot 0..3 to change the highlight and lowlight position. $opacity changes the intensity of the border

    filter($function, $arg1=NULL, $arg2=NULL, $arg3=NULL, $arg4=NULL)
    As http://php.net/manual/en/function.imagefilter.php e.g. $this->image_moo->filter(IMG_FILTER_GRAYSCALE); to convert to greyscale

    round($radius, $invert=FALSE, $corners(array(topleft, topright, bottomright, bottomleft))
    Rounds the edges of the output by X pixels. Use invert to chop them out instead of rounding them. Corners is an array of 4 TRUE / FALSE values, allowing yo to control which corners get rounded.
    ——————————————————————————————————————-
    So to create a photo effect on your image

    $this->load->library('image_moo');
        // single thumbnail
        $this->image_moo
            ->load("myfile.x")
            ->resize(200,200)
            ->border(10,"#fff")           // thick white border
            ->border(1,"#000")            // thin black border
            ->save("thumb.x");
        if ($this->image_moo->error) print $this->image_moo->display_errors();

    So these new features allow you to make your thumbs a bit more snazzy.

    Still to do :-
    Fix the watermark text when angle is used
    Add shadow facility
    Add reflection facility *maybe*

    Open to ideas of other things you might want to do in this library!

  • #11 / Jul 29, 2010 2:21pm

    gaben

    2 posts

    Hi, this is very good library!
    An idea for saving the thumbnails:
    if i want to save the thumbnail like the original file name, i just write to save() that word, like thumb_marker: _thumb, in the offical image_lib 😊

    i hope you understand, sorry for my bad englsih 😊

    if i use a transparent PNG file for watermarking, why make a white background for it? :( can you help me?

  • #12 / Jul 29, 2010 3:19pm

    Mat-Moo

    350 posts

    Can’t see how I can add that to the current save facility easily, but easy to add it as a new function

    function save_pa($prepend="", $append="", $overwrite=FALSE)
        //----------------------------------------------------------------------------------------------------------
        // Saves the temp image as the filename specified,
        // overwrite = true of false
        //----------------------------------------------------------------------------------------------------------
        {
            // validate we loaded a main image
            if (!$this->_check_image()) return $this;
    
            // get current file parts
            $parts=pathinfo($this->filename);
    
            // save
            $this->save($newfile,$parts["dirname"].'/'.$prepend.$parts['filename'].$append.$parts["extension"]);
    
            return $this;
        }

    Needs a new private var $filename=”” and in the load needs $this->filename=$filename;
    Then (untested) you should be able $this->image_moo->load(”/a/b/c/xxx.gif”)->resize(400,400)->save_pa(“x_”,“_y”,TRUE); which would save the file as “/a/b/c/x_xxx_y.gif” at least that is the theory. Will fully test and add to library

  • #13 / Aug 26, 2010 1:08pm

    Mat-Moo

    350 posts

    Updated to 0.9.2, fixed a couple of minor bugs :-
    Full details at
    http://www.matmoo.com/codeigniter/image_moo/

  • #14 / Sep 25, 2010 4:55am

    hugle

    289 posts

    Hello Mat-Moo,

    this lib is amazing 😊 thank you!

    Once it saved me, today I have problems using image watermarks using native CI lib. will see if your lib will help…

    Btw, I could like to ask, if it could be difficult to implement smth like this:

    ->set_background_colour(”#49F”) - this already done
    but I would need smth like this: ->set_background_image(“image.x”) - so the pic would be put over the image ....
    so we have nicer background 😊

    Thank you so much!

  • #15 / Sep 25, 2010 6:57am

    hugle

    289 posts

    Hello again 😊

    wanted to inform you, that line 233 needs to be changed, to:

    $this->save($parts["dirname"].'/'.$prepend.$parts['filename'].$append.'.'.$parts["extension"]);

    I’ll play with it and post pore results I come up with any problems, modifications:)

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases