PHP Image Processing (GD Library)
In this page:
$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
// Print `extension_loaded('gd') ? "GD library available" : "GD not installed"` to the output
echo extension_loaded('gd') ? "GD library available" : "GD not installed";
?>
Login to try C/C++/Java/PHP code in the editor
Canvas Surfaces बनाना
imagecreatetruecolor() memory में एक दी गई width और height का एक खाली canvas allocate करता है, जो save या output करने से पहले आपके shapes, text, या loaded images draw करने के लिए surface बन जाता है।
उदाहरण: Creating Canvas Surfaces
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Geometry Draw करना
GD imageline(), imagerectangle(), और imageellipse() जैसे functions देता है सीधे किसी canvas पर basic shapes draw करने के लिए, जो on the fly simple graphics जैसे charts generate करने के लिए foundation है।
उदाहरण: Drawing Geometry
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Load और Resize करना
imagecopyresampled() एक मौजूदा image load करता है और smooth interpolation के साथ एक resized copy produce करता है, जो है कि आप oversized originals के बजाय properly scaled thumbnails कैसे generate करते हैं।
उदाहरण: Loading and Resizing
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Image Files Save करना
processing खत्म होने पर, imagejpeg() या imagepng() या तो result को disk पर एक file में save करता है या इसे सीधे browser को stream करता है, बशर्ते आप पहले matching Content-Type header भेजें।
उदाहरण: Saving Image Files
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
- बड़े images के लिए
imagedestroycall करना या memory free करना भूल जाना। header('Content-Type: image/png')से पहले output भेजना।- GD extension enabled न होने पर GD functions इस्तेमाल करने की कोशिश करना।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: