problem in showing an img open from zip archive - php

iam get the img date from the zip archive by getFromIndex()
and the date like that (this the first line of text appearing)
‰PNG ��� IHDR���#���#���ªi
know how i can makke appear
iam used
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
but its did not worked
and used
header('Content-Type: image/png');
imagepng($data);
imagedestroy($im);
but its did not worked and giving me
Warning: imagepng(): supplied argument is not a valid Image resource
now you asked me about the date format
we assume the output is
$date = file_get_contents('http://sstatic.net/so/img/logo.png');
the text appear is the same format
but i must use $date because i must get the contents first

I suspect this image data is not the only output of this script, right?
But it must be.

The data already looks like a PNG file (notice the magic number). What happens if you output the data to a file ? Or How about just echo the data and don't call imagepng. Will that work ?

Related

How to convert image stored in db as hex into visible image

Context
I have access to database. That database contains image data something like 0xFFD8FFE....................09090
Problem
I need those cryptic(hex) data to convert into visual image. I am not being able to find any way.
I have tried
$image = base64_decode(file_get_contents('img.txt'));
$db_img = imagecreatefromstring($image);
Header("Content-type: image/jpeg");
imagepng($db_img);
Here img.txt is file name containing the hex data.
It Doesn't work. How can I achieve this?
To turn a string like 0xFFD8FFE...09090 into actual binary data, you need to first get rid of the "0x" prefix, which is not part of the actual data, and then do a simply hex → bin conversion:
$data = '0xFFD8FFE...09090'; // maybe from file_get_contents
$binary = hex2bin(substr($data, 2));
If you don't know what type of image it is then, the most efficient method to find out is probably the use of finfo:
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->buffer($binary);
header('Content-Type: ' . $type);
echo $binary;
There's no need to use gd to reencode the image if $binary already is an image. You may want to inspect $type here a bit to verify that it's really the kind of file you expect before blindly outputting it.

GD imagefilter no image output

I'm trying to convert an jpg image to grayscale with GD imagefilter but i can't get the filtered image to output on the browser. I use this code
<?php
$thumb="w=250&q=100";
$imgstring = get_image('cover_image',1,1,0,NULL,$thumb);
$im = imagecreatefromjpeg($imgstring);
imagefilter($im, IMG_FILTER_GRAYSCALE);
header("Content-type: image/jpg");
imagejpeg($im);
imagedestroy($im);
?>
**The $imgstring is the url string of the image ('http://xxx.xx/image.jpg').
With the header content line the result is an error "Warning: Cannot modify header information - headers already sent by..." followed by what appears to be the image code (a lot of question marks characters and other garbage).
Without the header content line the result is just the image code(?) again (symbols and characters)
What's wrong?
The error is exactly what it says - something in your code has caused output to be performed, which kills the header() call, preventing the content-type from being set. In the absence of any valid content-type header, the client browser assumes text/plain, and is showing your JPEG image - as raw "garbage".
You need to figure out WHERE in your code that output is being performed (I'm willing to bet a shiny penny it's in that get_image() function, and eliminate it.

Hexadecimal String to binary PDF file

I have a hexadecimal value that is a PDF that I am getting from a web service that I am trying to save locally using PHP. The below is a snippet of the value.
I have tried to achieve this using pack in PHP either receive an error that "x" is not valid or the pdf will not save correctly. It will be empty or says error opening.
Partial Value is: "0x255044462D312E340A0A322030206F626A0A3C3C2F5479"
I have tried the following unsuccessfully after searching google for some time:
$pack = pack("h*", $string);
file_put_contents('my.pdf', $pack);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $pack;
Can anyone tell me what I am doing incorrectly? I realize this is not the entire PDF but I cant put entire online.
Thanks for any help

php base64 encode imagecreatetruecolor

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.

export an image in base64

I want to create an image in php having the data encoded in base64
Use the code: file_put_contents('export/MyFile.png', base64_decode($img));
I read on the browser, but 7483 should be a larger number.
if I open the image was created only half (the other half is transparent)
if the variable $img contains a short string, it works.
if it contains a string too long the image will be created only partially.
why?
PS: if I use
$img = base64_decode($img);
$fp = fopen("export/MyFile.png", "w");
fwrite($fp, $img);
fclose($fp);
I have the exact same problems
thanks
file_put_contents('export/MyFile.png', base64_decode($img));
Should be:
file_put_contents('export/MyFile.png', base64_encode($img));
If you want to encode the image, you can't use the decode function.

Categories