Image processing class for image processing, including adding watermarks and generating thumbnails

Source: Internet
Author: User

/**
*===================================================================
* image.class.php image processing class for image processing, including adding watermarks and generating thumbnails
* March 25, 2013 22:10:38
*===================================================================
*/
Class image{
Private $thumbPrefix = ' thumb_ '; Thumbnail prefix
Private $waterPrefix = ' water_ '; Watermark Image Prefix

Picture type and function name corresponding to creating canvas resource
Private $from = Array (
' Image/gif ' = ' imagecreatefromgif ',
' Image/png ' = ' imagecreatefrompng ',
' Image/jpeg ' = ' imagecreatefromjpeg '
);

Picture type and function name corresponding to the generated picture
Private $to = Array (
' Image/gif ' = ' imagegif ',
' Image/png ' = ' imagepng ',
' Image/jpeg ' = ' imagejpeg '
);


constructor function
Public Function __construct () {

}
/**
* Add Watermark function
* @access Public
* @param $image string target picture
* @param $water string watermark Picture
* @param $postion number to add watermark location, default 9, lower right corner
* @param $path A string watermark Picture storage path, default is empty, indicating in the current directory
* @return
*/
Public function watermark ($image, $water, $postion =9, $path = ") {
Get source map and watermark picture information
$DST _info = getimagesize ($image);
$water _info = getimagesize ($water);
$DST _w = $dst _info[0];
$DST _h = $dst _info[1];
$src _w = $water _info[0];
$src _h = $water _info[1];

Gets the name of the creation function for each picture
$DST _create_fname = $this->from[$dst _info[' mime '];
$src _create_fname = $this->from[$water _info[' mime '];

Use mutable functions to create canvas resources
$DST _img = $dst _create_fname ($image);
$src _img = $src _create_fname ($water);

Watermark Location
Switch ($postion) {
Upper left
Case 1:
$DST _x = 0;
$DST _y = 0;
Break
Upper
Case 2:
$DST _x = ($dst _w-$src _w)/2;
$DST _y = 0;
Break
Top Right
Case 3:
$DST _x = $dst _w-$src _w;
$DST _y = 0;
Break
Middle Left
Case 4:
$DST _x = 0;
$DST _y = ($dst _h-$src _h)/2;
Break
In medium
Case 5:
$DST _x = ($dst _w-$src _w)/2;
$DST _y = ($dst _h-$src _h)/2;
Break
Middle Right
Case 6:
$DST _x = $dst _w-$src _w;
$DST _y = ($dst _h-$src _h)/2;
Break
Lower left
Case 7:
$DST _x = 0;
$DST _y = $dst _h-$src _h;
Break
Down in
Case 8:
$DST _x = ($dst _w-$src _w)/2;
$DST _y = $dst _h-$src _h;
Break
Down right
Case 9:
$DST _x = $dst _w-$src _w;
$DST _y = $dst _h-$src _h;
Break
Random
Case 0:
$DST _x = rand (0, $DST _w-$src _w);
$DST _y = rand (0, $DST _h-$src _h);
Break
Default
# code ...
Break
}

Add a watermark picture to the target icon
Imagecopy ($dst _img, $src _img, $dst _x, $dst _y, 0, 0, $src _w, $src _h);

Create a picture with a watermark
$waterfile = $path. $this->waterprefix.basename ($image);
$generate _fname = $this->to[$dst _info[' mime '];

if ($generate _fname ($dst _img, $waterfile)) {
return $waterfile;
} else {
return false;
}


}

/**
* Generate thumbnails, equal scale, with filler effect
* @access Public
* @param $image A string target picture,
* @param $max _width number thumbnail maximum width
* @param $max _height number thumbnail maximum height
* @return successfully returned the thumbnail name, failed to return false
*/
Public function thumbnail ($image, $max _width, $max _height, $path = ") {
Get picture information
$info = getimagesize ($image);
$SRC _width = $info [0];
$SRC _height = $info [1];
Echo $src _width, $src _height;
Get the thumbnail size by calculating the scale
if ($src _width/$max _width > $src _height/$max _height) {
# This should be the width
$DST _width = $max _width;
$DST _height = ($max _width/$src _width) * $SRC _height;
} else {
# At this time should be high as the subject
$DST _height = $max _height;
$DST _width = ($max _height/$src _height) * $SRC _width;
}
Creating source graph resources using mutable functions
$src _create_fname = $this->from[$info [' MIME '];
$src _img = $src _create_fname ($image);
Create a thumbnail resource with a size of $max_width x $max _height;
$DST _img = Imagecreatetruecolor ($max _width, $max _height);
Fill white background
Imagefill ($dst _img, 0, 0, imagecolorallocate ($dst _img, 255, 255, 255));
Calculates the position of the thumbnail on the canvas to ensure that the picture is centered when the scale is unequal
$DST _x = ($max _width-$dst _width)/2;
$DST _y = ($max _height-$dst _height)/2;
The thumbnail will be resampled proportionally to adjust its position
Imagecopyresampled ($dst _img, $src _img, $dst _x, $dst _y, 0, 0, $dst _width, $dst _height, $src _width, $src _height);
$thumbfile = $this->thumbprefix. PathInfo ($image, pathinfo_basename);

$generate _fname = $this->to[$info [' MIME '];
if ($generate _fname ($dst _img, $path. $thumbfile)) {
# successful return thumbnail name, note the name returned, different places upload scheme will have a different path
Return date (' Ymd '). ‘/‘ . $thumbfile;
} else {
# Failed to return false
return false;
}
}
}

Invoke instance
$img = new Image;
$img->thumbnail (' a.jpg ', 200,200, ' d:/amp/www/shopcz/');
$img->watermark (' a.jpg ', ' sina.png ', 9, ' d:/amp/www/shopcz/');

Image processing class for image processing, including adding watermarks and generating thumbnails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.