Telegram bot API error 400 Photo has unsupported extension - php

I am trying to send a dynamically created png image (php script that generates a png file, [code below]) through one of my telegram bots using "sendPhoto" method.
It works fine when I link a physical png file (in the multipart field parameter), but when I try and use the php file, I receive the following error from Telegram:
Photo has unsupported extension. Use one of .jpg, .jpeg, .gif, .png, .tif or .bmp
The PHP code is simple enough, works well when I open the file in my browser (i'm shown a dialog to download the png file and it opens fine on my computer):
header("Content-type: image/png");
header('Content-Disposition: attachment; filename="moo.png"');
$image = imagecreatetruecolor(50, 50);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
ob_start ();
imagepng($image);
ob_end_flush();
imagedestroy($image);
Is there a way I can bypass this extension check and send my dynamic image script (file with .php extension) in my telegram request?

turns out bot API had nothing to do with it.
i wrongfully attached the script file when I really should have needed to attach the script output (duh!) :/
So, flushing the output buffer into a tmpfile and then sending it did the trick
function pngCraft($width = 100, $height = 100, $asBase64 = FALSE)
{
$image = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
ob_start ();
imagepng($image);
$image_data = ob_get_contents();
ob_end_clean ();
$out = ($asBase64) ? base64_encode($image_data) : $image_data;
imagedestroy($image);
return $out;
}
$file = tmpfile();
fwrite($file, pngCraft());
fseek($file,0);
$a = stream_get_meta_data($file);
$uri = $a['uri'];
rename($uri, $uri.='.png');
$fields[TG_METHOD_SENDPHOTO_PHOTO] = '#'.$uri;
$fields[TG_METHOD_SENDPHOTO_CAPTION] = 'Yay!';

Related

Save image on php imagepng

I have in a single PHP file an Upload and then, I use imagecreatefrompng to link 2 images, the uploaded image, and another I have in my website.
It works fine in another php file, but when I try to use it in the upload php file, it doesn't save the imagepng($src, "name.png");
The code is:
imagecopymerge($src, $dest, 0, 0, 0, 0, 600, 600, 45);
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$cad = "";
for($i=0;$i<12;$i++) {
$cad .= substr($str,rand(0,62),1);
}
imagepng($src, "fp/$cad.png");
header('Content-Disposition: Attachment; filename="flagperfil.png"');
header('Content-Type: image/png');
imagedestroy($dest);
imagedestroy($src);
I can't put header('Content... because is a PHP file who has a lot of information. If i put this, then it won't be displayed.
Thanks

Convert image format PNG to JPEG without saving to disk - PHP

I am taking a PNG image from a url as below.
I want to convert the PNG image to JPEG without saving disk with PHP.
Finally I want to assign JPEG image to $content_jpg variable.
$url = 'http://www.example.com/image.png';
$content_png = file_get_contents($url);
$content_jpg=;
Simplified answer is,
// PNG image url
$url = 'http://www.example.com/image.png';
// Create image from web image url
$image = imagecreatefrompng($url);
// Start output buffer
ob_start();
// Convert image
imagejpeg($image, NULL,100);
imagedestroy($image);
// Assign JPEG image content from output buffer
$content_jpg = ob_get_clean();
You want to use the gd library for this. Here's an example which will take a png image and output a jpeg one. If the image is transparent, the transparency will be rendered as white instead.
<?php
$file = "myimage.png";
$image = imagecreatefrompng($file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
header('Content-Type: image/jpeg');
$quality = 50;
imagejpeg($bg);
imagedestroy($bg);
?>

PHP: Generate png from text on the fly

I wrote this function to create png file from text :
function pngfromtext($text){
$fontsize = 5;
$width = imagefontwidth($fontsize)* strlen($text);
$height = imagefontheight($fontsize);
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 255, 255);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
}
I put codes to functions.php file, When use this function in another page I get this error:
Warning: Cannot modify header information - headers already sent by (output started at ..\functions.php on line 58
�PNG IHDRZ^%JPLTE����ٟ�tRNS#��f�IDAT�c` Hȱ�7�H��'��`c��s�����i��$���Hl`8��Ɛ�� ��#�c��p�� q�3f�òm�� �g�ـ�6fF ���h�bc�sXd4c�A4����?|�¦����r+���!IEND�B`�
What is wrong?
Set the header at the point where you know that your output is going to be an image. This means set this statement
header('Content-type: image/png');
at the beginning of your php script.
There is also a possibility that the header-command was already executed before this point here.
You can't use multiple header in on page.
before header('Content-type: image/png'); put ob_clean();
that cleans the response object so you can add the headers again

Content-Length for dynamically resized image PHP

I've written an PHP page to dynamically resize an image stored as a BLOB in a MySQL database. I'm doing this on the fly without storing any temporary files. Is there any way to determine a Content-Length to send in the header?
I have the image created with imagecopyresampled() and use imagepng() (or the like) to output the image data.
$origImage = imagecreatefromstring($image['data']);
$newImage = imagecreatetruecolor($width, $height);
// preserve alpha
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
$resizeSuccess = imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $width, $height, $image['width'], $image['height']);
header('Content-Type: image/png');
imagepng($newImage, null, $pngCompression, PNG_NO_FILTER);
imagedestroy($newImage);
Instead of your imagepng call, try this:
ob_start(function($c) {
header("Content-Length: ".strlen($c));
return $c;
});
imagepng($newImage, null, $pngCompression, PNG_NO_FILTER);
obj_end_flush();
Also, the server should handle Content-Length automatically for you.

how to colorize PHP result image using GD

I have a code that resize and colorize the image accordingly input values... the problem is I can able to colorize only one time with fresh image saved by other application..Please help me.. I hope there are many PHP expers are here.....
<?php
createImage(50,50, 0,0, 255);
function createImage($width, $height, $nR, $nG, $nB)
{
$image = imagecreatefrompng("source.png");
imagealphablending($image, false);
imagesavealpha($image, true);
//resize the image
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesx($image));
//colorize the image
$nrgb = str_pad(dechex($nR), 2, '0', STR_PAD_LEFT). str_pad(dechex($nG), 2, '0', STR_PAD_LEFT). str_pad(dechex($nB), 2, '0', STR_PAD_LEFT);
$newColor = $nrgb;
$c2 = sscanf($newColor ,"%2x%2x%2x");
for($i=0;$i<$width;$i++)
{
for($j=0;$j<$height;$j++)
{
$cIndex = imagecolorat($new_image,$i,$j);
imagecolorset($new_image,$cIndex,$c2[0],$c2[1],$c2[2]);
}
}
header("Content-Type: image/png");
imagepng($new_image,"test.png");
}
?>
Sounds to me like you are manipulating an image resource and outputting it and then wanting to go back and further manipulate it without starting over. You can do this by
a) save the image resource as a session variable, and then use the session variable in subsequent alterations.
b) save the altered image before outputting it, and then open the saved altered image and go from there. I don't know what file type you are using but for instance with gif images your code should be using imagegif() to output the image. You would utilize this same function (or other image type equivalent function) to also save the image.
I suggest looking at the imagefilter function found here: http://php.net/manual/en/function.imagefilter.php
Look at IMG_FILTER_COLORIZE on that page.

Categories