Libchart (PHP chart) - same image? - php

I am using LibChart. It works fine, however, i have an issue. I'm not sure it is directly linked to the library, more of a general php/image thing that i am maybe missing.
Thing is, the image doesn't update itself (the image that contains the graph) unless i re-upload the PHP file that draws it. This is how the graph is made, using LibChart
$chart->render("generated/demo4.png");
That renders it into that file, and i display it using
<img alt="Line chart" src="generated/demo4.png" style="border: 1px solid gray; float: right;"/>
It works great, but only the first time it is drawn. It won't redraw the image unless i re-upload the file that draws it. That is kinda bad, since it draws data from a database, and when that changes it needs to be reflected in the graph.
What might be the issue?
How can i redraw the image without re-uploading the file?

Which internet browser are you using?
Some of them (for example IE) are storing images in internal memory (for faster rendering the web site). You can save the rendered graph in file, then load it again in variable and finaly this variable show as base64 data.
$path = "tmp/graph.png";
$chart->render($path);
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
return "<img src=\"data:image/".$type.";base64,".base64_encode($data)."\" />";
It is a bit weird, but it worked for me. Now I am looking for some better solution.

Related

how to create image for php file output

I want to create collage of photos with PHP. I created a test code on my localhost as follows:
<style type="text/css">
body{background:url('images/Winter.jpg'); }
#collage:after{content:"";clear:both;display:block;}
}
</style>
<?php
ob_start();
$dir = "images";
if($fp = opendir($dir))
{
while($file = readdir($fp))
{
if('jpg'==(pathinfo($file, PATHINFO_EXTENSION)))
{
$style = "style='float:left; -webkit-transform:rotate(".mt_rand(2, 30)."deg); border:solid 5px #eee;'";
$ht = "height='".mt_rand(100, 300)."'";
echo "<div class='img_div' $style>";
echo "<img src='$dir/$file' $ht >";
echo "<div style='background:#eee;font-size:20px;'>hi</div>";
echo '</div>';
}
}
}
closedir($fp);
?>
It generated the output which I want but now I want the user to be able to download it as an image file. How do I do it?
If you want that you will have to create a new blank image using something like php's GD library and position your images into that instead of printing and positioning them using html/css
(I believe I read somewhere that in the future you will be able to display html/css on a html5 canvas, if that's true you'd be able to extract an image using that, but for now I don't think that will be possible)
Short of using the new HTML5 canvas feature, that will let the browser do all the work of merging the mosaic into one single image for you (but is it supported by many browsers yet ? I honestly don't know.),
you can also create that image on the server's side, using php's GD library. That was, I think, the only way before HTML5 and may still be the best way for a while (as I said, I don't know exactly what HTML5 implementations are worth to this day)
A good place to start would be there: http://php.net/manual/en/ref.image.php (read the many examples by visitors, on that page and on the function-specific pages, they can be a valuable education).
Now, this method has one inconvenient (in addition to you having to do the whole work): it taxes your server's CPU and slows down its response. That's ok if you have only a few visitors at a time. But otherwise, your collage should be pre-calculated once and for all, so the server doesn't have to redo the job for each visit.

Google Contact API Picture Data, returns data, but I dont know how to display it with PHP

I am using OAuth 1.0, I am getting the contacts just fine. Next I fetch an image using the link that is in the contact info. If the user has an image the request works and return a bunch of data. When I echo it I get something like this:
"" ÿÀ``"ÿÄÿÄ<!"12A#Qq‘BRa3‚’±Ñðbrƒ¡Â$%¢³ÿÄÿÄ#!1Q"AaqÿÚ?ôÌìç™pzõWoÂ~vïD±èÐvQNl/žåÐìMCÀƒÚüü¿ ÔLß÷&‹ðKš×aG¥=Ë È
Which I am assuming is the data for the image. Now that I have this, I cant figure out a way to display it.
here is an example of what I am doing:
$consumer = new OAuth($key,$secret);
$image = $consumer->fetch($theImageUrl);
return $image;
The request is working, theres no 400,401, or 404 errors.
I tried doing this already:
<img src="/art/transperantimage.png" style='background: #fff url(data:image/png;base64,<?=$image ?>) repeat-x bottom'/>
and I just ended up with more data jibberish.
I guess my question is how the heck to I display this data?
Per the documentation, this request returns the bytes of the image. So you have three options:
Write a PHP script that outputs those bytes (and only those bytes) directly to the client using the appropriate Content-Type header, which is what #Prowla has in mind. Then point to this script in your <img src="...">.
Write the bytes to a publicly-accessible file on your web server, and then put the URL of that file in your <img src="...">.
Use a data URI, which you seem to have attempted, but forgot that you need to Base64 encode the data first, e.g.:
<img src="data:image/jpeg;base64,<?php echo base64_encode( $image ); ?>" />
While #3 is looks the simplest, #2 is probably the best solution since the image likely doesn't change very often so there's no sense requesting it from the API every single time someone reloads your page. You can just write the image to a file if the file doesn't already exist, and then periodically (e.g. every day or week) check to see if there is a new image and if there is, overwrite the old one.
Before printing out the image set the header content type to something like (depending on the data type):
header('Content-Type: image/jpeg');

Can I filter an image client side, Not server side, using PHP GD Library?

I use Soundcloud for my tracks.
I'm using their jquery player to place a widget on my new site, as you can see on the top right:
The problem is, the waveform Souncloud provides is a one colour only deal:
My goal: To change this waveform PNG from curent colour to black, but client side.
I know I can change things using PHPs GD library, and I've done this successfully with a test image on my server using this code:
http://php.net/manual/en/function.imagefilter.php
(Search for "IMG_FILTER_BRIGHTNESS")
<?php
$im = imagecreatefrompng('hello.png');
if($im && imagefilter($im, IMG_FILTER_BRIGHTNESS, -255))
{
echo 'Image brightness changed.';
imagepng($im, 'hello.png');
imagedestroy($im);
}
else
{
echo 'Image brightness change failed.';
}
?>
It works perfectly! BUT
It changes the actual image on my server! Pretty cool, but not possible...
Obviously I cant change the image on Soundcloud's server (all the data, images, music comes from there API)
So What I'm looking for is a way were I can change the colour of the PNG client side, on the fly. I have a lot of tracks on there, so basically each time the user clicks next or previous, the waveform loads in and before it does it needs to change that image's colour :-?
Is this possible?
To see the player in action on my test site
(the styling on that one is old, by the way, but it's functionality is correct)
http://marckremers.com/2011/
NB the entire site does not work beyond what you see there. Still a WIP.
Thanks so much
This would have to be done client-side using Javascript, either a fully JS solution or one that uses AJAX to send it to PHP, then receives the final image.
You can try the Pixastic JS library:
http://www.pixastic.com/lib/docs/#intro
If that doesn't work, I would use jQuery to read the image, send it to a PHP script using JSRS/AJAX and then replace it on the page.

Image SRC From PHP Script on IIS - Not Displaying Consistently

Last week I converted my page img src values from pointing at image files to using a PHP script to serve up the images. The primary reason was to accommodate both files and database BLOBs as the actual source.
Now when a user goes to a page, sometimes images show and sometimes not. If not, and the page is refreshed\reloaded, then the image appears. When images do not appear, sometimes it is an image the user has already accessed previously today.
I am stumped.
Here is the img tag:
<img src="../somedir/image_script.php?i=1234">
The image_script.php file figures out where to get the image from, then finishes up with:
header("Content-type: image/jpeg");
if($from_db){
print $image_blob;
} else {
$im = imagecreatefromjpeg($image_file);
imagejpeg($im,null,100);
imagedestroy($im)
}
I am using PHP 5.2.8 on IIS 6 using FastCGI. There are no cache headers on the image_script.php file nor on the directory it is in. Currently 99.9% of the images are file based, so I do not know if there is a difference in result between db-based and file-based images. When I go directly to image_script.php in my browser it returns the requested image (i=????) 100% of the time.
a> Any clue as to why the hit and miss with images being displayed? and,
b> what would be a proper way to actually cache the images served up by the PHP script? (they are very static)
Scott
Hmm. Can't tell for sure, but maybe your imagecreatefromjpeg is occasionally running out of memory? In that case, you'd serve an error message out as JPEG data and never see it, right?
Incidentally, wouldn't just grabbing the image file in as a string and shovelling it out without going through imagecreatefromjpeg/imagejpeg/imagedestroy be more efficient? It looks like you're reading a JPEG file, creating an internal PHP memory image from it, then reconverting it to a JPEG (at hefty 100% quality) then serving that data out, when you could simply read the JPEG file data in and print it, like you do from the database.
What happens if you do, say...
...
} else {
header ('Content-length: ' .filesize($image_file));
readfile ($image_file);
}

How to load dynamic image w/php gd library, w/o saving it on server or having src="script.php"?

I would like to generate a dynamic image from a script, and then have it load to the browser without being persistent on the server.
However, I cannot call this by setting the image's src="script.php", since that would require running the script that just generated the page and its data all over again, just to get the final data that will generate the graph.
Is there a way to do this that is similar to setting image's src="script.php", but which is called from within another script, and just sends the image without saving it? I need access to the data that is used in the generation of the markup, in order to create this dynamic image.
Or, if not, what is the easiest way to destroy the image once the page is loaded? a quick ajax call?
Is there any way to cache certain data for some limited time frame in order for it to be available to some other script?
Any ideas would be greatly appreciated, as I'm having a really hard time finding the right solution to this...
Thanks!
You can inline the image into a <img> tag if you need to.
Like
<?php
$final_image_data; // Your image data, generated by GD
$base64_data = base64_encode($final_image_data);
echo "<img src=\"data:image/png;base64,{$base64_data}\" ... />";
?>
That should work on all modern browsers, and IE8. Doesn't work well with some email clients tho (Outlook, for one).
Also, another solution I found is to store the image in a session variable which is then called from a php script in the image tag. This would allow a user specific image to be served, and then removed from memory by the script... This also avoids messy img src="" tags...
Hopefully that is helpful to someone.
Use a rewrite rule.
RewriteRule ^magicimage.jpg$ /myscript.php
Then simply echo your image data from gd, instead of writing it to disk -- which is as simple as not providing a filename to the appropriate image*() function
myscript.php
<?php
$im = imagecreatetruecolor($w, $h);
//...do gd stuff...
header('Content-type: image/jpeg');
//this outputs the content directly to the browser
//without creating a temporary file or anything
imagejpeg($im);
And finally, utilize the above
display.php
<img src="magicimage.jpg">

Categories