Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emre-edu-tech/4a40faa453a49c8d6c2e06d28fc188c8 to your computer and use it in GitHub Desktop.
Save emre-edu-tech/4a40faa453a49c8d6c2e06d28fc188c8 to your computer and use it in GitHub Desktop.
Creating a theme screenshot.png file for Wordpress Theme Development.
<?php
// Check if GD is installed
if (!extension_loaded('gd')) {
die("GD library is not installed. Please enable it in your PHP configuration.");
}
// Set image dimensions and properties
$width = 1200;
$height = 900;
$backgroundColor = [240, 240, 240]; // Light gray background
$textColor = [50, 50, 50]; // Dark gray text
// The font file stated below must be in the same path with this file
$fontFile = __DIR__ . '/arial.ttf'; // Specify a TTF font file path
// Create the image resource
$image = imagecreatetruecolor($width, $height);
// Allocate colors
$bgColor = imagecolorallocate($image, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]);
$txtColor = imagecolorallocate($image, $textColor[0], $textColor[1], $textColor[2]);
// Fill the background
imagefill($image, 0, 0, $bgColor);
// Add text to the image
$text = "Your Theme Name";
$fontSize = 40;
$angle = 0;
$box = imagettfbbox($fontSize, $angle, $fontFile, $text);
$textX = ($width - ($box[2] - $box[0])) / 2; // Center the text horizontally
$textY = ($height - ($box[5] - $box[3])) / 2; // Center the text vertically
// textX and textY must be integer because of the signature of the imagettftext() function signature
$textX = intval(round($textX, 0));
$textY = intval(round($textY, 0));
imagettftext($image, $fontSize, $angle, $textX, $textY, $txtColor, $fontFile, $text);
// Output the image to a file
imagepng($image, 'screenshot.png');
// Clean up
imagedestroy($image);
// Inform the user
echo "Screenshot generated successfully!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment