PHP image processing skills revealed
Sep 13, 2023 am 09:30 AMPHP Image Processing Skills Revealed
In modern Internet applications, image processing is one of the very common requirements. Whether it is compressing, cropping, adding watermarks to images, generating thumbnails, etc., you need to use image processing skills. As an excellent server-side programming language, PHP provides rich image processing functions and extension libraries, which provides good support for us to achieve these needs. This article will reveal some PHP image processing techniques and provide specific code examples.
- Image compression
Image compression is an important means to optimize website loading speed and reduce bandwidth consumption. You can use the GD library in PHP for image compression. The following is a sample code for image compression using the GD library:
function compressImage($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source); } imagejpeg($image, $destination, $quality); return $destination; } $source = 'original.jpg'; $destination = 'compressed.jpg'; $quality = 50; compressImage($source, $destination, $quality);
- Image cropping
In practical applications, images often need to be cropped according to needs to suit Different size requirements. The following is a sample code for image cropping using the GD library:
function cropImage($source, $destination, $x, $y, $width, $height) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source); } $crop = imagecrop($image, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]); imagejpeg($crop, $destination); return $destination; } $source = 'original.jpg'; $destination = 'cropped.jpg'; $x = 0; $y = 0; $width = 200; $height = 200; cropImage($source, $destination, $x, $y, $width, $height);
- Add watermark
Watermark is a way to protect image copyright and display image information. You can easily add watermarks to images using PHP. The following is a sample code that uses the GD library to add watermarks to images:
function addWatermark($source, $watermark, $position) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source); } $watermarkImg = imagecreatefrompng($watermark); $watermarkWidth = imagesx($watermarkImg); $watermarkHeight = imagesy($watermarkImg); switch ($position) { case 'top-left': $x = 0; $y = 0; break; case 'top-right': $x = imagesx($image) - $watermarkWidth; $y = 0; break; case 'bottom-left': $x = 0; $y = imagesy($image) - $watermarkHeight; break; case 'bottom-right': $x = imagesx($image) - $watermarkWidth; $y = imagesy($image) - $watermarkHeight; break; default: $x = 0; $y = 0; break; } imagecopy($image, $watermarkImg, $x, $y, 0, 0, $watermarkWidth, $watermarkHeight); imagejpeg($image, $source); return $source; } $source = 'original.jpg'; $watermark = 'watermark.png'; $position = 'bottom-right'; addWatermark($source, $watermark, $position);
- Generate thumbnails
When displaying images, in order to adapt to different display needs, often Need to generate thumbnails. The following is a sample code that uses the GD library to generate thumbnails:
function generateThumbnail($source, $destination, $width, $height) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source); } $thumb = imagecreatetruecolor($width, $height); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image)); imagejpeg($thumb, $destination); return $destination; } $source = 'original.jpg'; $destination = 'thumbnail.jpg'; $width = 200; $height = 200; generateThumbnail($source, $destination, $width, $height);
The above are some PHP image processing techniques and corresponding code examples. By using the GD library and PHP's image processing functions, we can easily compress, crop, add watermarks, and generate thumbnails to images. I hope this article helps you when working with images in development.
The above is the detailed content of PHP image processing skills revealed. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

Done in one minute! How to cast screen from Huawei mobile phone to TV revealed

Win11 Tips Sharing: Skip Microsoft Account Login with One Trick

What are the tips for novices to create forms?

A must-have for veterans: Tips and precautions for * and & in C language

VSCode Getting Started Guide: A must-read for beginners to quickly master usage skills!

PHP programming skills: How to jump to the web page within 3 seconds

Win11 Tricks Revealed: How to Bypass Microsoft Account Login

Detailed explanation of the usage skills of √ symbol in word box
