Getting image from XML php - php

I am struggling with getting images from xml file
Let's say one image is stored in XML like this
<obrazok1>/9j/4AAQSkZJRgABAQAAAQABAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gMTAwCv/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf ...</obrazok1>
My php code looks like this
dump($x->ponuka->obrazok1);
$data = base64_decode($x->ponuka->obrazok1);
dump($data);
$image = imagecreatefromstring($data);
header('Content-Type: image/png');
imagepng($image);
After this i get a typical icon of not known image ...
I think it might be a problem after using base64_decode.
It gives me:

Is it your dump? Try commenting your dumps
// dump($x->ponuka->obrazok1); // comment this
// dump($data); // comment this

Related

JQplot - jqplotToImageStr - How to convert the generated string back into a chart

I can create the chart, display it, generate an image and convert the image using jqplotToImageStr which is then posted to another php page.
I want to be able to convert the string back into an image and display it on the php page which I can then convert to pdf. I've tried using this php code
$img = $_POST['hidImage'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
return $data;
(Using modx 2.5.1 in a snippet)
But it doesn't render as an image - the image shows the top and bottom sections of the source code (345 lines in total) that is generated.
I'd really appreciate any help or advice before my sanity finally leaves me :-(
You need to set headers for the image type.
Check this answer for your reference.
header('Content-type: image/png');

PHP: Show JPG from Binary

I have a jpg blob thats been stored in an external DB and I'm looking to display that as an image via php. The issue is whenever I set the Content-Type to image/jpeg and echo out the blob I get the broken image icon when browsing to it.
I have tried making the file from scratch via sublime and that works when I save it as a hexadecimal file so I know the data is valid.
I have tried making the script create a file but it sets the charset=us-ascii so it won't get seen as a image file.
Does anyone have any experience with raw image binary files? anyone know how I can display the image or even save it out to a file?
Thanks in advance.
P.S. I would provide the binary but its just too big to put on here.
EDIT: (added some code)
<?php
header('Content-Type: image/jpeg;');
$data = 'some long string of hex';
// tried echoing it directly..
echo $data;
// and writing to a file...
file_put_contents('test.jpg', $data);
?>
After continuing research I found this post PHP: create file from an HEX string
With the following code I fixed the issue.
<?php
header('Content-Type: image/jpeg;');
$data = 'The hex data';
$data = pack('H*',$data);
$im = imagecreatefromstring($data);
imagejpeg($im);
?>
Try $im = imagecreatefromstring($data); The output it by imagejpeg($im);

How to get and save THIS transparent image with PHP

I am trying to get a image from a URL, and then save it to the server. It is a simple task, yeah, but this image has transparency... The thing is, when i save that file to my PC or when I try to get and save it via PHP to my server, then I get the same result: the image gets all messed up with its transparency set to black. The URL of the image is:
http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|
Another weird thing is my browser says the image is MIME Type image/png but I can't use imagecreatefrompng(); because I get the "image not a valid PNG" error...
Here is my code (I did try other solutions, this is what I tryed the last time):
<?php
$result=file_get_contents("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|F04001|Shirt|F10020|Pants|MO55|");
$img = imageCreateFromstring($result);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
imagecolortransparent($img);
header("Content-type: image/png");
imagepng($img);
?>
Oh, and I just tryed this image against the copy() function, and still get the same result... It looks like the image is smaller in size then the original image...
Just tryed this:
file_put_contents("files/test.png",file_get_contents($result));
and still not working... so it has something to do with the image itself, because whatever i try to get the data, it does not work.
Try this code (changes at lines 2-4):
<?php
$img = imageCreateFromgif("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|");
$index = imagecolorexact($img, 0, 0, 0);
imagecolortransparent($img, $index);
header("Content-type: image/png");
imagepng($img);
?>
It works for me although it's a little trick ;) ..and I don't know exactly why the quality is worse..
PS: The main problem I see is that the image at the url you provided is not really a png.

PHP mySQL turning a string into a png is only working for the first image

So I am trying to get a bunch of photos to appear that are saved in a mysql database. With the code I am using, the page displays just the very first photo in the database with nothing else. There are 5 different photos and I don't know where they are going. If someone could help that would be great. My code is here:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
}
The headers will only display one image per page.
You could try:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
print '<img src="data:image/png;base64,'.$data.'" />';
}
That's untested, but something like that should work.
A better way would be to store the image file on the disk and store the location in the database.
You could either insert them into mysql as in blob type or simply upload the file to a folder and insert the file name into the database (I assume you just dont create random folders for each instance therefore you would know the destination path already)

PHP Debugging: How to view image resource information

I am debugging a PHP application that handles image resources. I would like to see the output ($dst_image as per the PHP manual's jargon), but the code is not in a place that I could simply output it to the browser. Would the best debugging procedure be to write $dst_image to a file, and to load that file in the browser? Any other ideas?
Thanks.
See Example #1 and #2, imagejpeg will output the jpeg data.
You need to do few tings:
set headers for image header('Content-Type: image/jpeg');
output your image data
Make sure you are not outputting any data except the image
At the end you should end up with something like this:
<?php
// Get new dimensions
// Resample
// etc...
// set header so browser can render image properly
header('Content-Type: image/jpeg');
// Output
imagejpeg($image, null, xxx);
// [or]
echo file_get_contents($pathToJpgImage);
If you find your self in a situation where current request outputs data and you cannot output image... You can inject the base64 encoded image data into the HTML by using <img src="data:image/jpeg;base64,..." />. See php documentation on base64_encode for images.

Categories