Check if file is a png


#1

How can I check if a file is a png? I’ve tried to use extension(), however, it is outputting something like ‘31ff35d1fe2a8941ac900f29fcea56d5.jpg’. Ideally, I would like to add ‘pdf’ to the classlist. Any help is greatly appreciated.

<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->thumbnail()): ?>
    <div class="<?= $thumbnail->extension(); ?>">

    </div>
    <?php endif; ?>
<?php endforeach; ?>

#2

I normally do something like this… you can repeat it for other extensions…

<?php foreach($labs->filterBy('extension', 'pdf') as $pdf): ?>
<a href="<?= $pdf->url() ?>"><i class="fa fa-file-pdf-o"></i><?= $pdf->filename() ?> (<?= $pdf->niceSize() ?>)</a> 
<?php endforeach ?>

I think yours isn’t working since its missing file()

<?= $thumbnail->file()->extension(); ?>

It’s not clear from your code example how you are finding the files to work with. Where and how are you setting the $labs variable?


#3

Some notes here:

  • Not quite clear if you want to check if the file is a png or a pdf, a thumbnail doesn’t make sense for a PDF.
  • There is no thumbnail() file method, it is called thumb() (thumbnail() is a method of the Simpleimage class, but not of the file class)

You probably mean original() not file().

The above code should look like this:

<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->resize(200)): ?>
    <div class="<?= $thumbnail->original()->extension(); ?>">
blah blah
    </div>
    <?php endif; ?>
<?php endforeach; ?>

But if you want to filter by a particular extension, you have to ideally use a filter like suggested above, or use an if statement.


#4

Apologies, that was badly worded. I do not want to filter by file type, just to output the file type (.png, .gif, .jpg, etc) in the containing div's classlist, that way, I can change the container CSS based on the file type.

The more complete code is below, within the snippet responsive-image toFile() extension is called to create different size thumbnails. I added original() and the filename is still output rather than the extension type. Is what I’m trying to do possible using extension()?

<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->thumbnail()): ?>
    <div class="lab-thumbnail-cnt <?= $thumbnail->original()->extension(); ?>">
        <div class="lab-thumbnail responsive-image">
            <?php snippet('responsive-image', array('field' => $thumbnail)); ?>
        </div>
    </div>
    <?php endif; ?>
<?php endforeach; ?>


#5

What is thumbnail(), a field in your page? Assuming that $labs is a collection of pages? Then that field only returns a filename, not a file object.

In that case, get the file like this:

<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->thumbnail()->toFile()): ?>
    <div class="lab-thumbnail-cnt <?= $thumbnail->extension(); ?>">
        <div class="lab-thumbnail responsive-image">
            <?php snippet('responsive-image', array('field' => $thumbnail)); ?>
        </div>
    </div>
    <?php endif; ?>
<?php endforeach; ?>

#6

Yes, thumbnail() is an image field and $labs is a collection.

If I add toFile() I get the error:

Call to a member function extension() on null

<div class="lab-thumbnail-cnt <?= $thumbnail->original()->toFile()->extension(); ?>">
    <div class="lab-thumbnail responsive-image">
        <?php snippet('responsive-image', array('field' => $thumbnail)); ?>
    </div>
</div>

#7

See my code above please, not original()->toFile() but thumbnail()->toFile() within the if statement.