Jun 12 2007

Source Code for GD library image function

php gd library

Hello my php friends,

I have written a class library using gb for a lot of image functions:

  • Resize
  • Round Edge
  • Rotate
  • Watermark

Click details for Souce code:

Class Thump
{
public function Resize($image,$new_width,$new_height=0)
{

$old_width = imagesx($image);
$old_height= imagesy($image);

if($new_height==0) // if the height is not specified....calculate the relative height
$new_height= $new_width * $old_height / $old_width ;

$new_image= imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

return $new_image;
}

public function RoundImage($image,$corner_radius=20,$topleft=true,$topright=true,$bottomleft=true,$bottomright=true)
{

$corner_source = imagecreatefrompng(sfConfig::get('sf_root_dir').'/web/images/rounded_corner.png');

$corner_width = imagesx($corner_source);
$corner_height = imagesy($corner_source);
$corner_resized = ImageCreateTrueColor($corner_radius, $corner_radius);
ImageCopyResampled($corner_resized, $corner_source, 0, 0, 0, 0, $corner_radius, $corner_radius, $corner_width, $corner_height);

$corner_width = imagesx($corner_resized);
$corner_height = imagesy($corner_resized);

$width = imagesx($image);
$height= imagesy($image);

$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);

// Top-left corner
if ($topleft == true) {
$dest_x = 0;
$dest_y = 0;
imagecolortransparent($corner_resized, $black);
imagecopymerge($image, $corner_resized, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}

// Bottom-left corner
if ($bottomleft == true) {
$dest_x = 0;
$dest_y = $height - $corner_height;
$rotated = imagerotate($corner_resized, 90, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}

// Bottom-right corner
if ($bottomright == true) {
$dest_x = $width - $corner_width;
$dest_y = $height - $corner_height;
$rotated = imagerotate($corner_resized, 180, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}

// Top-right corner
if ($topright == true) {
$dest_x = $width - $corner_width;
$dest_y = 0;
$rotated = imagerotate($corner_resized, 270, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
return $image;

}
public function RotateImage($image,$angle=15)
{
$white = ImageColorAllocate($image,255,255,255);
$image = imagerotate($image, $angle, $white);
return $image;
}

public function AddWaterMark($image,$logo)
{
$WM=new transparentWatermark($logo);
$WM->setStampPosition ( 400, 400);
$image = $WM->markImageFile ( $image) ;

return $image;

}
}

?>

TAGS:

2 Comments on this post

Trackbacks

  1. divakar said:

    how to install ffmpeg or ffmpeg-php in windows.I want step by step instructions. please tell me. and where to get ffmpeg source code.

    April 23rd, 2008 at 11:04 am
  2. Sajith M.R said:
    April 23rd, 2008 at 4:28 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts