Home > Backend Development > PHP Tutorial > Steps to create image thumbnails using PHP and GD library

Steps to create image thumbnails using PHP and GD library

王林
Release: 2023-07-12 08:04:01
Original
1365 people have browsed it

Title: Steps to create image thumbnails using PHP and GD library

Introduction:
In web development, images often need to be thumbnailed to adapt to different page layouts. This article will introduce the steps on how to use PHP and GD library to create image thumbnails, and attach relevant code examples.

1. Install and configure the GD library
The GD library is a library for image processing. You can use some simple functions to process images. Before we begin, we need to ensure that the GD library is properly installed and configured.

  1. Check whether the GD library has been installed:
    Execute the phpinfo() function in the PHP script and search for the "GD Support" section. If relevant information can be found, it means that the GD library has been installed successfully.
  2. Install the GD library:
    If the GD library is not installed, you can install it through the following steps:

    • Linux system: execute the following command to install the GD library

      sudo apt-get install php-gd
      Copy after login
    • Windows system: Edit the php.ini file, cancel the following lines (remove the semicolon at the beginning of the line) and save:

      ;extension=gd.so
      Copy after login

2. Steps to create image thumbnails
The following are the steps to create image thumbnails using PHP and GD library:

  1. Open the original image:

    $source_image = imagecreatefromjpeg("original.jpg");
    Copy after login
  2. Get the width and height of the original image:

    $source_width = imagesx($source_image);
    $source_height = imagesy($source_image);
    Copy after login
  3. Calculate the width and height of the thumbnail:
    Suppose we want to shrink the original image to 200 Pixel width, and scale the height according to the aspect ratio.

    $thumbnail_width = 200;
    $thumbnail_height = $source_height * ($thumbnail_width / $source_width);
    Copy after login
  4. Create a blank thumbnail canvas:

    $thumbnail_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
    Copy after login
  5. Scale the original image to the thumbnail canvas:

    imagecopyresampled($thumbnail_image, $source_image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $source_width, $source_height);
    Copy after login
  6. Save thumbnails to the specified path:

    imagejpeg($thumbnail_image, "thumbnail.jpg");
    Copy after login
  7. Release memory:

    imagedestroy($source_image);
    imagedestroy($thumbnail_image);
    Copy after login

3. Sample code

// 打开原始图像
$source_image = imagecreatefromjpeg("original.jpg");

// 获取原始图像的宽度和高度
$source_width = imagesx($source_image);
$source_height = imagesy($source_image);

// 计算缩略图的宽度和高度
$thumbnail_width = 200;
$thumbnail_height = $source_height * ($thumbnail_width / $source_width);

// 创建一个空白的缩略图画布
$thumbnail_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

// 缩放原始图像到缩略图画布
imagecopyresampled($thumbnail_image, $source_image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $source_width, $source_height);

// 保存缩略图到指定路径
imagejpeg($thumbnail_image, "thumbnail.jpg");

// 释放内存
imagedestroy($source_image);
imagedestroy($thumbnail_image);
Copy after login

Conclusion:
With PHP and GD library, we can easily create image thumbnails to suit different page layouts. The above are the steps for creating image thumbnails, with relevant code examples attached. I hope it will be helpful to you.

The above is the detailed content of Steps to create image thumbnails using PHP and GD library. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template