Check if valid image

Discussion in 'PHP' started by Fracisc, Mar 6, 2014.

  1. #1
    I have this:
    if (getimagesize($x->image_url) !== false) {
        echo "img";
    }
    PHP:
    The problem is that if the image url is badly formatted I am getting this:
    Warning: getimagesize(http:///images/crap.jpg) [function.getimagesize]: failed to open stream: operation failed in bla...
    Code (markup):
    Can you suggest a better code?

    Thanks!
     
    Fracisc, Mar 6, 2014 IP
  2. ObliqueSolutions

    ObliqueSolutions Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #2
    Can you show how $x->image_url is being set?
     
    ObliqueSolutions, Mar 6, 2014 IP
  3. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #3
    It is read from a products feed. Some URLs are valid, some are not. Some have bad formating, some return a 404. I need to check for all these errors..
     
    Fracisc, Mar 6, 2014 IP
  4. ObliqueSolutions

    ObliqueSolutions Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #4
    if (filter_var($url, FILTER_VALIDATE_URL)) { /* then set the variable */ }
    Code (markup):
     
    ObliqueSolutions, Mar 6, 2014 IP
  5. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #5
    HuggyStudios, Mar 6, 2014 IP
  6. ObliqueSolutions

    ObliqueSolutions Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #6
    Also you can just:
    if (@getimagesize($x->image_url)) { echo "img"; }
    Code (markup):
     
    ObliqueSolutions, Mar 6, 2014 IP
    Fracisc likes this.
  7. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #7
    I don't see how suppressing the error actually fixes anything?
     
    HuggyStudios, Mar 6, 2014 IP
  8. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #8
    I can't fix the missing image, the problem is from an advertisers feed. All I can do is to prevent showing a missing image.
     
    Fracisc, Mar 6, 2014 IP
  9. ObliqueSolutions

    ObliqueSolutions Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #9
    That's not an error.
     
    ObliqueSolutions, Mar 6, 2014 IP
  10. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #10
    http:///images/crap.jpg
    Code (markup):
    is incorrectly formated as there is no domain. so either add your domain
    http://yourdomain.com/images/crap.jpg
    Code (markup):
    or remove the http://
    /images/crap.jpg
    Code (markup):
     
    stephan2307, Mar 6, 2014 IP
  11. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #11
    So you think supressing notices, warnings and errors is a good tip for developing? Where did you learn that?
     
    HuggyStudios, Mar 6, 2014 IP
  12. ObliqueSolutions

    ObliqueSolutions Greenhorn

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #12
    Well there are various ways to fix your error, but I require a context in
    No, it's never a good tip, as much as enabling notices and warnings in production, but in this issue, it would solve the problem user has encored.
     
    ObliqueSolutions, Mar 6, 2014 IP
  13. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #13
    it wouldn't solve the problem. It would only hide the problem.

    What you need to do is to add some code around your if condition and check what $x->image_url actually contains. And based on the content you either run the if or not. You could also wrap the whole thing in a try catch code.
     
    stephan2307, Mar 6, 2014 IP
  14. terrymason

    terrymason Well-Known Member

    Messages:
    727
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    145
    #14
    I use this on my wordpress sites to show product datafeeds (from CJ). I use curl to go get the image - if curl fails then I use a local image. I cache the results of this query for a couple of days, since all these requests to external sites can slow yours down. Hope it helps, I struggled with this for some time.

    productImageCheck simply turns the check on or off.

    // begin check for broken images
    get_option('productImageCheck');
    $productImage = $CurrentProduct->{'image-url'};
    if (empty($productImage)) {
        $productImage = CJPLUGINURL . "/images/noimage.gif";
    } elseif(get_option('productImageCheck')) {
        $chProductImage = curl_init();
        curl_setopt($chProductImage, CURLOPT_URL,$productImage);
        // don't download content
        curl_setopt($chProductImage, CURLOPT_NOBODY, 1);
        curl_setopt($chProductImage, CURLOPT_FAILONERROR, 1);
        curl_setopt($chProductImage, CURLOPT_RETURNTRANSFER, 1);
        if(curl_exec($chProductImage )!==FALSE) {
        } else {
            $productImage = CJPLUGINURL . "/images/noimage.gif";
        }
    } //end  check for broken images
    PHP:
     
    terrymason, Mar 8, 2014 IP
    Fracisc likes this.
  15. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #15
    It is almost as bad as an error. There is a purpose for PHP showing Warnings and Notices. They are not to be ignored and hidden, but are there to be fixed.
     
    DomainerHelper, Mar 8, 2014 IP
    PoPSiCLe and deathshadow like this.
  16. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #16
    ... and I would hit the like button a hundred more times if it let me.
     
    deathshadow, Mar 8, 2014 IP