If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Image Cropping/Trimming and Resizing Tool (PHP & GD Library)

This function, while taking the correct parameters, while clip designated areas(edges) of an image. It will call createthumb() which uses an images true aspect ratio to properly size the image to fit in the new dimensions of ($new_w x $new_h). If createthumb() fails to create a new image, it will return the source of a default image [user specified].

SAVE THIS FILE AS: _thumb.php

PHP Code:

<?php

//********************************************* [START] ********************************************///** TITLE: IMAGE CROPPING* VERSION: 1.2* AUTHOR: David Russell* COPYRIGHT: Cypher101 Development (c) 2011* CREATED: Thursday, March 10, 2011 12:33 AM EST (UTC -5) (America/Montreal)* * **IMPORTANT** * This script is open to the public. If being used please keep the shown copyright information for* publisher and owner development credit. Any modifications below can be made.** ---------------------------------------------------------------------------------------------------* SUMMARY* ---------------------------------------------------------------------------------------------------** This function, while taking the correct parameters, while clip designated areas(edges) of an image.* It will call createthumb() which uses an images true aspect ratio to properly size the image to fit * in the new dimensions of ($new_w x $new_h). If createthumb() fails to create a new image, it will * return the source of a default image [user specified].** **IMPORTANT** * Change the resource of the default image and the output source of newly generated default image* in createthumb(). This resized source will be returned as a string value if createthumb() fails to * generate an image.** ---------------------------------------------------------------------------------------------------* PARAMETERS: cropthumb( resource $_file, output_resource $_newFile, int $new_w, int $new_h, * int $clip_x, int $clip_y, int $clip_x2, int $clip_y2 )* ---------------------------------------------------------------------------------------------------* * 1) [resource $_file]: Existing image source.* Example: /images/thumbs/image.png* * 2) [output_resource $_newFile]: Output image source.* Example: /images/thumbs/sized/newimage.png** 3) [int $new_w]: Maximum width of new image. ** 4) [int $new_h]: Maximum height of new image.** 5) [int $clip_x]: Positive 'x' value. The amount of pixels being clipped from left of the image.* * 6) [int $clip_y]: Positive 'y' value. The amount of pixels being clipped from top of the image.* * 7) [int $clip_x2]: Positive 'x' value. The amount of pixels being clipped from right of the image.* * 8) [int $clip_y2]: Positive 'y' value. The amount of pixels being clipped from bottom of the image.* * ---------------------------------------------------------------------------------------------------* RETURN VALUE* ---------------------------------------------------------------------------------------------------** Returns a string value - the source of the new image.** ---------------------------------------------------------------------------------------------------* OTHER VARIABLES* ---------------------------------------------------------------------------------------------------** [string $_PUB_DIR] {global scope variable}: Reference to a local scope variable defined as the Home* or Base directory of the domain.* Example Format: /home/user/public_html * Note: Either define $_PUB_DIR outside of function if using global or as a local variable * within the function.** [string $HOME] {global scope variable}: Reference to a local scope variable defined as the Home or* Base directory of the domain.* Example Format: "../../.." or "http://thesite.com"* Note: Either define $HOME outside of function if using global or as a local variable within * the function.** ---------------------------------------------------------------------------------------------------* OTHER FUNCTIONS* ---------------------------------------------------------------------------------------------------* * [string_return createthumb()]:** SUMMARY: This functions is a stand-alone function and can be used separate from cropthumb() if * only resizing an image. It will use an images true aspect ratio to properly size the image to * fit in the new dimensions of ($new_w x $new_h). If it fails to create a new image, it will * return the source of a default image [user specified].** PARAMETERS: createthumb( resource $_file, output_resource $_newFile, int $new_w, int $new_h )* * 1) [resource $_file]: Existing image source.* Example: /images/thumbs/image.png* * 2) [output_resource $_newFile]: Output image source.* Example: /images/thumbs/sized/newimage.png** 3) [int $new_w]: Maximum width of new image. ** 4) [int $new_h]: Maximum height of new image.* * RETURN VALUE: Returns a string value - the source of the new image.* ** [bool_return thumbFldr()]:** SUMMARY: If the output directory for the new image does not exist, this function will create all * directories leading up to the final directory. Setting permissions as 0755.** PARAMETERS: thumbFldr( output_resource $_newFile )* * 1) [output_resource $_newFile]: Output image source.* Example: /images/thumbs/sized/newimage.png* * RETURN VALUE: Bool value.** ---------------------------------------------------------------------------------------------------* EXAMPLE USAGE* ---------------------------------------------------------------------------------------------------* * [string_return cropthumb()]:** $_imgsource = cropthumb("/images/thumbs/mypic.jpg","/images/thumbs/sized/mynewpic.jpg",500,400,105,* 40,15,5);* echo "<img src='$_imgsource' border='0' id='thumbnail_1' title='Newly Cropped Thumbnail'/>";** [string_return createthumb()]:** $_imgsource = createthumb("/images/thumbs/mypic.jpg","/images/thumbs/sized/mynewpic.jpg",500,400);* echo "<img src='$_imgsource' border='0' id='thumbnail_1' title='Newly Cropped Thumbnail'/>";**///******************************************** [END] ***********************************************//

$crop->_defaultImg = "/img/noimage.png"; //== Default Image ==// $crop->_file = "/img/background/try2.jpg"; //== Image being cropped ==//$crop->_newFile = "/img/background/sized/try5.jpg"; //== Output directory & name or newly cropped image ==//$crop->new_w = 1920; //== Maximum dimensions(Width) of new image (Image will fit in this dimension maintaining its ratio) ==//$crop->new_h = 416; //== Maximum dimensions(Height) of new image (Image will fit in this dimension maintaining its ratio) ==//$crop->clip_x = 50; //== How far to clip from left of the image ==// $crop->clip_y = 100; //== How far to clip from top of the image ==// $crop->clip_x2 = 254; //== How far to clip from right of the image ==// $crop->clip_y2 = 50; //== How far to clip from bottom of the image ==//