Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Class outputs image, I need to save the image instead
Old 01-08-2012, 01:37 PM Class outputs image, I need to save the image instead
Extreme Talker

Posts: 176
Trades: 0
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($url0);
    
$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.
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-08-2012, 02:29 PM Re: Class outputs image, I need to save the image instead
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-08-2012, 04:16 PM Re: Class outputs image, I need to save the image instead
Extreme Talker

Posts: 176
Trades: 0
Quote:
Originally Posted by chrishirst View Post
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
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 01-08-2012, 05:21 PM Re: Class outputs image, I need to save the image instead
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
PHP Code:
file_put_contents('path/to/my-file.png'$fv); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-08-2012, 05:22 PM Re: Class outputs image, I need to save the image instead
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
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!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 01-08-2012, 05:54 PM Re: Class outputs image, I need to save the image instead
Extreme Talker

Posts: 176
Trades: 0
thanks guys, that's just what I needed, works, and the fopen wasn't necessary
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 01-08-2012, 05:56 PM Re: Class outputs image, I need to save the image instead
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Physicsguy View Post
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-08-2012, 06:02 PM Re: Class outputs image, I need to save the image instead
Extreme Talker

Posts: 176
Trades: 0
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?
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 01-08-2012, 06:23 PM Re: Class outputs image, I need to save the image instead
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
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.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 01-08-2012 at 06:28 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 01-08-2012, 06:31 PM Re: Class outputs image, I need to save the image instead
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-08-2012, 08:14 PM Re: Class outputs image, I need to save the image instead
Extreme Talker

Posts: 176
Trades: 0
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..
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 01-08-2012, 08:51 PM Re: Class outputs image, I need to save the image instead
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
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.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 01-08-2012 at 08:53 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Class outputs image, I need to save the image instead
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50383 seconds with 12 queries