search
HomeBackend DevelopmentPHP TutorialPhp image processing code sharing

Currently, only three functions are implemented: 1: Image scaling, 2: Image cropping, 3: Adding image watermark
In the instantiation, different functions are achieved by passing different values ​​to the second parameter

Copy Code The code is as follows:


include "image.class.php";
$image=new image("2.png", 1, "300", "500", "5.png "); //Use the image zoom function
$image=new image("2.png", 2, "0,0", "50,50", "5.png"); //Use the image cropping function
$image=new image("2.png", 3, "1.png", "0", "5.png"); //Use the image watermark function
$image->outimage();
? >


PHP code

Copy code The code is as follows:


/*Known issues: 1. In the image scaling function, use the imagecreatetruecolor function to create a canvas and use the transparency processing algorithm, but images in PNG format cannot be transparent.Creating a canvas with the imagecreate function can solve this problem, but the number of colors in the scaled image is too small
*
*
*type value:
* (1): represents the use of the image scaling function. At this time, $value1 represents the zoomed image width, $value2 represents the height of the zoomed image
* (2): represents the use of image cropping function, at this time, $value1 represents the coordinates of the cropping starting point, for example: starting from the origin is "0,0" and is preceded by x Behind the axis is the y-axis, separated by, in the middle. $value2 represents the width and height of the crop. It is also in the form of "20, 20".
* (3): represents the use of the image watermark function. At this time, $value1 represents the watermark. The file name of the image. $value2 represents the position of the watermark in the image. There are 10 values ​​to choose from. 1 represents upper left, 2 represents middle left, 3 represents left and right, 4 represents center left, 5 represents center center, and 6 represents center right. 7 represents bottom, 8 represents bottom middle, 9 represents bottom right, 0 represents random position
*
*/
class image{
private $types; //The function number used, 1 is the image zoom function and 2 is the image cropping function 3. Add image watermark function to images
private $imgtype;//Format of image
private $image; //Image resources
private $width;//Width of image
private $height;//Height of image
private $value1; //According to the different type values ​​passed, $value1 represents different values ​​respectively
private $value2;//According to the different type values ​​passed, $value2 represents different values ​​respectively
private $endaddress;//The address after output +File name
function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
$this->types=$types;
$this->image=$this ->imagesources($imageaddress);
$this->width=$this->imagesizex();
$this->height=$this->imagesizey();
$this->value1 =$value1;
$this->value2=$value2;
$this->endaddress=$endaddress;
}
function outimage(){ // Output different functions according to the different type values ​​passed in
switch ($this->types){
case 1:
$this->scaling();
break;
case 2:
$this->clipping();
break;
case 3:
$this ->imagewater();
break;
default:
return false;
}
}
private function imagewater(){ //Add image watermark function
//Use function to get the length and width of the watermark file
$imagearrs= $this->getimagearr($this->value1);
//Call the function to calculate the location where the watermark is loaded
$positi $imagearrs[0], $imagearrs[1]);
//Add watermark
imagecopy( $this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]) ;
//Call the output method to save
$this->output($this->image);
}
private function clipping(){ //Image cropping function
//Assign the passed in values ​​to variables respectively
list($src_x, $src_y)=explode(",", $this->value1);
list($dst_w, $dst_h)=explode(",", $this->value2);
if ($this->width height return false;
}
// Create a new canvas resource
$newimg=imagecreatetruecolor($dst_w, $dst_h);
//Crop
imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $ dst_h, $dst_w, $dst_h);
//Call the output method to save
$this->output($newimg);
}
private function scaling(){ //Picture scaling function
//Get the proportional scaling Width and height
$this-> proimagesize();
//Scale according to parameters, and call the output function to save the processed file
$this->output($this->imagescaling());
}
private function imagesources($imgad){ //Get the image type and open the image resource
$imagearray=$this->getimagearr($imgad);
switch($imagearray[2]){
case 1://gif
$this->imgtype=1;
$img=imagecreatefromgif($imgad);
break;
case 2://jpeg
$this->imgtype=2;
$img=imagecreatefromjpeg($imgad);
break;
case 3://png
$this->imgtype=3;
$img=imagecreatefrompng($imgad);
break;
default:
return false;
}
return $img;
}
private function imagesizex(){ //Get the image width
return imagesx($this->image);
}
private function imagesizey(){ //Get the image height
return imagesy($this->image);
}
private function proimagesize(){ //Calculate the width and height of the proportionally scaled image
if($this->value1 && ($this->width height)) { // Constant scaling algorithm
$this->value1=round(($this->value2/ $this->height)*$this->width);
}else{
$this->value2= round(($this->value1/ $this->width) * $this->height);
}
}
private function imagescaling(){//Image scaling function, returns the processed image resource
$newimg=imagecreatetruecolor($this->value1, $this->value2);
$tran=imagecolortransparent($this-> ;image);//Handling transparency algorithm
if($tran >= 0 && $tran image)){
$tranarr=imagecolorsforindex($this->image, $tran) ;
$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
imagefill($newimg, 0, 0, $newcolor);
imagecolortransparent($newimg, $newcolor);
}
imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this-> ;width, $this->height);
return $newimg;
}
private function output($image){//Output image
switch($this->imgtype){
case 1:
imagegif($ image, $this->endaddress);
break;
case 2:
imagejpeg($image, $this->endaddress);
break;
case 3:
imagepng($image, $this->endaddress) );
break;
default:
return false;
}
}
private function getimagearr($imagesou){//Return image attribute array method
return getimagesize($imagesou);
}
private function position($num, $width, $height){//Return the coordinates of a position based on the number passed in. $width and $height represent the width and height of the inserted image respectively
switch($num){
case 1:
$positionarr[0] =0;
$positionarr[1]=0;
break;
case 2:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=0;
break;
case 3:
$positionarr[0]=$this->width-$width;
$positionarr[1]=0;
break;
case 4:
$positionarr[0]=0;
$ positionarr[1]=($this->height-$height)/2;
break;
case 5:
$positionarr[0]=($this->width-$width)/2;
$positionarr [1]=($this->height-$height)/2;
break;
case 6:
$positionarr[0]=$this->width-$width;
$positionarr[1]=( $this->height-$height)/2;
break;
case 7:
$positionarr[0]=0;
$positionarr[1]=$this->height-$height;
break;
case 8:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=$this->height-$height;
break;
case 9:
$ positionarr[0]=$this->width-$width;
$positionarr[1]=$this->height-$height;
break;
case 0:
$positionarr[0]=rand(0, $this->width-$width);
$positionarr[1]=rand(0, $this->height-$height);
break;
}
return $positionarr;
}
function __destruct() {
imagedestroy($this->image);
}
}
?>

The above introduces the PHP image processing code sharing, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

如何修复无法连接到iPhone上的App Store错误如何修复无法连接到iPhone上的App Store错误Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

CSS 维度属性详解:height 和 widthCSS 维度属性详解:height 和 widthOct 21, 2023 pm 12:42 PM

CSS维度属性详解:height和width在前端开发中,CSS是一种强大的样式定义语言。其中,height和width是两个最基本的维度属性,用于定义元素的高度和宽度。本文将对这两个属性进行详细解析,并提供具体的代码示例。一、height属性height属性用于定义元素的高度。可以使用像素(pixel)、百分比(percentage)或者

watch4pro好还是gt好watch4pro好还是gt好Sep 26, 2023 pm 02:45 PM

watch4pro和gt各自具有不用的特点和适用场景,如果注重功能的全面性、高性能和时尚外观,同时愿意承担较高的价格,那么Watch 4 Pro可能更适合。如果对功能要求不高,更注重电池续航和价格的合理性,那么GT系列可能更适合。最终的选择应根据个人需求、预算和喜好来决定,建议在购买前仔细考虑自己的需求,并参考各种产品的评测和比较,以做出更明智的选择。

一篇搞懂this指向,赶超70%的前端人一篇搞懂this指向,赶超70%的前端人Sep 06, 2022 pm 05:03 PM

同事因为this指向的问题卡住的bug,vue2的this指向问题,使用了箭头函数,导致拿不到对应的props。当我给他介绍的时候他竟然不知道,随后也刻意的看了一下前端交流群,至今最起码还有70%以上的前端程序员搞不明白,今天给大家分享一下this指向,如果啥都没学会,请给我一个大嘴巴子。

如何使用 iPadOS 17.4 优化 iPad 电池寿命如何使用 iPadOS 17.4 优化 iPad 电池寿命Mar 21, 2024 pm 10:31 PM

如何使用iPadOS17.4优化iPad电池寿命延长电池续航时间是移动设备体验的关键,iPad是一个很好的例子。如果您觉得iPad电池消耗速度过快,不用担心,在iPadOS17.4中有许多技巧和调整可以显著延长设备的运行时间。本深入指南的目标不仅仅是提供信息,而是改变您使用iPad的方式,增强您的整体电池管理,并确保您可以在无需充电的情况下更长时间地依赖您的设备。通过采用此处概述的做法,您朝着更高效、更谨慎地使用技术迈出了一步,这些技术是根据您的个人需求和使用模式量身定制的。识别主要的能量消耗者

See all articles

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

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),