Note that there are some explanatory texts on larger screens.

plurals
  1. POFind if a file exists, if not, revert to original path
    primarykey
    data
    text
    <p>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. </p> <p>For example, if I request the image path:</p> <pre><code>http://example.com/image1.jpg </code></pre> <p>It could be modified to this:</p> <pre><code>http://example.com/image1-150x100.jpg </code></pre> <p>The function is intended for WordPress, so takes the $post->ID argument, the custom field name, width and height of the intended target.</p> <p>Here is the function:</p> <pre><code>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; } } </code></pre> <p>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'?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload