![]() ![]() It’s important to note that we've used the imagecreatefromjpeg function as we want to resize the jpeg image in our example. It’ll be used later on, when we actually create the thumbnail image. The thumbnail width parameter is optional, and if you don’t pass any value, it’ll take 100 as the default width.įirstly, we’ve used the imagecreatefromjpeg function, which creates the image resource in memory out of the source image path which was initialized in the constructor. The createThumb method takes two arguments: the destination image path where the thumbnail image will be saved and the thumbnail width which will be used for resizing. Let’s go through the createThumb method in detail to understand how it works. Imagejpeg ( $destImage, $destImagePath ) Imagecopyresampled ( $destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight ) $destImage = imagecreatetruecolor ( $thumbWidth, $thumbHeight ) ![]() $thumbHeight = floor ( $orgHeight * ( $thumbWidth / $orgWidth )) $sourceImage = imagecreatefromjpeg ( $this -> source ) Public function createThumb ( $destImagePath, $thumbWidth = 100 ) Go ahead and create the file with the following contents. Next, we’ll create the example.php file, which demonstrates how to use the ThumbImage class. In this section, we’ll go through a real-world example to demonstrate how you could create image thumbnails in your PHP projects.įirstly, we’ll create the file, which contains the ThumbImage class and holds the logic of creating thumbnail images. Once you’ve installed and enabled the gd extension, it’s time to look at the real-world example, and that’s what we’ll discuss in the next section. You’ll need to have root access to your server shell in order to be able to install it yourself. If you want to install it yourself, take a look at my article explaining how to install specific PHP extensions on your server. In this case, you just need to install the gd extension, and you’re good to go. If you don’t find it, it means that gd is not installed on your server. If it’s installed and configured in your PHP installation, you should be able to find it as shown in the following screenshot. Now, try to find the gd extension section. Next, open the URL in your browser, and it should display the PHP configuration information, as shown in the following screenshot. Upload this file to the document root of your website. If you’re not sure about whether it's there, let's quickly check.Ĭreate the info.php file with the following contents. In a default PHP installation, the GD library should already be enabled. In this section, I'll go through the prerequisites for the example that we'll discuss later in this article.įirstly, you should make sure that the GD library is enabled in your PHP installation. It’s one of the easiest ways to create image thumbnails on the fly. ![]() There are different tools that you can use to resize images in PHP, and we’re going to discuss one of the most popular options among them: the GD library. Instead, you should always resize images before they're displayed on your website. That's because images uploaded by users could be of large size and will not be optimized for web display. Also, if you’ve enabled image uploads on your website, it’s essential that you should never display the original images that are uploaded by users. When you’re working on projects that are related to media, more often than not you will need to create thumbnails from the original images. Today, we’ll discuss how you could create thumbnail images in PHP with the help of the GD library.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |