0

For example, I have this "/image.php" file in my "/theme-directory":

header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
      or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);

That code will create a new GD image stream and output an image.

How can I save the outputted image into the Media Library, with a different filename and extension like "filename.png"?

0

I found that there's a predefined WordPress function to upload a file into the Media Library but it's only working asynchronously with a <form> element and a $_FILES array.

Second, I also found that there's a second imagepng() parameter filename which is the path where you want to save the file. I tried to save it to a WordPress uploads' path such "/wp-content/uploads/$yyyy/$dd/filename.png" but it's not showing in the Media Library mostly because there's no record of it from the database.

So, the best thing you could do is to save the image in the "/images" or "/img" folder of your theme directory and use the path of that directory:

$source = get_template_directory() . "/images/filename.png";
imagepng($im, $source);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.