PHP processing pictures of the class implementation code _php Tips

Source: Internet
Author: User
Tags explode imagejpeg transparent watermark
Copy Code code as follows:

<?php
/**
* 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 file
* @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);
}
/**
* Scale the picture by a percentage
* @param string $SRC Original document
* @param string $DST The destination file entered
* @param float/array $zoom scaling, floating point type with percent bloom, array type scaled at 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)) {
Zoom by percentage
$new _width = $old _width * $zoom;
}
ElseIf (Is_array ($zoom)) {
Explicit Scaling Dimensions
$new _width = $zoom [0];
}
Whether the height of the zoom is defined
if (!isset ($zoom [1])) {
Equal proportional scaling
$resize _im = $this->imageresize ($src _im, $new _width);
}
else {
Non-equal scale 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);
}
/**
* Trim 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 is not standard, recalculate the proportions to cut
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
$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 Picture
* @param $src pictures that need to be written to 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);
}
/**
* Access to different GD objects via 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;
}
/**
* Zoom to the image processing
* @param resource $SRC _im image GD Object
* @param integer $width The width of the picture
* @param the height of an integer $height picture, if you do not set the height, the picture will be scaled 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 populate 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 on 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 the class variable value
*/
Public Function __get ($key)
{
return $this-> $key;
}
}
?>

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.