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

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');

Related

Whether there generate QR code in my project directory will get mix when multi people using it?

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">

Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension

I want to Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension (like imageMagick). I searched everywhere, but I couldn't find a perfect solution. I tried below code (got from link)
function get_dpi($filename){
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,0,4);
return array(hexdec($x),hexdec($y));
}
But I am not getting the correct Horizontal and vertical DPI. For example, I used an image with 96dpi and 96dpi, but I got (100,100).And this function is only for JPEG file formats.
The DPI of an image is usually a matter of fiction. Rarely is an image created where the physical dimensions of the final rendering actually matter (as far as the image itself is concerned). That said, the DPI information is stored in the EXIF data of a JPEG so you can read it from there with the built-in PHP function:
<?php
$filename = "/Users/quentin/Dropbox/Camera Uploads/2016-03-30 21.01.09.jpg";
$exif = exif_read_data($filename);
?>
DPI is <?php echo $exif["XResolution"] ?> by <?php echo $exif["YResolution"] ?>

PHP Binary Image Data

I'm working with some binary image data given to me by another developer and trying to figure out how to display the images on the screen. The binary images all look like this:
0xFFD8FFE000104A46494...[TRUNCATED]...F6145145690D8E4ACFDE3FFFD9
I've tried the following PHP code to get the image to save:
$data = '0xFFD8FFE000104A46494...F6145145690D8E4ACFDE3FFFD9'; //full data entered here
$string = pack('H*', $data);
$result = imagecreatefromstring($string);
imagejpeg($result, 'test.jpg');
This saves an image called test.jpg, but it is a jumbled image and not the actual image it should be.
I also tried using base64 like this:
<img src="data:image/jpeg;base64,<?php echo base64_encode($data) ?>">
which also did not work. Any suggestions would be appreciated, thanks!

Getting image from XML 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

Converting all pages of a multi page PDF into an image with PHP causes a duplicate result in the image

So I am using imagick to convert a PDF into an image. All works well for single page PDFs but I ran into a snag with a multi page pdf.
I found an example on how to deal with multiple pages here: http://php.net/manual/en/function.imagick-appendimages.php so I took that code:
$im = new Imagick($src);
$im->readImage($src);
$im->resetIterator();
$ima = $im->appendImages(true);
$ima->setImageFormat('jpg');
header("Content-Type: image/jpeg");
print $ima;
This works in that it produces an image with each page of the pdf the problem is that the PDF is displayed twice in the image. So, a three page PDF is displayed as a single image of 6 pages.
Here is an example PDF
and the resulting image created with the above code.
What am I doing that is causing this to happen?
Resolution:
Based on tandu's answer this did the trick
$im = new Imagick($src);
$im->resetIterator();
$ima = $im->appendImages(true);
$ima->setImageFormat('jpg');
header("Content-Type: image/jpeg");
print $ima;
The Imagick constructor loads the image from the file you provide. When you run readImage(), it loads them again. You only need one of those two.

Categories