Hai Sob! ketemu lagi di turorial Jagoan Hosting! Jika kamu menemui error dengan notifikasi yang bertuliskan gd library not found atau gd extension must be loaded didalam web hostingmu. maka kamu harus tahu dulu sob mengenai penyebabnya. Yuk kita simak apasih sebenarnya yang menjadi penyebab dari Error gd Library not found : Show
Untuk mengatasinya kamu bisa mengikuti panduan berikut: Berikut langkah-langkah mengatasi error GD Library not found : STEP 1: Login ke cpanel Kamu terlebih dahulu, berikut tampilannya : Login cPanel STEP 2: Cari menu select php server seperti yang ada dibawah ini : Select PHP Version STEP 3: Kemudian akan melihat daftar di bawah pilihan. layar pertama menampilkan versi PHP, yang secara default terdaftar sebagai “native”. Kemudian sesuaikan PHP version yang digunakan untuk menjalankan websitemu, akan tetapi jika kamu ingin mengubah ekstensi individu on / off, kamu dapat melakukan ini dengan mencentang / tidak mencentang berbagai ekstensi. PHP Version STEP 4: Kemudian pastikan module GD sudah diaktifkan. Centang gd STEP 5: Klik save Demikian "Tutorial Mengatasi error gd library not found" semoga bermanfaat. Jika masih ada yang belum di pahami, sobat dapat menghubungi jagoanhosting, melalui open tiket atau Live chat ya Sob! The internet would be pretty dull without images. However, maintaining and manipulating hundreds or thousands of images for your website can be a headache. As your site design changes, you might need to modify all your images—for example, you might need to convert all your images to grayscale or resize them to 50% of their original size. You might also want to compress or crop different images. Doing this manually is time-consuming and error-prone, but with a little programming knowledge it can be automated. In this tutorial, you will learn about the GD (Graphic Draw) library in PHP. You'll see how this library can be used to manipulate images by resizing, cropping, rotating, or filtering them.
What Is GD?PHP can do much more than just serve HTML to visitors. For instance, it has the ability to manipulate images. Not only that, but you can also create your own images from scratch and then either save them or serve them to users. PHP can handle almost all your basic image manipulating needs using the GD library—short for Graphic Draw. SetupIf you are working on Windows, you can include the php_gd2.dll file as an extension in php.ini. If you're using something like XAMPP, you will find the php_gd2.dll file in
the directory xampp\php\ext. You can also check if GD is installed on your system using the function You can also visit the requirements and installation pages to learn more about the installation process. Creating Images Using PHP GDThe first step towards manipulation of images using PHP is loading them into memory as an image resource. This can be achieved by using different functions for different formats. All these functions have very similar names so they are easy to remember. Create a New ImageThe Load an Image FileIf you are planning on manipulating
images that are already stored somewhere, you will benefit from using functions like Create an Image From a StringThe GD library also allows you to create images from a string using the
Rotate, Scale, Crop, and Flip an ImageSome common operations that you might want to perform on an image resource are rotation, scaling, cropping, and flipping. RotationYou can rotate an image that you have already loaded in the script using the Sometimes, the rotated image will have different dimensions in comparison to the original version. This means that you will end up with an uncovered area
after the rotation. The third parameter of the ScalingIt is very easy to scale an image using the GD library. You just have to pass the image resource as well as the width and height to the You can also specify the mode for scaling the
image. It can be set to CroppingYou can crop any image resource using the The butterfly image above was cropped using the following code: $im_php = imagecreatefromjpeg('path/to/image'); $size = min(imagesx($im_php), imagesy($im_php)); $im_php = imagecrop($im_php, ['x' => $size*0.4, 'y' => 0, 'width' => $size, 'height' => $size]); $im_php = imagescale($im_php, 300); Basically, we store the length of the smallest side in the Flipping ImagesImages can be flipped horizontally, vertically or in both directions using the
The top left image in the above
figure is the original. The top right image was created using Applying Filters to an ImageGD also has a very useful For starters, specify the image resource and the name of the filter that you want to apply. You can set it to one of the 12 predefined filter types mentioned in the docs.
Some filters like COLORIZE filter, the bottom left was created using the GRAYSCALE filter, and the image in the bottom right was created using the BRIGHTNESS filter. (This butterfly image was found at Pixabay.)Other Useful Image Manipulation FunctionsYou should also know about some other common GD functions that come in handy every now and then. Get Image DimensionsYou can determine the width and height of an image resource using the Another function called Saving an ImageOnce you have made all the desired changes to an image, you'll most likely want to either output it to the browser or save it as a file. In either case, you will have to use one of the GD output functions like
Get and Set the Colors of Specific PixelsThere are a variety of functions in PHP that you can use to get or set the color of individual pixels at specific locations in an image. The first step is to create an image resource with functions such as After that, you can use the function The color of pixel at any particular location can be set using the Replacing Colors in Images with PHPYou can easily replace one color with another in PHP using the concepts we learned above. The quality of results will vary from image to image. As a general rule, less variation in the colors means better replacement. Solid colors can be replaced very effectively using this method. Let's write some code to replace the colors in the following image. <?php $filename = 'fancy_name.png' $im_php = imagecreatefrompng($filename); $origin_color = imagecolorsforindex($im_php, imagecolorat($im_php, 0, 0)); $dark_red = imagecolorallocate($im_php, 100, 0, 30); list($width, $height, $type, $attr) = getimagesize($filename); for($x = 0; $x <= $width - 1; $x++) { for($y = 0; $y <= $height - 1; $y++) { $index_color = imagecolorsforindex($im_php, imagecolorat($im_php, $x, $y)); if($origin_color == $index_color) { imagesetpixel($im_php, $x, $y, $dark_red); } } } imagepng($im_php, 'fancy_name_red.png'); ?> The code
above uses functions from the previous section to get and set color values. You should note that we are doing an exact color match. This might result in some artifacts because the pixels might vary in color slightly around boundaries. We could also avoid the calls to the Resizing All Images in a DirectoryLet's apply the knowledge we have gained so far to do something practical. In this section, we will resize all the JPEG images in a particular directory to have a width of 640 pixels. The height will be calculated automatically based on the dimensions of the original image. We will save the resized images in a new folder titled Resized. All the original images in this case have the same dimensions, but the code will work properly with images that have different sizes and aspect ratios. $directory = 'Nature/'; $images = glob($directory."*.jpg"); foreach($images as $image) { $im_php = imagecreatefromjpeg($image); $im_php = imagescale($im_php, 640); $new_height = imagesy($im_php); $new_name = str_replace('-1920x1080', '-640x'.$new_height, basename($image)); imagejpeg($im_php, $directory.'Resized/'.$new_name); } In the above code, we begin by using the Since all the images that we want to resize are JPEGs, we use the Each of the original image files had -1920x1080 appended to the filename to indicate its dimensions. We use Finally, we save the resized images in a folder named Resized
with the new filenames. You can also pass a third parameter to the Apply Grayscale and Contrast Filters on Each Image in a DirectoryThis time, we will be applying two different filters on each image in our directory and saving the final result in a different directory without making any changes to the file name. Let's dive into the code, and I will explain what each function does later. $directory = 'Nature/'; $images = glob($directory."*.jpg"); foreach($images as $image) { $im_php = imagecreatefromjpeg($image); imagefilter($im_php, IMG_FILTER_GRAYSCALE); imagefilter($im_php, IMG_FILTER_CONTRAST, -25); $new_name = basename($image); imagejpeg($im_php, $directory.'Grayscale/'.$new_name); } As you can see, we load the images from the Nature directory exactly like we did for the previous example. However, we'll use the Notice that Another important thing to keep in mind is that the contrast filter accepts values from -100 to 100. Negative values imply more contrast, and positive values imply less contrast. This is the opposite of what some people might expect! A value of 0 will leave the image unchanged. The brightness filter, on the other hand, has minimum and maximum limits of -255 and 255. The negative value in this case implies minimum brightness, and the positive value implies maximum brightness. We get the file name from the file path using the Final ThoughtsThe aim of this tutorial was to get you familiar with the GD library in PHP and to show you how to use all these functions to make your life easier. You can use the examples at the end
of the tutorial as a guide to help you write your own image manipulation scripts. For example, you can resize an image only if it is wider than a given limit by determining its width using the All these functions open up a lot of possibilities to make image manipulation easier and save you a lot of time in the end. Did you find this post useful? Freelancer, Instructor I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around. |