Home >Backend Development >PHP Tutorial >PHP class implementation code for processing images_PHP tutorial

PHP class implementation code for processing images_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:29932browse

Copy code The code is as follows:

/**
* author:yagas
* email:yagas60@21cn.com
*/
class Image
{
/**Class protected variables*/
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 image file
* @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 image by percentage
* @param string $src original image file
* @param string $dst input target file
* @param float/array $zoom scaling ratio, Bloom by percentage for floating point type, and scale by specified size for array type
* @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)) {
//Clear The zoom size
$new_width = $zoom[0];
}
//Whether the zoom height is defined
if(!isset($zoom[1])) {
/ /Scaling
$resize_im = $this->imageresize($src_im, $new_width);
}
else {
//Non-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);
}
/**
* Crop the image
* @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);
//When the image is larger than the thumbnail Zoom when
if($old_width > $width && $old_height>$height) {
$resize_im = $this->imageresize($src_im, $width);
//The image ratio is not appropriate When normalizing, recalculate the proportion 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 {
//The image is smaller than Center the image when thumbnailing
$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 The image that needs to be watermarked
* @param $mark Watermark image
* @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);
}
/**
* Obtain different GD objects through files
*/
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 scaling the image
* @param resource $src_im image GD object
* @param integer $width width of the image
* @param integer $height height of the image, if not set Height, the image will be scaled proportionally
* @return resuorce $im returns 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;
//创建新的图象并填充默认的背景色
$im = imagecreatetruecolor($new_width, $new_height);
$rgb = explode(",", $this->background);
$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
imagefill($im, 0, 0, $white);
//对图片进行缩放
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 value
*/
public function __get($key)
{
return $this->$key;
}
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320758.htmlTechArticle复制代码 代码如下: ?php /*** author:yagas * email:yagas60@21cn.com*/ class Image { /**Class protected variables*/ protected $th_width = 100; protected $th_height = 50; protected...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn