With the continuous development of the Internet, pictures have become an important medium for people to communicate and disseminate information online. For website developers, processing and manipulating images is very necessary. Among many programming languages, PHP is a very popular language. This article will introduce how to use PHP to process and operate images.
1. Image upload
In web pages, users are usually required to upload images, and the file upload mechanism provided by PHP makes image uploading very simple. The following is a sample code:
<html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">选择图片:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="上传"> </form> </body> </html>
The code for uploading files can be processed in the upload.php
file. The sample code is as follows:
<?php if (isset($_POST["submit"])) { $file_name = $_FILES["file"]["name"]; $file_tmp = $_FILES["file"]["tmp_name"]; move_uploaded_file($file_tmp, "uploads/" . $file_name); echo "上传成功。"; } ?>
In the above code, ## The #move_uploaded_file() function moves the uploaded image to the
uploads/ directory and saves it on the server.
imagecrop() function for image cropping. The sample code is as follows:
$filename = "uploads/test.jpg"; $image = imagecreatefromjpeg($filename); $crop_image = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => 200, 'height' => 200]); imagejpeg($crop_image, "uploads/crop.jpg");
imagecreatefromjpeg() and
imagecrop()Read and crop a picture respectively. Once the cropping is complete, save it on the server.
imagescale() function to perform image scaling. The sample code is as follows:
$filename = "uploads/test.jpg"; $image = imagecreatefromjpeg($filename); $scale_image = imagescale($image, 200); imagejpeg($scale_image, "uploads/scale.jpg");
imagecreatefromjpeg() and
imagescale()Read and scale a picture respectively. Once the scaling is complete, save it on the server.
imagecopy() and
imagecopymerge() functions for image watermark processing. The sample code is as follows:
$filename = "uploads/test.jpg"; $image = imagecreatefromjpeg($filename); $watermark = imagecreatefrompng("watermark.png"); imagecopy($image, $watermark, 0, 0, 0, 0, imagesx($watermark), imagesy($watermark)); imagejpeg($image, "uploads/watermark.jpg");
imagecreatefrompng() reads the watermark image, and
imagecopy() merges it at the specified location. Finally, the merged image is saved on the server.
quality parameter in the
imagejpeg() function for image compression. The sample code is as follows:
$filename = "uploads/test.jpg"; $image = imagecreatefromjpeg($filename); imagejpeg($image, "uploads/compress.jpg", 50);
imagecreatefromjpeg()Read in a picture.
imagejpeg()Save it on the server. The
quality parameter is 50, which means the image quality has been reduced by 50%.
The above is the detailed content of How to use PHP to process and operate images?. For more information, please follow other related articles on the PHP Chinese website!