Learning note-php Image plus watermark combination image simple perfect tailoring-2016.4.7

Source: Internet
Author: User

<?php
/**
* Created by Phpstorm.
* User: Lan Xiaoyu
* DATE:2016/3/30
* time:23:08
*/
Image Processing Class
Class image{
Private $file; Image Address
Private $width; Get the width of an image
Private $height; Get the height of an image
Private $type; Get the type of image
Private $img; The resource handle of the original image
Private $new; New Resource handle
Construction method
Public function __construct ($file) {
$this->file = $_server[' Document_root '). $file;
List ($this->width, $this->height, $this->type) = getimagesize ($this->file);
$this->img = $this->gettype ($this->file, $this->type);
}
/*
* Watermark processing, add watermark to the image, and set the location of the watermark, while the proportional processing of the picture
* @param $new _width New width
* @param $new _height new heights
*/
Public Function ckeimg ($new _width = 0, $new _height = 0) {//initialization
Gets the width and height of the watermark and the type
List ($water _width, $water _height, $water _type) = getimagesize (Mark);//Mark here is a resource handle for a set watermark picture
Get the watermark Resource
$water = $this->gettype (MARK, $water _type);
Determine if the new width and height are present
if (Empty ($new _width) && empty ($new _height)) {
$new _width = $this->width;
$new _height = $this->height;
}
If the value passed is not a number but a letter or something, we also need to handle it.
if (!is_numeric ($new _width) | |!is_numeric ($new _height)) {
$new _width = $this->width;
$new _height = $this->height;
}

Equal proportions processing pictures
if ($this->width < $this->height) {
Let the ratio of length and new height equal
$new _width = ($new _height/$this->height) * $this->width;
}else{
Let the new height and the new length equal proportion
$new _height = ($new _width/$this->width) * $this->height;
}
Set the location of the watermark picture
$water _w = $new _width-$water _width-5;
$water _h = $new _height-$water _height-5;


/**
* Create a background map
* Use function Imagecreatetruecolor (); Two parameter height and width
*
*/
$this->new = Imagecreatetruecolor ($new _width, $new _height);
Create a cropped image
Imagecopyresampled ($this->new, $this->img,0,0,0,0, $new _width, $new _height, $this->width, $this->height );
To add a watermark to the picture, but here to add the judge, only the width and height of the image is larger than the width and height of the watermark, we just add watermark to the picture,
if ($new _width > $water _width && $new _height > $water _height) {
Imagecopy ($this->new, $water, $water _w, $water _h,0,0, $water _width, $water _height);
}
Destroying Watermark Resources
Imagedestroy ($water);
At the time of the output, we destroy the resource handle of the output image, using the Out () method within the class
}

/****************************************/
/*
* Image clipping three: fixed length high, equal column, cropping, enlarging, trimming for image
*
*/
Public function thumb ($new _width = 0, $new _height = 0) {//To avoid no value, so we initialized the new width and height
In addition, a judgment is needed here.
if (Empty ($new _width) && empty ($new _height)) {
$new _width = $this->width;
$new _height = $this->height;
}
If the value passed is not a number but a letter or something, we also need to handle it.
if (!is_numeric ($new _width) | |!is_numeric ($new _height)) {
$new _width = $this->width;
$new _height = $this->height;
}
Fixed generated image width and height
$n _w = $new _width;
$n _h = $new _height;
Initialize crop points
$cut _w = 0;
$cut _h = 0;
Determine the width and height of the original image
if ($this->width < $this->height) {//If the width of the original image is smaller than his height
Let the ratio of length and new height equal
$new _width = ($new _height/$this->height) * $this->width; The new width equals the new height divided by the original height multiplied by the original width.
Formula Explanation: In the equal proportion of the cutting, we first want to find the equal proportion of the factor, that is, according to what proportion to scale
If the section is more than Gao Xiao, then we will use the new height, divided by the old height, to get a proportional percentage, and then multiply the width of the yuan equal to the new width
For example: The original is 500*1000, set the width and height of 150 * 50
Then the new width equals (50/1000) *500
}else{
Let the new height and the new length equal proportion
$new _height = ($new _width/$this->width) * $this->height;
}

Here we need to find a suitable cutting point by another small method, as follows:
if ($new _width < $n _w) {
If the new height is less than the new container height
$r = $n _w/$new _width;
Calculating the equal scale factor by the length
$new _width *= $r;
Length after extended fill
$new _height *= $r;
Extend the height after filling
$cut _height = ($new _height-$n _h)/2;//here, be sure to subtract the height of the container by the height of the scale minus two to get the trimmed point.
Find out the height of the clipping point
}
if ($new _height < $n _h) {
If the new height is less than the container height
$r = $n _h/$new _height;
Calculating the equal scale factor by the height
$new _width *= $r;
Length after extended fill
$new _height *= $r;
Extend the height after filling
$cut _width = ($new _width-$n _w)/2;//here, be sure to use a new width minus the width of the container, divided by two, to get the trimmed point.
Find out the length of the clipping point
}

$this->new = Imagecreatetruecolor ($n _w, $n _h);
Create a cropped image
Imagecopyresampled ($this->new, $this->img,0,0,0,0, $new _width, $new _height, $this->width, $this->height );
}


Determine the image type and then load the image resource
Private Function GetType ($file, $type) {
$img = ";
Switch ($type) {
Case 1:
$img = Imagecreatefromgif ($file);
Break
Case 2:
$img = Imagecreatefromjpeg ($file);
Break
Case 3:
$img = Imagecreatefrompng ($file);
Break
Default
Tool::alertback (' Please upload a file with a picture type of gif,jpg,png! ‘);
}
return $img;
}
Image output
Public function out () {
Imagepng ($this->new, $this->file);//Output
Imagedestroy ($this->img);//Destruction of resources
Imagedestroy ($this->new);//Destruction
}
}

Learning note-php Image plus watermark combination image simple perfect tailoring-2016.4.7

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.