Class outputs image, I need to save the image instead
01-08-2012, 01:37 PM
|
Class outputs image, I need to save the image instead
|
Posts: 176
|
I'm using this class to grab favicons: http://www.controlstyle.com/articles...t/php-favicon/
here's my code:
PHP Code:
if (file_exists($filename)) { echo 'yes'; } else { include('favicon.php'); $favicon = new favicon($url, 0); $fv = $favicon->get_output_data();
if ($fv!=='') { header('Content-type: image/png'); echo $fv; } }
Right now it says yes if I already have the icon saved (in a favicon folder, as 'domaincom.png'), and if I don't have it, it outputs it. How can I change it from outputting the file, to instead just saving it into the favicon folder (under $filename) and giving me a confirmation that it worked.
Also, sometimes the class outputs just a white square, not sure if anyone has a solution for that.
|
|
|
|
01-08-2012, 02:29 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
|
Output the path and filename wrapped in a image element.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
01-08-2012, 04:16 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 176
|
Quote:
Originally Posted by chrishirst
Output the path and filename wrapped in a image element.
|
It already outputs the image, I need to know how to use php to move the file that's created
instead of
PHP Code:
header('Content-type: image/png'); echo $fv;
I need something that takes the data in $fv and saves it to $filename
|
|
|
|
01-08-2012, 05:21 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
PHP Code:
file_put_contents('path/to/my-file.png', $fv);
|
|
|
|
01-08-2012, 05:22 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
So you need to save it?
I suppose file_put_contents could help. Just give it the image data and it'll save it as whatever you want, wherever you tell it to. The file needs to exists first, I think, so a quick fopen() should work.
EDIT: Dang it Matt, beat me again!
|
|
|
|
01-08-2012, 05:54 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 176
|
thanks guys, that's just what I needed, works, and the fopen wasn't necessary
|
|
|
|
01-08-2012, 05:56 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by Physicsguy
The file needs to exists first, I think, so a quick fopen() should work.
|
file_put_contents will create the file if it does not exist. If the file does exist it will be overwritten.
|
|
|
|
01-08-2012, 06:02 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 176
|
Oh, I just remembered that it outputs a white square if there's a problem getting it, is there a quick way to check if it's an all white square? Or do I have to change some code in the class to make it give an error?
|
|
|
|
01-08-2012, 06:23 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Well, if you have the code that sets it to be a white square, then set a variable like $not_found=1 and use it later in the code.
But if you can't do that, then you'd have to look at PHP's GD manual, loop through each pixel in the image, and check if they are all 255/255/255 (white in RGB).
Looping through each pixel can be done like this:
PHP Code:
$file = $fv; //Assuming $fv is a URL
list($width,$height)=getimagesize($file);
for($y=0;$y<$height;$y++){ for($x=0;$x<$width;$x++){ $rgb=imagecolorat($im,$x,$y); $r=($rgb>>16)&0xFF; $g=($rgb>>8)&0xFF; $b=$rgb&0xFF;
echo $r.'/'.$g.'/'.$b;
} }
Not tested, but should work.
Last edited by Physicsguy; 01-08-2012 at 06:28 PM..
|
|
|
|
01-08-2012, 06:31 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
You may want to make some changes so it doesn't just create a white square if there's an error. Maybe have it throw an exception or just return false. Generating a white square and then checking if it's a white square seems like the long way around.
|
|
|
|
01-08-2012, 08:14 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 176
|
So I found where in the class it's creating the white square, seems easy enough to throw a variable in, but I think I'm doing it wrong. Is there something I need to do to access variables created in a class?
PHP Code:
if ($error != 'yes') { file_put_contents($filename, $fv); echo 'image saved <img src="'.$filename.'"/>'; } else { echo 'sorry, errored'; }
I tried changing it to return false, but now it returns a broken image instead (and still thinks it got it).
Last edited by Skeddles; 01-08-2012 at 08:23 PM..
|
|
|
|
01-08-2012, 08:51 PM
|
Re: Class outputs image, I need to save the image instead
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Yeah, when you create a variable in the class, it'll only be useable in the class. Just use something like $GLOBALS['error'] = true; to add it to the GLOBALS superglobal array, which can be used anywhere in your code. To call it, use the same syntax: echo $GLOBALS['error'];
By the way, it's unstable and bad practice to use 'yes' and 'no' in place of true and false. This is because both 'yes' and 'no' are set strings, and mean the same thing in PHP.
Last edited by Physicsguy; 01-08-2012 at 08:53 PM..
|
|
|
|
|
« Reply to Class outputs image, I need to save the image instead
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|