PHP class implementation code for processing Pictures _php Tutorial

Source: Internet
Author: User
Tags imagejpeg transparent watermark
Copy CodeThe code is as follows:
/**
* Author:yagas
* email:yagas60@21cn.com
*/
Class Image
{
/** class Protection Variable */
protected $th _width = 100;
protected $th _height = 50;
protected $quality = 85; Picture quality
protected $transparent = 50; Watermark Transparency
protected $background = "255,255,255"; Background color
/**
* Generate thumbnail files
* @param $src Original document
* @param $dst target file
*/
Public function thumb ($src, $dst =null, $output =true)
{
$thumb = Array ($this->th_width, $this->th_height);
$this->scale ($SRC, $thumb, $DST, $output);
}
/**
* Zoom to Picture by percentage
* @param string $src original file
* @param string $DST the target file entered
* @param float/array $zoom scaling, floating-point type when a percentage bloom, array type when scaled by a specified size
* @param boolean $output whether to generate file output
*/
Public Function Scale ($SRC, $dst =null, $zoom =1, $output =true)
{
if (!file_exists ($SRC)) Die (' File not exists. ');
if (! $zoom) Die (' The Zoom undefine. ');
$src _im = $this->im ($SRC);
$old _width = imagesx ($src _im);
if (Is_float ($zoom)) {
Scale by percentage
$new _width = $old _width * $zoom;
}
ElseIf (Is_array ($zoom)) {
Clear Scaling Dimensions
$new _width = $zoom [0];
}
Whether to define the height of the zoom
if (!isset ($zoom [1])) {
Equal scale Scaling
$resize _im = $this->imageresize ($src _im, $new _width);
}
else {
Non-proportional scaling
$resize _im = $this->imageresize ($src _im, $new _width, $zoom [1]);
}
if (! $output) {
Header ("Content-type:image/jpeg");
Imagejpeg ($resize _im, NULL, $this->quality);
}
else {
$new _file = Empty ($DST)? $SRC: $DST;
Imagejpeg ($resize _im, $new _file, $this->quality);
}
Imagedestroy ($im);
Imagedestroy ($NIM);
}
/**
* Cut the picture
* @param $src Original file
* @param $dst target file
* @param $output whether to generate the target file
*/
Public function Capture ($SRC, $dst =null, $output =true) {
if (!file_exists ($SRC)) Die (' File not exists. ');
$width = $this->th_width;
$height = $this->th_height;
$src _im = $this->im ($SRC);
$old _width = imagesx ($src _im);
$old _height = Imagesy ($src _im);
$capture = Imagecreatetruecolor ($width, $height);
$rgb = Explode (",", $this->background);
$white = Imagecolorallocate ($capture, $rgb [0], $rgb [1], $rgb [2]);
Imagefill ($capture, 0, 0, $white);
Zoom when the picture is larger than the thumbnail
if ($old _width > $width && $old _height> $height) {
$resize _im = $this->imageresize ($src _im, $width);
When the picture scale is not normalized, the scale is recalculated for cropping
if (Imagesy ($resize _im) < $height) {
$proportion = $old _height/$this->th_height;
$resize _im = $this->imageresize ($src _im, $old _width/$proportion);
}
$POSX = 0;
$posy = 0;
}
else {
Center the picture when the picture is smaller than the thumbnail image
$POSX = ($width-$old _width)/2;
$posy = ($height-$old _height)/2;
$resize _im = $src _im;
}
Imagecopy ($capture, $resize _im, $posx, $posy, 0, 0, imagesx ($resize _im), Imagesy ($resize _im));
if (! $output) {
Header ("Content-type:image/jpeg");
imagejpeg ($capture, NULL, $this->quality);
}
else {
$new _file = Empty ($DST)? $SRC: $DST;
Imagejpeg ($capture, $new _file, $this->quality);
}
Imagedestroy ($src _im);
@imagedestroy ($resize _im);
Imagedestroy ($capture);
}
/**
* Write watermark Image
* @param $src need to write a picture of the watermark
* @param $mark watermark Picture
* @param $transparent Watermark Transparency
*/
Public function mark ($SRC, $mark, $dst = ", $output =true)
{
$mark _info = getimagesize ($mark);
$src _info = getimagesize ($SRC);
List ($MW, $mh) = $mark _info;
List ($SW, $sh) = $src _info;
$px = $SW-$MW;
$py = $sh-$mh;
$im = $this->im ($SRC);
$mim = $this->im ($mark);
Imagecopymerge ($im, $mim, $px, $py, 0, 0, $MW, $MH, $this->transparent);
if ($output) {
$new _file = Empty ($DST)? $SRC: $DST;
Imagejpeg ($im, $new _file, $this->quality);
}
Else
{
Header (' Content-type:image/jpeg ');
Imagejpeg ($im);
}
Imagedestroy ($im);
Imagedestroy ($mim);
}
/**
* Get different GD objects through the file
*/
protected function IM ($file)
{
if (!file_exists ($file)) Die (' File not exists. ');
$info = getimagesize ($file);
Switch ($info [' MIME '])
{
Case ' image/gif ':
$mim = Imagecreatefromgif ($file);
Break
Case ' image/png ':
$mim = Imagecreatefrompng ($file);
Imagealphablending ($mim, false);
Imagesavealpha ($mim, true);
Break
Case ' Image/jpeg ':
$mim = Imagecreatefromjpeg ($file);
Break
Default
Die (' File format errors. ');
}
return $mim;
}
/**
* Processing of images for zooming
* @param resource $SRC _im image GD Object
* @param the width of an integer $width picture
* @param the height of an integer $height picture, and if you do not set the height, scale the picture proportionally
* @return Resuorce $im return a GD object
*/
protected function Imageresize ($src _im, $width, $height =null) {
$old _width = imagesx ($src _im);
$old _height = Imagesy ($src _im);
$proportion = $old _width/$old _height;
$new _width = $width;
$new _height = Is_null ($height)? Round ($new _width/$proportion): $height;
Create a new image and fill in the default background color
$im = Imagecreatetruecolor ($new _width, $new _height);
$rgb = Explode (",", $this->background);
$white = Imagecolorallocate ($im, $rgb [0], $rgb [1], $rgb [2]);
Imagefill ($im, 0, 0, $white);
Zoom to a picture
Imagecopyresized ($im, $src _im, 0, 0, 0, 0, $new _width, $new _height, $old _width, $old _height);
return $im;
}
/**
* Class variable Assignment
*/
Public Function __set ($key, $value)
{
$this $key = $value;
}
/**
* Get class variable values
*/
Public Function __get ($key)
{
return $this $key;
}
}
?>

http://www.bkjia.com/PHPjc/320758.html www.bkjia.com true http://www.bkjia.com/PHPjc/320758.html techarticle Copy the code as follows:? PHP/** * Author:yagas * email:yagas60@21cn.com * * Class Image {/** class protection variable */protected $th _width = P rotected $th _height = 50; Protected ...

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