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
Related
I'm working on an endpoint that returns a single image from the filesystem (like a user portrait). I use an ajax script for testing that processes the result of an endpoint and sticks it in a div. This raised the issue of constructing an image from an ajax response blob. The solutions I found on SO all assume that the image is being returned in base64 encoding. I got it working by changing the endpoint to output the image with base64 encoding, but this seems like an extra step, especially for iOS clients, which can understand the image being output by readfile.
Is this what people do in production environments? Doesn't conversion to base64 increase the size and put a load on the server? What advantages/disadvantages are there to each method? Is there another way that is even better?
header("Content-type: {$imginfo['mime']}");
// Output the file stream
readfile($filename);
// Output as base64
$type = pathinfo($filename, PATHINFO_EXTENSION);
$data = file_get_contents($filename);
base64_encode($data);
echo base64_encode($data);
I have a question about the application generate QR code image.
I have an application when clients click a button there will generate a QR code image, my way is store in the project library, then print <img> with the url to the screen. then clients can see it.
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
my code is bellow:
function generate_qrcode($url){
$filename = 'hante_qrcode.png';
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;
//generate QR code image
$o = QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
echo "<pre>";
print_r($o);
print_r('<img src="hante_qrcode.png">');
}
if there get mix, how to solve this problem?
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
yes
how to solve this problem?
there are two ways to solve this problem
you can provide unique name for every files like using timestamp using time() function or with user ID. cause as per you are passing parameters while generating qr code you need to store the file. without saving file also possible but in that case you can't configure pixel size and frame size. you can refer this for PHP QR code-Examples
don't store image on server and find some js to generate qr code directly from client side.
having a one demo for that check if you can use it
var qrcode = new QRCode("qrcode");
qrcode.makeCode('https://stackoverflow.com');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<div id="qrcode"></div>
Of course it will be overwritten.
Solution 1
Create unique filename for every image. This way you can save your images for use later. Another benefit of this, you don't have to create image again for same url.
$filename = md5($url) . ".png";
if(!file_exists($filename)){
$o = QRcode::png($url, $filename, ...);
}
echo '<img src="'.$filename.'">';
Solution 2
If you don't want to save images for disk space reasons you can serve image directly. In your code, user sends request to index.php and fetch image address as response. After then browser makes another request to get image. You can return image rather than returning html.
// image.php
// Still we want to give uniqe filename because we can get another request while one request is processing
$filename = md5(microtime) . "_qr.png";
$o = QRcode::png($url, $filename, ...);
$image = file_get_contents($filename);
// remove the file after stored in a variable
unlink($filename);
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($image));
echo $image;
// index.html
<img src="image.php?url=someurl">
Hi I have searched the web for 2 days but did not accomplish what I am looking for.
I have an apache server which will be accessed by 146 students. the user picks an angle from dropdown lets say 45 degress, then user clicks CALCULATE button. Then user clicks DIAGRAM button to see how the sine graph looks like.
Works like charm when i write the image to a file e.g: imagepng($img,"diagram.png");
Now the problem is that the diagram.png will always get overwritten by the last user. So for example if another user logs in and calculates the Sin 135. Both users will see Sine 135 because filename is hardcoded since there is conflict of filename.
I have searched the web on how to create the image dynamically instead of writing to a file and then reading the file. I have come across the following but not working:
base64_encode and decode
What would I have to do to my code of imagepng(...., ...) mentioned above to make use of base64 so I can actually draw the picture of already processed data. Let assume if I comment out the imagepng(..) code, then what do I replace it with. I hope I don't have to change my code a whole lot.
Please help
thanks
Amit
The filename argument to imagepng is optional. From the manual:
filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
You would just need to send a png header at the top of the script and you would get the image as output for that script.
It's hard to tell without seeing you code how it is structured
but if once the user submits the form all you do is show the image by itself, then you can do something like this.
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
If you embed the image into an html page as the result, then your best best would be to change the url of the image on the success page to something like this.
<img src="/path/to/file.php?deg=45" />
Then in the file.php
$deg = $_GET['deg'] + 0; // make sure it is a number
$img= function_render_graph($deg);
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
By using a GET request, rather then a POST request then the image will likely be cached by the browser, so it doesn't need to be rendered each time. (Given that you have a drop list of angles, there must be a limited number of graphs that can actually be drawn)
Draw_Resultant_Prism_Graph (parameters)
{
$img = imagecreatetruecolor(800,750);
....
....
...
the following lines captures the data from output buffer and displays on same screen
***some version of IE have some issues mostly the dumb terminals where IE update is ADMIN
***restricted
ob_start();
header("Content-type: image/jpeg");
imagepng($img);
$output = ob_get_contents();
ob_end_clean();
imagedestroy($img);
echo img src="data:image/jpeg;base64,'.base64_encode($output).'"
user tags around img above and semicolon af
}
Hi i write some code for images to be put side by side.I use Imagick library for that purpose.This is my code.
$im = new Imagick();
// session contain image path like upload/my.jpg
$im->readImage("http://localhost/wordpress3.5/".$_SESSION['imgname']);
$im->readImage("http://localhost/wordpress3.5/".$_SESSION['preimgurl']);
$im->resetIterator();
$combined = $im->appendImages(false);
/* Output the image */
$combined->setImageFormat("png");
header("Content-Type: image/png");
echo $combined;exit();
But the output is not what i suppose to be.this is output.I write this code under the plugin/ files.Also i want to save that image to directory like "localhost/wordpress/uplaod_pic/".
You should use an image tag with the src tags containing the direct or base64 version of the image.
First example would be direct:
echo '<img src="http://localhost/wordpress3.5/'.$_SESSION['imgname'].'">';
Second would be using base64 with the data URI scheme, PHP Example:
echo '<img src="data:image/png;base64,'.base64_encode($combined).'">';
I would recommend the first method, simply because it looks like a product display, the images are most likely public and hotlinking can be busted if you choose.
In both examples, since the image is inline, you wouldn't need to set the content-type to an image.
You should also place the correct mime type within the second version, either image/png, image/jpeg or others depending on the image.
Edit: To extend this:
$combined contains the image, so you want to save this, simple use:
file_put_contents('upload_pic/'.$time().'.png', $combined);
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.