Home > Backend Development > PHP Tutorial > Software for processing images PHP class implementation code for processing images

Software for processing images PHP class implementation code for processing images

WBOY
Release: 2016-07-29 08:41:02
Original
1111 people have browsed it

Copy the code The code is as follows:


/**
* author:yagas
* email:yagas60@21cn.com
*/
class Image
{
/**class protected 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 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 zoom ratio, bloom by percentage when using floating point type. For array types, rows are scaled according to the 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 zoom size
$new_width = $zoom[ 0];
}
//Whether the height of the zoom is defined
if(!isset($zoom[1])) {
//Scaling in equal proportions
$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);
}
/**
* Crop 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);
//Resize when the image is larger than the thumbnail
if($old_width > $width && $old_height>$height) {
$resize_im = $this->imageresize ($src_im, $width);
//When the image proportion does not meet the standard, recalculate the proportion and crop it
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 {
//When the image is smaller than the thumbnail, The image is displayed in the center
$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 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 pictures
* @param resource $src_im Image GD object
* @param integer $width Width of the picture
* @param integer $height Height of the picture, if the height is not set, the picture will be proportional Zoom
* @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;
}
}
?>

以上就介绍了处理图片的软件 PHP 处理图片的类实现代码,包括了处理图片的软件方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template