I have an image in a string $data. I need to resize that image, so I convert it to a image with imagecreatefromstring. How to I get that resized image back to $data variable? All I've found is imagepng in its ilk.
Use imagepng with output buffering:
ob_start();
imagepng($image);
$data = ob_get_clean();
Related
I have a PHP function which takes arbitrary string and creates image resource from it, autodetecting image format, as follows:
$im = imagecreatefromstring($image_data_string);
As you surely know, this creates the $im image resource when the $image_data_string contains any supported image type, like JPG, PNG, etc.
After some manipulation to the $im resource (modifying some pixels etc), I would like to return back the image string again in the same format. That is, if the data string contained PNG image, I want to return string of generated PNG image. If it was JPG then I would like to return it as JPG in string.
PHP supports imagejpeg() or imagepng() and other functions, so if I knew how to detect the image type of $im resource then I would be all set. There are function like imagesx() and imagesy() which return width and height of the $im resource, but I am unable to find appropriate function which would return the IMAGE TYPE of the $im resource.
The only way which comes to my mind is to analyze the first bytes of the original $image_data_string in order to find out the image type. But I would prefer a native PHP function for that purpose, which would work on the $im resource argument. Is there anything like that?
Thanks
Use this code instead
$imgdata = base64_decode($image_data_string);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
$im = imagecreatefromstring($image_data_string);
$filename = 'somerandomname';
switch($mime_type){
case 'image/png':
imagepng($image, $filename.'.png');
break;
default:
break;
}
imagedestroy($im);
I have an image.png which is generated dynamically (barcode) and which is returned to html as
<img src="data:image/png;base64,B64STR" />
but it sometimes get defects (white dots).
To convert image to base64 string I use this code:
ob_start();
imagepng($img);
$contents = ob_get_contents();
ob_end_clean();
return base64_encode($contents);
Can be the output buffer be the cause of lost data? Can I convert the image to base64 some other way?
imagepng return a bool. Use file_get_contents without ob :
return base64_encode(file_get_contents($img));
I want to open a image url, convert it to an image, so I can use imagecopymerge() on it, then convert that to base64.
I was able to view the image data using file_get_contents, but I'm not sure how to combine that with imagecreatefromstring, imagecopymerge, and base64_encode.
The way I finally did it was to use
$img=imagecreatefrompng('url');
ob_start();
imagepng($img);
$imgString=ob_get_clean();
$imgBase64=base64_encode($imgString);
I found out that I can't convert an image to a string, and have to use the buffer.
I have a php script that generates an image then outputs the image as a png.
$img = imagecreatetruecolor($graphWidth,$graphHeight);
...
*drawing routines*
...
header("Content-type: image/png");
imagepng($img);
What I now need is to get the php script to base64 encode the image data and output that string (so I can eventually pass that encoded data to a java script which decodes and adds this image to a generated pdf).
I have tried many times to get it working myself using other stackoverflow posts/answers etc. but I don't understand enough about this technology to have gotten it to work.
Can someone help me with this please?
Thanks in advance,
Chris
The reason this doesn't work is because the image in $img is a resource, not an actual PNG image. When you call imagepng(), it outputs the image.
What I would do is create an output buffer, and base-64 encode it.
ob_start();
imagepng($img);
$buffer = ob_get_clean();
ob_end_clean();
echo base64_encode($buffer);
Don't forget to change your Content-Type header, as this is no longer image/png.
I need to convert animated GIF to static in PHP. I mean e.g. use its first frame. Any idea how to do that ?
"Stripping" the GIF of animation can be done by converting it to another format and then back again. PNG is a good candidate for this "other format", since it is non-lossy, unlike JPEG. Using PHPs GD functions, and outputting a PNG instead of a GIF:
header('Content-type: image/png');
imagepng(imagecreatefromgif($file));
This might work (haven't tested) if PHP/GD doesn't support animated GIFs (I don't think it does); and it will output the image in GIF format, unlike the above snippet:
header('Content-type: image/gif');
imagegif(imagecreatefromgif($file));
If that won't work, and output in GIF format is essential, this will:
$img1 = imagecreatefromgif($file);
$size = getimagesize($img1);
$img2 = imagecreatetruecolor($size[0], $size[1]);
imagecopy($img2, $img1, 0, 0, 0, 0, $size[0], $size[1]);
header('Content-type: image/gif');
imagegif($img2);
Take a look at http://php.net/manual/en/function.imagecreatefromgif.php
Check the code snippet from max lloyd.
The best way I could think (not very cute one) is to convert the gif to png/jpeg and then turn it to gif again, :P
try this for converting ;)
http://gallery.menalto.com/node/13206
hope this helps you
yes, you can try the gd library for this
http://php.net/manual/en/book.image.php
take a look into imagejpeg() function