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

PHP class implementation code for processing images_PHP tutorial

Jul 21, 2016 pm 03:43 PM
email php code picture deal with copy accomplish of kind

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles