← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 8 of 10

PHP Image Processing (GD Library)

GD library PHP को pictures draw और edit करने देती है, server पर चल रहे एक tiny paint program जैसा। यह photos resize कर सकता है, उन पर text लिख सकता है, और result save कर सकता है।
Syntax
php
$image = imagecreatetruecolor(width, height);
$color = imagecolorallocate($image, red, green, blue);
imageline($image, x1, y1, x2, y2, $color);
imagepng($image);
imagedestroy($image);

GD Library का Introduction

GD library images programmatically बनाने और manipulate करने के लिए एक PHP extension है -- thumbnails resize करना, watermarks add करना, या server पर पूरी तरह charts generate करना।

उदाहरण: Introduction to GD Library

php
<?php
// Print `extension_loaded('gd') ? "GD library available" : "GD not installed"` to the output
echo extension_loaded('gd') ? "GD library available" : "GD not installed";
?>

Canvas Surfaces बनाना

imagecreatetruecolor() memory में एक दी गई width और height का एक खाली canvas allocate करता है, जो save या output करने से पहले आपके shapes, text, या loaded images draw करने के लिए surface बन जाता है।

उदाहरण: Creating Canvas Surfaces

php
<?php
// Declare `$image`, set to `imagecreatetruecolor(100, 100)`
$image = imagecreatetruecolor(100, 100);
// Print `"Canvas created: " . imagesx($image) . "x" . imagesy($image)` to the output
echo "Canvas created: " . imagesx($image) . "x" . imagesy($image);
// Call `imagedestroy($image)`
imagedestroy($image);
?>

Geometry Draw करना

GD imageline(), imagerectangle(), और imageellipse() जैसे functions देता है सीधे किसी canvas पर basic shapes draw करने के लिए, जो on the fly simple graphics जैसे charts generate करने के लिए foundation है।

उदाहरण: Drawing Geometry

php
<?php
// Declare `$image`, set to `imagecreatetruecolor(100, 100)`
$image = imagecreatetruecolor(100, 100);
// Declare `$color`, set to `imagecolorallocate($image, 255, 0, 0)`
$color = imagecolorallocate($image, 255, 0, 0);
// Call `imagerectangle($image, 10, 10, 90, 90, $color)`
imagerectangle($image, 10, 10, 90, 90, $color);
// Print "Rectangle drawn on canvas" to the output
echo "Rectangle drawn on canvas";
// Call `imagedestroy($image)`
imagedestroy($image);
?>

Load और Resize करना

imagecopyresampled() एक मौजूदा image load करता है और smooth interpolation के साथ एक resized copy produce करता है, जो है कि आप oversized originals के बजाय properly scaled thumbnails कैसे generate करते हैं।

उदाहरण: Loading and Resizing

php
<?php
// Declare `$src`, set to `imagecreatetruecolor(200, 200)`
$src = imagecreatetruecolor(200, 200);
// Declare `$thumb`, set to `imagecreatetruecolor(50, 50)`
$thumb = imagecreatetruecolor(50, 50);
// Call `imagecopyresampled($thumb, $src, 0, 0, 0, 0, 50, 50, 200, 200)`
imagecopyresampled($thumb, $src, 0, 0, 0, 0, 50, 50, 200, 200);
// Print `"Thumbnail: " . imagesx($thumb) . "x" . imagesy($thumb)` to the output
echo "Thumbnail: " . imagesx($thumb) . "x" . imagesy($thumb);
// Call `imagedestroy($src)`
imagedestroy($src);
// Call `imagedestroy($thumb)`
imagedestroy($thumb);
?>

Image Files Save करना

processing खत्म होने पर, imagejpeg() या imagepng() या तो result को disk पर एक file में save करता है या इसे सीधे browser को stream करता है, बशर्ते आप पहले matching Content-Type header भेजें।

उदाहरण: Saving Image Files

php
<?php
// Declare `$image`, set to `imagecreatetruecolor(50, 50)`
$image = imagecreatetruecolor(50, 50);
// Call `imagepng($image, "output.png")`
imagepng($image, "output.png");
// Print `file_exists("output.png") ? "Saved to output.png" : "Save failed"` to the output
echo file_exists("output.png") ? "Saved to output.png" : "Save failed";
// Call `imagedestroy($image)`
imagedestroy($image);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बड़े images के लिए imagedestroy call करना या memory free करना भूल जाना।
  2. header('Content-Type: image/png') से पहले output भेजना।
  3. GD extension enabled न होने पर GD functions इस्तेमाल करने की कोशिश करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.