Home >Backend Development >PHP Tutorial >PHP image processing skills revealed

PHP image processing skills revealed

王林
王林Original
2023-09-13 09:30:111250browse

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);
  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);
  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);
  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);

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:
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