Home Backend Development PHP Tutorial PHP image processing skills revealed

PHP image processing skills revealed

Sep 13, 2023 am 09:30 AM
Skill php image processing Big reveal

PHP image processing skills revealed

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

  1. 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);
Copy after login
  1. 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);
Copy after login
  1. 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);
Copy after login
  1. 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);
Copy after login

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!

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 Article

Hot Article

Hot Article Tags

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)

Done in one minute! How to cast screen from Huawei mobile phone to TV revealed Done in one minute! How to cast screen from Huawei mobile phone to TV revealed Mar 22, 2024 pm 06:09 PM

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 Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Mar 27, 2024 pm 02:57 PM

Win11 Tips Sharing: Skip Microsoft Account Login with One Trick

What are the tips for novices to create forms? What are the tips for novices to create forms? Mar 21, 2024 am 09:11 AM

What are the tips for novices to create forms?

A must-have for veterans: Tips and precautions for * and & in C language A must-have for veterans: Tips and precautions for * and & in C language Apr 04, 2024 am 08:21 AM

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! VSCode Getting Started Guide: A must-read for beginners to quickly master usage skills! Mar 26, 2024 am 08:21 AM

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 PHP programming skills: How to jump to the web page within 3 seconds Mar 24, 2024 am 09:18 AM

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

Win11 Tricks Revealed: How to Bypass Microsoft Account Login Win11 Tricks Revealed: How to Bypass Microsoft Account Login Mar 27, 2024 pm 07:57 PM

Win11 Tricks Revealed: How to Bypass Microsoft Account Login

Detailed explanation of the usage skills of √ symbol in word box Detailed explanation of the usage skills of √ symbol in word box Mar 25, 2024 pm 10:30 PM

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

See all articles