Activate your free membership today | Log-in

Thursday, June 4th, 2009

Image thumbnail crop tool with Processing.js and PHP GD

Category: Examples

Matt Schoen has written a nice image thumbnail tool that lets you zoom in on the part of the image that you want to crop for a thumbnail.

You can check out the Processing code that does the work:

JAVASCRIPT:
  1.  
  2. void setup(){
  3.     size(600, 600);     //for internal size variables
  4.     frameRate(30);      //set our draw rate
  5.     stroke(0);    //all lines will be black
  6.     color c = color(128,128,128,128);
  7.     fill(c);        //all filled rects will be 50% gray with 50% transparency
  8. }
  9.  
  10. int xoffset = 0;        //temp variable while we're dragging the mouse
  11. int yoffset = 0;
  12. int xpos = 0;      //where to draw the image
  13. int ypos = 0;
  14. int startx, starty;
  15. int prsd = 0;
  16. PImage a;  // Declare variable "a" of type PImage 
  17. a = loadImage("img.jpg"); // Load the images into the program
  18. double s = 1.0;
  19. double r = 1.0;
  20.  
  21. void draw(){    //this method is called by processingjs at the above frame rate
  22.         background(0)//draw a black background
  23.                 //for testing purposes, to figure out the scaling
  24.         scale(s);
  25.         image(a, xpos - xoffset, ypos - yoffset);       //draw scaled image
  26.         scale(1/s);     
  27.         //for whatever reason, using "scale(1.0);" didn't reset the scale here.  I have no idea why...
  28.        
  29.         //draw ghosting frame
  30.         noStroke();     //we don't want borders on these rects
  31.         rect(0,0,600,100);
  32.         rect(0,500,600,100);
  33.         rect(0,100,100,400);
  34.         rect(500,100,100,400);
  35.         line(0,560,600,560);
  36.        
  37.         //draw the "button" regions
  38.         line(300,560,300,600);
  39.         line(570, 0, 570, 30);
  40.         line(570, 30, 600, 30);
  41. }
  42. void mousePressed(){
  43.         if(mouseY> 560){
  44.                 if(mouseX> 300){
  45.                         s += .1;
  46.                 }
  47.                 else{
  48.                          s -= .1;
  49.                 }
  50.         }
  51.         if(mouseY <30 && mouseX> 570)
  52.                 link("http://www.pagefoundry.net/matt/jquery/renderthumb.php?x=" + xpos + "&y=" + ypos + "&s=" + s);
  53.         prsd = 1;
  54.         startx = mouseX;
  55.         starty = mouseY;
  56. }
  57. void mouseDragged(){
  58.         if(prsd> 0){
  59.         xoffset = startx - mouseX;
  60.         yoffset = starty - mouseY;
  61.         }
  62. }
  63. void mouseReleased(){
  64.         prsd = 0;
  65.         xpos -= xoffset;
  66.         ypos -= yoffset;
  67.         xoffset = 0;
  68.         yoffset = 0;   
  69. }
  70.  

Posted by Dion Almaer at 7:08 am
9 Comments

++---
2.5 rating from 33 votes

9 Comments »

Comments feed TrackBack URI

nice, but is a simple cropper
maybe the new stuff is using the processing library?
in safari is not working….

What about this: Kroppr ?

PS. yes is mine, and yes is paid.

Comment by rborn — June 4, 2009

sorry, here is the link again
Kroppr

Comment by rborn — June 4, 2009

It`s not work in Opera

Comment by morman — June 4, 2009

kroppr is nice, at least it’s a pleasure to look at the cute koala, instead of the bloody eye.

Comment by syang — June 4, 2009

oops, it’s not a koala, but at least it’s a cute animal, :), instead of a haunting bloody eye, :)

Comment by syang — June 4, 2009

Kroppr is quite good. I was very surprised when it rotated. Image Thumbnail Tool on the other hand…. thumbs down. I don’t see the need for using canvas – I’ve built thumbnail croppers with just JS/CSS.

Comment by AnM8tR — June 4, 2009

This is kinda cool for the fact it uses processing.js but I found this other one [I am in no way affiliated with defusion.org.uk]
http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/ and think it deserves a look – couldn’t find it in ajaxian history. At version 1.2.0 (last updated Oct 2006) it’s still very feature rich.
Defusion is trying to raise money to cover time to fix bugs and add features for v2.0.

Comment by agrath — June 4, 2009

About the browser compatibility — I used Firefox while I was writing the tool and honestly made no attempt at cross-browser support (maybe I should have called it v0.5 haha). The processing.js website mentioned what is essentially an IE patch, and I’m not sure about what is going on with Opera or (as reported by a friend) Chrome. I’m working on a bunch of other projects but when I get to updating this I’ll look into the compatibility issues (also if anyone out there knows exactly what’s wrong and how to fix it feel free to shoot me an e-mail: matt@pagefoundry.net)

Comment by mschoen — June 5, 2009

Cross browser jQuery plugin for image cropping: Crop Images with jCrop: Cross-Browser jQuery Pluging for Image Cropping

Comment by bloggingdev — November 10, 2009

Leave a comment

You must be logged in to post a comment.