PHP Class: Image Snapshot 1.3

Inspired by Backpackit.com’s image management, Image Snapshot can take a portion of an image (crop) to specific dimensions, from different areas of the original image.

This class has been created for the purpose of manipulating an uploaded image, and creating a Thumbnail image that does not alter the aspect ratio, and yet represents the contents of the original image. I’m sure other uses can be found for the Image Snapshot class, but I’ll leave that to you. I use it for Photo Galleries, and Shopping Cart systems.

View the manual to get a clearer picture.

This class supports:

  • Jpg, Gif, Png files
  • Output to Jpg
  • Can resize images before applying the crop
  • Can crop from any part of the image

Sites using this class:

Change Log
05-12-2005 1.3 Release
05-12-2005 Added: ImageFile field, can now load image from saved file.
05-12-2005 Added: custom position type, specify by percentages. Read updated manual for help with this.
28-11-2005 1.2 Release
28-11-2005 Added: ReturnedWidth and ReturnedHeight variables, for more feedback.
28-11-2005 Fixed: if crop area was bigger than original image, mixed results ocurred.
28-11-2005 1.1 Release
28-11-2005 Added: support for Image input with a variable
27-11-2005 1.0 Release

Downloads

Technorati: php, images, gd


Share this Post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Slashdot
  1. written by Rui Cruz June 23rd, 2006 at 01:34 | #1

    Hi, I’d like to say that I love this class and I use it all the time, there’s just one thing missing.

    You should add the option to resize by pixels instead of just by percentage.


  2. written by David August 30th, 2006 at 17:34 | #2

    Hello! Just wanted to tell you that the “Download Manual (highly reccomended) (Pdf file)” doesn’t work :(


  3. written by jc August 31st, 2006 at 09:16 | #3

    Right you are, David. I’ve just been transfered to a new server, and it wasn’t just this file that had ‘gone missing’. Everything is peachy now tho, and thanks for pointing it out :) Let me know how you go


  4. written by Brad September 8th, 2006 at 03:39 | #4

    Great image tool and cool pic in pdf manual. I need to dynamicly move the crop box around in real time. Any way you can think that could be done with this class? The user would have to be able to see the orginal image with the box overlay like in the pdf manual. Any advise would be great.


  5. written by jc September 8th, 2006 at 09:01 | #5

    I too would like to achieve that. I think the only way to make it happen would be to use Javscript or Flash in conjunction with the class. Would be very neat though!


  6. written by Luke January 22nd, 2007 at 08:45 | #6

    hello sir
    Thanks for your work – very interesting.
    I have been tinkering with your script “IMAGE SNAPSHOT CLASS” as i have been trying allsorts in the hope of achieving the following:
    To silently submit the size of an image (as displayed in the users browser) to retrieve its width in pixels. For example if a 1px x 1px red gif image is set to 100% width for display in a browser, to transmit back the final viewing width to php for further processing. The users allowable window width may be something like 602px max and so the Gif would be stretched to 602px ?? How can that info be received to be processed further and without the use of javascript. I was hoping your snapshot script would take an image as displayed on-screen. I am not interested at all in the displaying of the final image as it is for retrieving a measurement only to apply to further processing.
    Hope you can help with this. Thanks again for your work – just taking a good look at your sliding boxes.


  7. written by jc January 22nd, 2007 at 08:47 | #7

    Ok, I’m having trouble understanding the question. Do you want the dimensions of the image Before the class manipulates it, or After?


  8. written by Luke January 22nd, 2007 at 10:42 | #8

    I am trying to get users avavilable screen width without javascript but I realize now that this is not going to be possible without javascript.


  9. written by Phil McDonald August 31st, 2007 at 03:48 | #9

    Hi.

    Love the class. I’m trying to integrate it into the MG2 photo gallery. I can get it to create a thumbnail, no problem, but MG2 supports multiple uploads and when I try to upload 2 or more images at the same time, I get an error:

    Fatal error: Cannot redeclare class imagesnapshot in /usr/local/psa/home/vhosts/digitalexpression.ca/httpdocs/clients/funskate/gallery3/includes/snapshot.class.php on line 86

    Can you point out how to get around this? Any help would be very appreciated.


  10. written by Phil McDonald August 31st, 2007 at 04:51 | #10

    Never mind. Easy answer if I’d been thinking. Change my include to require_once.

    Thanks


  11. written by Bruce October 13th, 2008 at 06:59 | #11

    Hi!

    Loving this class. However, i cannot figure out how to just resize the image without cropping. is it possible? Trying to make it 100px by 100px, but it crops.


  12. written by jc October 13th, 2008 at 09:52 | #12

    That is outside the scope of this class. I’ve made a separate class to handle mere image size conversions and not cropping.

    http://blog.jc21.com/files/photo_resize_class.zip
    PHP4 and PHP5 versions. This class has no documentation, you’ll have to figure out the class methods yourself :) enjoy


  13. written by smilecho November 19th, 2008 at 23:48 | #13

    Hello, great class. Thanks very match. First of all, excuse me for my bad English, but I have a question. When I use .gif file with transparency, the output is .jpg with black background. Can it be changed background color, or it is depend on my GD?


  14. written by Razvan November 23rd, 2008 at 03:38 | #14

    First of all I’d like to thank you for this nice class. Nice job !

    Second, I’d like to tell you what I’ve changed in order to work nicer with pictures which have only one dimension bigger than the desired result picture (only width or height, not both of them), maybe this is something who interests others too:

    1. At line 247, I’ve changed the “AND” comparison, with an “OR”:

    if ($image_width > $this->Width || $image_height > $this->Height)

    2. I’ve changed the maxwidth / maxheight calculation in the if at line 254:

    if ($w_ratio < $h_ratio) {
    $maxheight = ceil($image_height * $w_ratio); $maxwidth = ceil($image_width * $w_ratio);
    } else {
    $maxwidth = ceil($image_width * $h_ratio);
    $maxheight = ceil($image_height * $h_ratio);
    }


  15. written by kyle March 19th, 2009 at 02:38 | #15

    I am running this class in the Ajax File Browser on my Linux Server with apache2 and php5 and it works great! However, I’m also running on 2000 server in IIS and php5, and I don’t get any thumbnails in the file info pane or in thumbnail view. What am I missing?


  16. written by jc April 1st, 2009 at 14:00 | #16

    Using the GD library on a Windows distribution is sometimes buggy. Images sometimes come out with green bars and jibberish, or sometimes not at all.

    I would analyse any log files and if there’s no problem there, update your PHP GD DLL.


  17. written by OxyMedia October 1st, 2009 at 02:32 | #17

    Very bad, nothing works, I can’t put a simple image url working :(


  18. written by OxyMedia October 1st, 2009 at 02:33 | #18

    What’s wrong here? :(

    include(‘snapshot.class.php’);

    $myimage = new ImageSnapshot;
    $myimage->ImageContents = ‘test.jpg’;
    $myimage->Width = 307;
    $myimage->Height = 161;
    $myimage->Resize = true; //if false, snapshot takes a portion from the unsized image.
    $myimage->ResizeScale = 100;
    $myimage->Position = ‘center’;
    $myimage->Compression = 80;

    if ($myimage->ProcessImage() !== false) {
    $img = $myimage->GetImageContents();
    } else {
    echo $myimage->Err;
    }

    //saving to a filename
    if ($myimage->SaveImageAs(‘temp.jpg’)) {
    echo ‘ReturnedWidth . ‘px;height:’ . $myimage->ReturnedHeight . ‘px;border:1px solid #f00;”>ReturnedWidth . ‘” height=”‘ . $myimage->ReturnedHeight . ‘” alt=”" />’;
    } else {
    echo $myimage->Err;
    }


  19. written by jc October 1st, 2009 at 08:58 | #19

    There are a number of problems with the use of the class in your example. The main one, is that you’re trying to tell the class that the contents of the image file is “test.jpg” – when in fact you mean to say that the contents is file_get_contents(‘test.jpg’).

    However, this 1.3 class is now superseded by a PHP 5.2+ version, Image Snapshot v2.0. Although it works in the same way, it works with less code and more readability. The class methods have changed slightly, but should be self explanatory.

    You can download it here:
    blog.jc21.com/wp-content/code/image_snapshot_v2.0.zip


  20. written by Jørgen February 9th, 2010 at 22:01 | #20

    Hi!

    Thank you for this wonderful class! I just upgraded to version 2 but cant get the example work. I put the two files, and an image called test.jpg, in the same folder, and I changed the saveImage-location to the same folder. But the only thing that appears is :
    “Problem converting image! Make sure you are Loading it.
    Cannot Save File: save.jpg NO DATA IN OUTPUTCould not save Image!
    Output Width: 800px
    Output Height: 600px ”

    Suggestions?

    Thanks
    Jørgen