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.
Related
I am trying to create a png watermark from text input. I use same function for jpeg and everything works perfectly fine.
I use imagestring(). In the code below I try to make two images, one of them is watermark and second one is resized dafault image.
I uplode photos through my form and they correctly appear in the default photos folder. None of the functions below return an image.
$destination_w is a path to the folder with watermarks. $destination_m is a path to the folder with miniatures. $_SESSION['mime'] and $_SESSION['image'] store correct values as I have tried to var_dump them and they appear in html which is rendered after this functions. Problem is that images do not exist, so they are not visible on my page.
This function is stored in watermark.inc.php file which is included after saving the photos to the default folder and their info in database.
else if ($_SESSION['mime'] === 'image/png'){
$img = imagecreatefrompng($source.'/'.$_SESSION['image']);
$blackcolor = imagecolorallocate($img, 0, 0, 0);
imagestring($img, 3,15, 30, $_SESSION['watermark'], $blackcolor);
imagepng($img, $destination_w.'/'.$_SESSION['image'], 100);
imagedestroy($img);
$img = imagecreatefrompng($source.'/'.$_SESSION['image']);
list($width, $height) = getimagesize($source.'/'.$_SESSION['image']);
$thumb = imagecreatetruecolor(200, 125);
//MINIATURES
imagecopyresized($thumb, $img, 0, 0, 0, 0, 200, 125, $width, $height);
imagepng($thumb, $destination_m.'/'.$_SESSION['image'], 100);
imagedestroy($img);
imagedestroy($thumb);
};
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!';
i have this script to take original picture , resample it twice , for thumbnail and preview.This script works fine , even though You may find some weaknesses of syntactic fashion , im sure.The script as it is , is not subject of my question. I am wondering whether i am supposed to somehow clear memory afterwards.Am i flooding my server with data ? or is this fine and clears itself afterwards.Iam asking because this script will handle my gallery , and it is expected to handle multiple files at once.
script is written like this :
$filename = $DumpHere.$Processed;
// Get new dimensions
list($width, $height) = getimagesize($filename);
// Resample thumbnail
$image_p = imagecreatetruecolor(70, 70);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 70, 70, $width, $height);
// Output Thumbnail
imagejpeg($image_p, $ThumbsFolder.'thumb_'.$Processed, 100);
// Resample preview
$image_p = imagecreatetruecolor(500, 300);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 500, 300, $width, $height);
// Output Preview
imagejpeg($image_p, $PreviewFolder.'preview_'.$Processed, 100);
just to be clear
$DumpHere
is path to a folder containing original files before processing.Thanks for any help.
You want to use imagedestroy() on your resources, so just add:
imagedestroy($image_p);
imagedestroy($image);
At the end and that will free up the memory. PHP is pretty good about getting rid of memory on it's own. For example, once your script ends, all the memory is freed up. But this is the method to explicitly return those resources to the system.
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.
I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038