Page 1 of 2 12 LastLast
Results 1 to 25 of 32
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    PHP Function
    Help! I have found a great php function that will resize images while keeping the image L x W ratio intact. Problem is I have never used a user defined function.

    The functions name is create_thumbnail($infile, $outfile, $maxh, $maxw)

    My plan was in include the function as a PHP include like this

    include ("makeimageSM.php")

    Then feed it the variables. When I did this I got the following errors

    Warning: Missing argument 1 for create_thumbnail() in....
    Warning: Missing argument 2 for create_thumbnail() in...
    Warning: Missing argument 3 for create_thumbnail() in...
    Warning: Missing argument 4 for create_thumbnail() in...
    Fatal error: Cannot redeclare create_thumbnail()...

    It looked pretty easy at first but I just can't figure out what I am doing wrong. Any help is greatly appreciated.
    Thanks,
    Doug

  2. #2
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Looks like you've got the function declared twice. Make sure you've only got it in a single place.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  3. #3
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    How?

    PHP Code:
              $path_parts pathinfo('$row[ImageURLsm]');
          
    $filename "".$path_parts['filename'].".".$path_parts['extension']."";    
          
    $path "wget -P imagecache .$row[ImageURLsm]";
          `
    $path`;
          
    $infile "imagecache/$filename";
          
    $outfile "imagecache/my$filename";
          include (
    "lib/makeimageSM.php"); 
    I think it might be because the file is not being retrieve. Man I hate the learning curve.
    Thanks,
    Doug

  4. #4
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    That error is just saying you're declaring the function twice, so it seems the include is working fine. Maybe you're calling the function wrong? If you proceed a function with the word "function" that's a function declaration. You don't use the word "function" when you call the function.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  5. #5
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    Let's see the function in makeimageSM.php
    ---
    This response was masterly crafted via the fingers of Patrick Allmond who believe you should StopDoingNothing starting today.
    ---
    Focus Consulting is where I roll | Follow @patrickallmond on Twitter
    Search Engine Marketing | Search Engine Optimization | Social Media | Online Video

  6. #6
    ABW Ambassador Boom or Bust's Avatar
    Join Date
    February 3rd, 2008
    Posts
    3,955
    Quote Originally Posted by itsupportnotes
    How?

    PHP Code:
              $path_parts pathinfo('$row[ImageURLsm]');
          
    $filename "".$path_parts['filename'].".".$path_parts['extension']."";    
          
    $path "wget -P imagecache .$row[ImageURLsm]";
          `
    $path`;
          
    $infile "imagecache/$filename";
          
    $outfile "imagecache/my$filename";
          include (
    "lib/makeimageSM.php"); 
    I think it might be because the file is not being retrieve. Man I hate the learning curve.
    I'm an ASP guy (not PHP) and may be totally wack, but in PHP can you place a variable along with a string within quotes when assigning another variable? -- $infile = "imagecache/$filename";
    ASP would be infile = "imagecache/" & filename



    X

  7. #7
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Ok, I'm a bit busy this morning But I should have time to go check what I have this afternoon. Thanks for the input.
    Thanks,
    Doug

  8. #8
    Lite On The Do, Heavy On The Nuts Donuts's Avatar
    Join Date
    January 18th, 2005
    Location
    Winter Park, FL
    Posts
    6,930
    No tips here on your function problem, we'd need to see it to best help you. I just wanted to say that GD (a library of image processing functions) is now fully integrated into PHP. Many hosts install the GD library as a standard practice, meaning you can just call existing functions (just like you'd call any other php functions) and manipulate your images. To see if your host has the GD library installed (it's very likely that they do), put phpinfo(); in a test page (see my code below) and inspect your php environment and installed libraries looking for the GD library among the others.

    If GD is installed, go here to learn more about it's functions:
    http://us2.php.net/gd

    To resize images, you likely would use either of these two functions:
    imagecopyresampled Copy and resize part of an image with resampling
    imagecopyresized Copy and resize part of an image

    Here's a standard test page I stick on my sites, just to check the server's time and the php environment when needed. Just save this code as a page called phpstuff.php (or whatever you'd like to name it) and visit the page in your browser.

    Code:
    <html>
    <head>
    <title>PHP Info</title>
    </head>
    <body>
    
    <p>server date and time
    <br>--------------------------------------------------------------------------------
    <br>
    <?php
    echo date("l dS \of F Y h:i:s A");
    ?>
    <br>
    
    <p>phpinfo()
    <br>--------------------------------------------------------------------------------
    <br>
    <?php
    phpinfo();
    ?>
    <br>
    
    </body>
    </html>
    When viewing this page, looking at the output of the phpinfo() function, scroll down and see if you see the gd library, it's likely right below the ftp library and above the gettext or hash libraries, about halfway down the page.

    Good luck!

  9. #9
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Still didn't get it. But I think part of the reason I am getting [bold]Fatal error: Cannot redeclare create_thumbnail()...[/bold] error is because I am using (or misusing) the function inside a while loop.

    So my question at this point would be how do I use a function in a while loop. I am pretty new to php so I think this will be a lesson on functions and I'm confused.

    What code should I post? The function, the section where I am trying to use the function or both.

    Thanks
    Thanks,
    Doug

  10. #10
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Let's see the part where you're trying to use the function. I believe that's where your problem is.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  11. #11
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Ok here is the section.


    PHP Code:
    while ($row mysql_fetch_array ($result)) { 
           
          
    $path_parts pathinfo('$row[ImageURLsm]');
          
    $filename "".$path_parts['filename'].".".$path_parts['extension']."";    
          
    $path "wget -P imagecache .$row[ImageURLsm]";
          `
    $path`;
          
    $infile "imagecache/$filename";
          
    $outfile "imagecache/my$filename";
          include (
    "lib/makeimageSM.php");    ///  This is the function 
          
    print ("<td class=ProdBoxSM><img src=\"../../$outfile\" alt=\"$row[Gender] $row[Name]\"><br><a href=\"http://www.footwearfare.com/item2/".$row[Sku]."/\">$row[Name]</a><br><br></td>\n"); 
        
    $cellnumber=$cellnumber 1;
            if (
    $cellnumber 3) {
            print (
    "</tr><tr align=center valign=top>\n");
            
    $cellnumber 1;
            }

    Let me know what you think.
    Thanks,
    Doug

  12. #12
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    It looks like you're including the file containing the function, but you aren't calling it. You call a function like you would any other PHP function, $var = my_function($input1, $input2). The error you're getting still indicates that you've declared the function twice though. Check inside makeimageSM.php to make sure you don't have "function create_thumbnail" in there twice.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  13. #13
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Ok that makes sense. I thought I was calling it correctly earlier though. But I'll double check. I'm headed home in like 15 minutes so I'll work on it there.

    Thanks!
    Thanks,
    Doug

  14. #14
    Affiliate Manager cbsturg's Avatar
    Join Date
    January 24th, 2007
    Location
    Lima OH
    Posts
    753
    A nifty little function that is quite helpful is the include_once protocol. As the name suggests, it will include the requested file exactly one time (helpful if you include multiple pages that also include pages and you only want to declare functions once).

    While I recommend cleaning up your code a bit and perhaps pulling the include function out of the loop if it's not necessary, this should patch your problem:

    PHP Code:
    include_once "lib/makeimageSM.php"
    Note that commands like include, include_once, echo, etc. are not true functions and do not require enclosing parameters in ()'s.

    Shoot me an email with questions: chris _at_ rizyn.com
    Chris Sturgill
    "All my life I've had one dream, to achieve my many goals." - H. Simpson

  15. #15
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Good call Chris. I completely overlooked the loop. That's definitely the cause of the problem. Always best to do your includes in a single place rather than right in the middle of your script. I usually do all mine in a single common.php file that includes all the necessary dependencies up front.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  16. #16
    Affiliate Manager cbsturg's Avatar
    Join Date
    January 24th, 2007
    Location
    Lima OH
    Posts
    753
    Agreed. I usually put all of my includes right at the top of the page - typically having each page require_once a globals.php script that defines all of my DB connection protocols, etc. Having includes scattered throughout the script gets very confusing to debug. The only exception to this is if I'm including a file that generates displayed content (as in views in the MVC structure). But files that handle logic aspects of the script are all included before any other lines of code.

    I'll tell ya' Scott, I don't know that I've ever received a compliment from a Code Ninja before. This is a landmark day in my professional career!
    Chris Sturgill
    "All my life I've had one dream, to achieve my many goals." - H. Simpson

  17. #17
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Ok here is an update. I think the only think am having a problem with is unlinking the file. Here is a section of code.

    PHP Code:
    $MyImageHeight "100";
    $MyImageWidth "100";
    require_once(
    'imagecache/makeimageSM.php');
    while (
    $row mysql_fetch_array ($result)) { 
           
    $imagename basename($row[ImageURLsm]);
           
    $MyInfile "$imagename";
           
    $MyOutfile "$imagename"
           
    $url $row[ImageURLsm];
           
    $path "wget -P imagecache $url";
          `
    $path`;
           
    unlink("imagecache/$imagename");
           
    $image create_thumbnail($imagename,$imagename,MyImageWidth,MyImageHeight); 
    The only error I receive is:

    [bold]Warning:[/bold] Cannot open file: 3-661094_blackred_sm.jpg in .......public_html/imagecache/makeimageSM.php on line 12

    makeimageSM.php is a function to resize an image and is located in the imagecache folder. The the image is being transferred to my server but like I said I think I am doing something wrong with the unlink function. Any help is greatly appreciated.
    Thanks,
    Doug

  18. #18
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    Not sure without looking at it all, but you may want to try this....
    Code:
    unlink("imagecache" . "/" . $imagename);

  19. #19
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    SMB that was a no go so I did some more reading the unlink() function is used to delete a file. I think I just need to remove a file lock or something. The merchant thumbnail is being transferred to my server but the image resizing function is not able to open the image.
    Thanks,
    Doug

  20. #20
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    You're deleting the image before you have a chance to resize it. You should delete after you've resized it.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  21. #21
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Yeah I saw that but the unlink() was not deleteing the image. I commented it out after reading the unlink() is for deleteing files and still thf function could not open the file.
    Thanks,
    Doug

  22. #22
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    If you're using datafeed image URLs you won't always get a result for basename() because not all merchants give you the direct URL to an image file. Sometimes they'll give you the link to a script that returns the image. In which case you'll need to automatically generate a random file name for each image and store that random name in a database associated with the datafeed URL.

    In this particular case I would check your $imagename variable to see what it contains. It may not contain anything at all due to the reason I just stated.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  23. #23
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    I printed the $imagename and it had the name a of the file.
    Thanks,
    Doug

  24. #24
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Looks like you're storing the image in imagecache/ but when you send the path to create_thumbnail() you aren't indicating that its inside imagecache/

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  25. #25
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Ok I'll make some changes there. I thought I could just feed the function the variable because php script is in the imagecache folder
    Thanks,
    Doug

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Search Function
    By AdamlCasey in forum PopShops
    Replies: 7
    Last Post: January 19th, 2010, 10:40 AM
  2. PHP question - include(file.php) with variables
    By teezone in forum Programming / Datafeeds / Tools
    Replies: 8
    Last Post: September 4th, 2009, 04:54 PM
  3. Can't send email via php mail function
    By oscar in forum Programming / Datafeeds / Tools
    Replies: 9
    Last Post: August 20th, 2004, 11:25 PM
  4. PHP search function help
    By Affiliate Ian in forum Programming / Datafeeds / Tools
    Replies: 8
    Last Post: December 16th, 2003, 09:27 AM
  5. The #@!%&* Search Function
    By Cedric in forum Rakuten LinkShare - LS
    Replies: 5
    Last Post: September 17th, 2002, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •