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.
Related
I've the data uri of a png image, I've stored the data uri in mysql data base. Lets say img.php is the file that returns as a png image
<?php
$id=$_GET['id];
//data base connection and mysql queries
$data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOAAAADMCAYAAABqQ6+sAAAEn0lEQVR4Xu3TUQkAIBAFQc1iHsOaUMES+zNX4MFwO9fZdzgCBBKBKcDE3SiBLyBAj0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgkI0A8QCAUEGOKbJiBAP0AgFBBgiG+agAD9AIFQQIAhvmkCAvQDBEIBAYb4pgk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FBBgiG+awANJn7JcDjYVEQAAAABJRU5ErkJggg==";
//$data is obtained from mysql database
header('Content-Type: image/png');
$im = imagecreatefromstring($data);
imagepng($im);
imagedestroy($im);
?>
I've tried the above code, but its not giving me any output,
please help.
Thanks in advance.
The code is mostly pointless. You already have a PNG inside the data uri, so there is exactly ZERO point in loading that PNG into GD, then re-compressing to a PNG. that's massive waste of RAM and CPU time to basically accomplish nothing.
Also, GD doesn't understand data uris, so you cannot feed this uri into GD functions and expect something useful to happen.
You already have a PNG, so all you need to do is basically this:
$b64_png = string_operation_to_extract_base64_data_from_data_uri($datauri);
header('Content-type: image/png');
echo base64_decode($b64_png);
The code works fine if i display only image with the help of code.
<?php
// my code processing
header("Content-type: image/png");
imagepng($base_image);
?>
But if i use some other fields like echo some text or i want to put some buttons on my page.
I get error, for code:
<?php
echo "hi";
?>
<?php
// my code processing
header("Content-type: image/png");
imagepng($base_image);
?>
It gives me error : The displayed page contains some errors.
Can someone please help me in this regard.
Any output before the Content-Type header will break your code. The browser does not know you are trying to serve it an image since it will have already defaulted to text/html by the time your image data turns up. If you want an image at a given point in your page, you will need to serve it as a separate object. The easiest way is to wrap it in an <img> tag e.g. <img src="myimage_generator.php" />
This answer is based off of another SO answer. Your problem is that you're trying to send header info after you already sent data to the browser, which is not possible. Even so you can't display an image on a page with it's data alone. You need to base64 encode the image data first. This way you can build a whole HTML page and place this image anywhere on it and position it with CSS.
// Enable output buffering
ob_start();
imagepng($base_image);
// Capture the output
$imagedata = ob_get_contents();
// Clear the output buffer
ob_end_clean();
echo '<img src="data:image/png;base64,'.base64_encode($imagedata).'">';
I have the following script to output an image to the browser wich works fine.
$file_to_output=$_SERVER['DOCUMENT_ROOT'].'/static/imgs/uploads/20110318172207_16.jpg';
header('Content-Type: image/jpeg');
$raw=imagecreatefromjpeg($file_to_output);
// Output the image
imagejpeg($raw);
// Free up memory
imagedestroy($raw);
when I put this exact same code in a view its doesn't work anymore and give a bunch of stange characters like this:
����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ��C
What do I have to do to make it work in a view?
You're not supposed to put that into a view. All view output is buffered, being returned through a Response object later.
This is all response logics, so your action code should look like:
$path = DOCROOT.'static/imgs/uploads/20110318172207_16.jpg';
$this->response->headers('content-type',File::mime($path))
->body(file_get_contents($path));
Another way would be:
$path = DOCROOT.'static/imgs/uploads/20110318172207_16.jpg';
// Send file as download
$this->response->send_file($path);
// Send file as inline
$this->response->send_file($path, NULL, array('attachment' => 'inline'));
// Another way to send as inline
$this->response->body(file_get_contents($path));
$this->response->send_file(TRUE, $path);
see Response#send_file
So i have an iphone app that that uploads an image to my webserver and i looked around and people seem to be doing something like
$data = file_get_contents($_FILES['file']['tmp_name']);
$image = imagecreatefromstring($data);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
I looked at the php docs, but i still don't understand what the header() does; does it convert the image into whatever format i want?
And for the imagepng(), where is the image outputted to? memory? is that why i need the imagedestroy()?
and where would i put in
move_uploaded_file()
Thanks in advance!
This code is intended to return as output an image - you could use it as a valid src for an image tag. That is, you could do this:
<img src="thatfile.php?something=1" />
The headers tell the browser that the data the server is going to send is an image (a PNG image, specifically).
In your example code, the file never actually gets written anywhere: the data stays in memory until the script ends, then it is simply "forgotten". imagedestroy frees up the memory and is good practice, but it really isn't necessary since the memory will be garbage collected after the request ends. If you want to preserve the image in a file, you'd have to use one of the related functions such as imagepng: http://www.php.net/manual/en/function.imagepng.php. The only difference between writing the file or not in your example code is the lack of a second argument for imagepng - second argument would be the desired file path.
It would help to read through the docs on this entire subject to gain a firm grasp of how these functions work and what each does. There are plenty of demos on the doc pages that show this in action.
This particular example gets the image uploaded through POST from the $_FILES array and simply outputs it back to the browser. The header is there to inform the browser that the content following is a PNG image.
Since you create an image from a string, it doesn't have "an extension". It's just an image resource at this point. You can create an actual file from it using imagepng, imagejpeg or any of the other methods to save an image resource to a file. You decide the extension (and file name) at that stage yourself.
E.g.:
imagepng($image, 'path/to/file.png');
and where would i put in move_uploaded_file()?
You wouldn't, since you don't have an uploaded file, only a string.
Header is purely for the server to let the browser know "Oh hey this is a png image please render it so"
imagepng encodes it into the png format and "prints" to the output
imagedestroy frees the memory taken by the image resource.
If you need to force extension you can use mod_rewrite
Here's a sample couple lines from my .htaccess:
RewriteEngine on
RewriteRule images/000000/00FF00/newmyinfo.jpg images/newmyinfo.php?bgcolor=000000&color=00ff00 [L]
Hope this helps!
I've installed the GD Library on my Apache just now, and it seems that my script below doesn't work.
I'm trying to add a layer "play.png" to a youtube video thumbnail (http://img.youtube.com/vi/VIDEOID/default.jpg)
I've tried it with many different videoID's but the image doesn't load. There is a message that the graphic couldn't be opened because it contains errors.
I'm opening the file with postimage.php?v=7yV_JtFnIwo
http://img.youtube.com/vi/7yV_JtFnIwo/default.jpg opens correctly too...
Does anyone know where the issue could be?
Thanks in advance!
<?php
// The header line informs the server of what to send the output
// as. In this case, the server will see the output as a .png
// image and send it as such
header ("Content-type: image/png");
// Defining the background image. Optionally, a .jpg image could
// could be used using imagecreatefromjpeg, but I personally
// prefer working with png
$background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
// Defining the overlay image to be added or combined.
$insert = imagecreatefrompng("play.png");
// Select the first pixel of the overlay image (at 0,0) and use
// it's color to define the transparent color
imagecolortransparent($insert,imagecolorat($insert,0,0));
// Get overlay image width and hight for later use
$insert_x = imagesx($insert);
$insert_y = imagesy($insert);
// Combine the images into a single output image. Some people
// prefer to use the imagecopy() function, but more often than
// not, it sometimes does not work. (could be a bug)
imagecopymerge($background,$insert,0,0,0,0,$insert_x,$insert_y,100);
// Output the results as a png image, to be sent to viewer's
// browser. The results can be displayed within an HTML document
// as an image tag or background image for the document, tables,
// or anywhere an image URL may be acceptable.
imagepng($background,"",100);
?>
Do not close (avoid whitespaces or newslines) your script with ?> and use NULL instead "".
imagepng($background, NULL);
Then, in imagepng the quality parameter is between 0 and 9, as in http://it.php.net/manual/en/function.imagepng.php.