Q:

Find if a file exists, if not, revert to original path

I've written a function to take an image path, add some text to the end of it, then join it back together. I'm trying to write an if else branch to test if the file exists after I've appended the new text to the image. 
For example, if I request the image path:
http://example.com/image1.jpg

It could be modified to this:
http://example.com/image1-150x100.jpg

The function is intended for WordPress, so takes the $post->ID argument, the custom field name, width and height of the intended target.
Here is the function:
function get_galleria_image ( $post_id, $customFieldName, $width, $height ) {

    $imagePath = get_post_meta($post_id, $customFieldName, true);

    $splitImage = explode('.', $imagePath);

    $count = count($splitImage);

    $firstHalf =  array_slice($splitImage, 0, $count-1);

    $secondHalf = array_slice($splitImage, $count-1);

    $secondHalf[0] = '-' . $width . 'x' . $height . '.' . $secondHalf[0];

    $firstHalfJoined = implode('.', $firstHalf);

    $completedString = $firstHalfJoined . $secondHalf[0];

    // if the filename with the joined string does not exist, return the original image
    if (file_exists($completedString)){
        return $completedString;
    } else {
        return $imagePath;
    }
}

I know this is crude, so any ideas to condense this code and make it "better" is welcomed. What I'm finding is that the function always returns the original image path. Can file_exists() take an absolute path like "http://example.com/image1.jpg', or will it only accept a root-style absolute path, like '/path/to/file/image1.jpg'?

A:

file_exists wont accept url path. Try this for url path to check if the given url is an image or not
function isImage($url)
  {
     $params = array('http' => array(
                  'method' => 'HEAD'
               ));
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) 
        return false;  // Problem with url

    $meta = stream_get_meta_data($fp);
    if ($meta === false)
    {
        fclose($fp);
        return false;  // Problem reading data from url
    }

    $wrapper_data = $meta["wrapper_data"];
    if(is_array($wrapper_data)){
      foreach(array_keys($wrapper_data) as $hh){
          if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19 
          {
            fclose($fp);
            return true;
          }
      }
    }

    fclose($fp);
    return false;
  }

