|
Posted by Rik on 07/17/06 22:47
Shawn Hamzee wrote:
>> Shawn Hamzee wrote:
>>
>> Don't toppost
>>
>>> <andy@andyh.co.uk> wrote:
>>>> <hamzee@sbcglobal.net.INVALID> wrote:
>>>>> I am having a problem with Image_Graph on php 5.1.4. I installed
>>>>> the package and all of its dependencies through pear installer
>>>>> without any hitches. Then I started to add some very simple code
>>>>> to create a graph in an existing php page. I added the code for
>>>>> the
>>>>> graph and added the elements of it plus some static data. However,
>>>>> when I browse the page, I get garbled data instead of the graph.
>>>>> Has anyone ever run into this problem. I am testing this on an xp
>>>>> test bed for now.
>>>>>
>>>>> I am thinking the fact that I'm getting garbled data means that
>>>>> the class creation has gone well; however rendering the graph is
>>>>> not happening for some reason.
>>>>
>>>> Have you output an appropriate Content-type header for the image
>>>> format you're
>>>> using?
>>> Actually, I just found out what is going on:
>>> In order to use image_graph, one cannot have both a graph and html
>>> (php generated or otherwise) on the same page.
>>>
>>> So in order to get around this, you'll have to save the graph to an
>>> image
>>> file (.png) and then echo a link in the script for it.
>>
>> Not really.
>> You could save the portion of your script that makes the graph
>> something like graphcreator.php, and echo that as the source for
>> your image. That way it will always stay up to date without any
>> constant diskwriting. Depending on how much people will see the
>> graph storing it and refreshing it periodically could save some
>> overhead. If it's just some statistics a few people will check out a
>> couple of times per week I'd keep it totally dynamic.
>>
>> Grtz,
>
> Really! Do you have a working example of your method? I'd be
> interested to see how the source of image can be a php file!
Not from the graph, but kind of a mirror effect with fade i recently worked
on:
(NOTE: by no means a finished product, it has some bugs in it!){
<?php
function transborder($image,$width,$red,$green,$blue){
$size = @getimagesize($image);
if($size===false) return false;
switch($size[2]){
case 1:
$image = imagecreatefromgif($image);
break;
case 2:
$image = imagecreatefromjpeg($image);
break;
case 3;
$image = imagecreatefrompng($image);
break;
default:
return false;
}
$x = $size[0];
$y = $size[1];
$new = imagecreatetruecolor($x, $y + $width - 1);
$back = imagecolorallocate($new,$red,$green,$blue);
imagefill($new,0,0,$back);
imagecopyresampled($new,$image,0,0,0,0,$x,$y,$x,$y);
for($i=1;$i<=$width;$i++){
imagecopyresampled($new,$image,0,$y+$i-1,0,$y-$i,$x,1,$x,1);
}
imagealphablending($new,true);
for($i=0;$i < $width;$i++){
//$trans = floor(0.03125 * $i * $i);
$trans = $i *2;
$color = imagecolorallocatealpha($new,$red,$green,$blue,$trans);
imageline($new,0,$y+$width-$i,$x,$y+$width-$i,$color);
}
header("Content-type: image/png");
imagepng($new);
imagedestroy($image);
imagedestroy($new);
}
/* now we can call the function with the following arguments:
string imagelocation (local or url)
int width mirrored surface
int red
int green
int blue*/
transborder('http://nl3.php.net/images/php.gif',50,255,240,240);
?>
The magic happens here:
header("Content-type: image/png");
imagepng($new);
Send the right header, and imagepng() (or imagejpeg() or imagegif()) will
output it directly. And never, ever, forget to imagedestroy() you images...
A good tip was to register a shutdown function:
function shutdown_func() {
global $image,$new;
if($img) imagedestroy($img);
if($new) imagedestroy($new);
}
register_shutdown_function("shutdown_func");
Allthough this does take an existing image, it creates a totally new one.
Creating an image with imagefilledpolygon(), imageline() and the like is
also possible. with those kind of functions the graph is actually made.
Check out the extensive GD library, a lot is possible (and even more
impossible unfortunately :-)
Grtz,
--
Rik Wasmus
[Back to original message]
|