{"flag":true,"single":true,"pageTitle":"GD library |Fetch images from a folder , write text ,and save them to other directory. ||PHP","post":{"id":338,"user_id":"1","slug":"gd-library-fetch-images-from-a-folder-write-text-and-save-them-to-other-directory-php-56gt","title":"GD library |Fetch images from a folder , write text ,and save them to other directory. ||PHP","body":"<p>Directory:<\/p>\r\n<ul>\r\n<li>images\/*.*<\/li>\r\n<li>converted\/*.*<\/li>\r\n<li>index.php<\/li>\r\n<\/ul>\r\n<ol>\r\n<li>This code index.php will fetch images from images folder and display them on browser with an convert button<\/li>\r\n<li>when you click on convert it will write text (\"Rizwan\") on each image and save to converted directory.<\/li>\r\n<\/ol>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\necho '&lt;input type=\"submit\" value=\"convert\" \/&gt;&lt;br\/&gt;';\r\ndefine('IMAGEPATH', 'images\/');\r\n\r\nif (is_dir(IMAGEPATH)){\r\n    $handle = opendir(IMAGEPATH);\r\n}\r\nelse{\r\n    echo 'No image directory';\r\n}\r\n\r\n$directoryfiles = array();\r\nwhile (($file = readdir($handle)) !== false) {\r\n    $newfile = str_replace(' ', '_', $file);\r\n    $directoryfiles[] = $newfile; \/\/all images directory inside images folder\r\n}\r\n\r\nforeach($directoryfiles as $directoryfile){\r\n    if(strlen($directoryfile) &gt; 3){\r\n    echo '&lt;img width=\"100px\" height=\"100px\" src=\"' . IMAGEPATH . $directoryfile . '\" alt=\"' . $directoryfile . '\" \/&gt; &lt;br&gt;';\r\n\r\n\r\n    $img = imagecreatefromjpeg(IMAGEPATH.$directoryfile);\r\n\t$white = imagecolorallocate($img, 255, 255, 255);\r\n    $fontSize = \"18\";\r\n    $rotation = \"270\";\r\n\t$txt = \"Hello World test\";\r\n\t$font = \"C:\\Windows\\Fonts\\arial.ttf\"; \r\n\t\/\/imagettftext($img, 24, 0, 5, 24, $white, $font, $txt);\r\n\t\/\/or\r\n\t$textcolor = imagecolorallocate($img, 255, 2, 5);\r\n\t$textCoords = imagettfbbox($fontSize, $rotation, $font, $txt);\r\n    $x = 36;\r\n    $y = 36;\r\n    imagettftext($img, $fontSize, $rotation, $x, $y, $textcolor, $font, $txt);\r\n    \r\n\t\/\/ OUTPUT IMAGE\r\n\t\/\/ header('Content-type: image\/jpeg');\r\n\t\/\/ imagejpeg($img);\r\n\t\/\/ imagedestroy($jpg_image);\r\n\timagejpeg($img, \"converted\/\".$directoryfile, 100);\r\n\r\n    }\r\n}\r\n\r\nclosedir($handle);\r\n\r\nfunction scrivi($scrivi, $p) {\r\n    $imgResource = imagecreatefromjpeg($p);\r\n    $textcolor = imagecolorallocate($imgResource, 255, 255, 255);\r\n    $fontPath = \"st.ttf\";\r\n    $fontSize = \"18\";\r\n    $rotation = \"270\"; \/\/ counter-clockwise rotation\r\n    $text = \"this is a text\";\r\n    $textCoords = imagettfbbox($fontSize, $rotation, $fontPath, $text);\r\n    $x = 36;\r\n    $y = 36;\r\n    imagettftext($imgResource, $fontSize, $rotation, $x, $y, $textcolor, $fontPath, $text);\r\n    unlink($p);\r\n    imagejpeg($imgResource, $p, 100);\r\n    imagedestroy($imgResource);\r\n}\r\n?&gt;<\/code><\/pre>","category_id":"1","is_private":"0","created_at":"2026-01-07T11:32:42.000000Z","updated_at":"2026-01-07T11:32:42.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Directory:  images\/*.* converted\/*.* index.php   This code index.php will fetch images from images folder and display them on browser with a - GD library |Fetch images from a folder , write text ,and save them to other directory. ||PHP (Updated: January 7, 2026) - Read more about GD library |Fetch images from a folder , write text ,and save them to other directory. ||PHP at my programming site [SITE]","categories":[]}