PHP image file operation encapsulation class
This article mainly introduces the PHP image file operation encapsulation class. Interested friends can refer to it. I hope it will be helpful to everyone.
php mainly uses GD library extension to operate image files. When we frequently use PHP to operate images, we will naturally encapsulate many functions, otherwise we will write too much repetitive code. When there are many functions related to pictures, we can consider organizing these functions, so we have the idea of encapsulating them into classes.
There are four main steps to operate pictures:
1. Open the picture2. Manipulate the picture
3. Output the picture
4. Destroy the picture## Steps #1, 3, and 4 need to be written every time, and they are almost the same every time. The only thing that really needs to be changed is the step of manipulating images. Manipulating pictures is often done through one or more main GD functions. <?php
class Image
{
private $info;
private $image;
public $type;
public function __construct($src)
{
$this->info=getimagesize($src);
$this->type=image_type_to_extension($this->info['2'],false);
$fun="imagecreatefrom{$this->type}";
$this->image=$fun($src);
}
/**
* 文字水印
* @param [type] $font 字体
* @param [type] $content 内容
* @param [type] $size 文字大小
* @param [type] $col 文字颜色(四元数组)
* @param array $location 位置
* @param integer $angle 倾斜角度
* @return [type]
*/
public function fontMark($font,$content,$size,$col,$location,$angle=0){
$col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);
imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);
}
/**
* 图片水印
* @param [type] $imageMark 水印图片地址
* @param [type] $dst 水印图片在原图片中的位置
* @param [type] $pct 透明度
* @return [type]
*/
public function imageMark($imageMark,$dst,$pct){
$info2=getimagesize($imageMark);
$type=image_type_to_extension($info2['2'],false);
$func2="imagecreatefrom".$type;
$water=$func2($imageMark);
imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);
imagedestroy($water);
}
/**
* 压缩图片
* @param [type] $thumbSize 压缩图片大小
* @return [type] [description]
*/
public function thumb($thumbSize){
$imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);
imagedestroy($this->image);
$this->image=$imageThumb;
}
/**
* 裁剪图片
* @param [type] $cutSize 裁剪大小
* @param [type] $location 裁剪位置
* @return [type] [description]
*/
public function cut($cutSize,$location){
$imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);
imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);
imagedestroy($this->image);
$this->image=$imageCut;
}
/**
* 展现图片
* @return [type] [description]
*/
public function show(){
header("content-type:".$this->info['mime']);
$funn="image".$this->type;
$funn($this->image);
}
/**
* 保存图片
* @param [type] $newname 新图片名
* @return [type] [description]
*/
public function save($newname){
header("content-type:".$this->info['mime']);
$funn="image".$this->type;
$funn($this->image,$newname.'.'.$this->type);
}
public function __destruct(){
imagedestroy($this->image);
}
}
?>
: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:php supports Chinese file downloadDetailed explanation of Mongodb connection to remote database under PHP And casesThe principle of PHP text to image function
The above is the detailed content of PHP image file operation encapsulation class. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Want to know how to add watermark to MeituXiuXiu? Meitu Xiuxiu is a very easy-to-use photo editing software. It provides functions such as cutting out pictures and placing them on another picture, changing the picture size by kb, removing watermarks, changing the background color of ID photos, and adding time, date and location watermarks to the full screen. Help users quickly complete the production of pictures. Some users have created their own pictures and don’t want others to steal them. They want to cover them with their own watermarks, but don’t know how to do it? The editor will now share with you how to add watermarks to beautiful photos! If you like it, come and download it! 1. How to add watermark to beautiful pictures? Share how to add watermark to beautiful photos! 1. Open the 2023 version of Meitu Xiu Xiu downloaded from this site. Meitu Xiu Xiu 2023 version Category: Shooting and beautification Download Meitu Xiu Xiu 2023 version is a feature-rich picture beautification and editing software

Removing watermarks is a useful tool in the software Scanner. Some users are not sure how to remove watermarks in Scanner. You can click Remove Watermark in Edit PDF on the save interface to close it. Next, the editor will explain Users brought us an introduction to how to remove watermarks. If you are interested, come and take a look! Scanner King usage tutorial How to remove the watermark with Scanner King? Answer: You can click on the save interface to edit the watermark removal in the PDF. Details: 1. Enter the software and click the [Camera] icon. 2. Photograph and scan the documents that need to be watermarked. 3. Click [→] to proceed to the next step. 4. After completing editing, click [✓]. 5. Click [Edit PDF]. 6. Select [Remove Watermark] below.

Open the clip and select a template. After editing the video, click Export, click Save and share without watermark. Applicable model of the tutorial: iPhone13 System: iOS15.3.1 Version: Cutting 6.8.0 Analysis 1 Open Cutting and select a cutting template. 2. After clicking to open the template, click the cut the same style option in the lower right corner. 3Select the photo fill segment from the album and click Next. 4How to click the export option in the upper right corner of the page. 5Finally, click Save and Share without watermark. Supplement: What kind of software is Cutting? 1 Cutting is a video editing software that has comprehensive editing functions, can change speed, has filters and beauty effects, and has rich music library resources. Starting from February 2021, clipping will be supported on mobile phones, Pads, and Mac computers.

How to remove watermarks from videos in Wink? There is a tool to remove watermarks from videos in winkAPP, but most friends don’t know how to remove watermarks from videos in wink. Next is the picture of how to remove watermarks from videos in Wink brought by the editor. Text tutorial, interested users come and take a look! How to remove video watermarks in Wink 1. First open wink APP and select the [Remove Watermark] function in the homepage area; 2. Then select the video you want to remove the watermark in the album; 3. Then select the video and click the upper right corner after editing the video. [√]; 4. Finally, click [One-click Print] as shown in the figure below and then click [Process].

Xiaomi Mi 14 Ultra is a new model with very good performance and configuration. This mobile phone also adopts a unique appearance design. It has a higher appearance and is very recognizable. Many consumers who want to buy it want to know how to use Xiaomi Mi 14 Ultra. Remove watermark? , the editor of this site will introduce it to you below! How to remove watermark from Xiaomi 14Ultra? 1. Open the camera application: Find and open the pre-installed camera application on Xiaomi 14. 2. Enter the settings menu: Tap the lower right or lower left corner of the screen (depending on which version you are using) to display the additional options button. Then, select "Settings" in the pop-up menu. 3. Turn off watermark option: In the settings menu, you will see various options and parameters. Scroll down until you find the "Watermark" option

In order to make the photos taken more personalized and unique, Xiaomi Mi 14 provides photo watermark settings. By setting photo watermarks, users can add patterns, text and logos to the photos they take, so that each photo can better record precious moments and memories. Next, we will introduce how to set a photo watermark in Xiaomi 14 to make your photos more personalized and vivid. How to set photo watermark on Xiaomi Mi 14? 1. First click “Camera”. 2. Then click "Settings". 3. Then find the watermark, and then you can start shooting.

How to remove the evaluation copy text in the lower right corner of win1124H2? When we use the system, sometimes the desktop will display a transparent watermark on the lower right corner of the screen. So how do we remove this transparent watermark? Users can directly use third-party software to operate. Let this site carefully introduce to users how to remove the watermark on the win1124H2 evaluation copy. To remove the watermark on the win1124H2 evaluation copy, download the UniversalWatermarkDisabler tool. After running it, the current system version and watermark status will be displayed. If "Ready for installation" is displayed in "Status", it can be removed.

1. We use WPS to open a document. There is a watermark in it. It looks messy. How to remove it? Look down. 2. Find the Insert tab in the menu bar, select the header and footer icons under this tab, and click on them with the left mouse button. 3. At this time, the text on the page becomes gray and cannot be edited, but the watermark on the back of the text can be edited at this time. 4. Click on the watermark, you can see that this is a picture watermark, because there are several small squares around the picture, and the picture can be edited at this time. 5. Use the delete key on the keyboard to delete the picture, and you can see that the watermark is gone. 6. Double-click the mouse on the page to exit the header and footer editing mode. The text on the page returns to normal color and can be edited, but at this time the watermark on the page has disappeared. 7.
