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

Known 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

Be Sociable, Share!
This entry was posted by jc on Sunday, June 11th, 2006 at 9:15 pm and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

21 Comments : Leave a Reply

  1. Rui Cruz says:

    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. David says:

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

  3. jc says:

    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. Brad says:

    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. jc says:

    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. Luke says:

    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. jc says:

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

  8. Luke says:

    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. Phil McDonald says:

    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. Phil McDonald says:

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

    Thanks

  11. Bruce says:

    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. jc says:

    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. smilecho says:

    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. Razvan says:

    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. kyle says:

    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. jc says:

    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. OxyMedia says:

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

  18. OxyMedia says:

    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. jc says:

    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. 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

  21. Thanks for sharing and info.