Jump to content
neophron

How to remove a file extension?

Recommended Posts

Hi,

I was searching in the forum about a method how to remove a file extension, like .jpg, but couldn't find anything.
There are some php methods out in the wild, but I wanted first to get sure, that Processwire is offering a solution.

The background is, that a clients website has a lot of images, packed in different galleries. All image files contain the name, dimensions and other stuff about the image (painting). I want to echo the image name in the image tag.

 

thanks 

Share this post


Link to post
Share on other sites

That's so trivial, that you hardly need a function for that.

$filename = '/path/to/image.jpg';
echo pathinfo($filename)['filename'];

Or am I missing something? 

Share this post


Link to post
Share on other sites
34 minutes ago, neophron said:

The background is, that a clients website has a lot of images, packed in different galleries. All image files contain the name, dimensions and other stuff about the image (painting). I want to echo the image name in the image tag.

Are these images saved in ProcessWire image fields?

Share this post


Link to post
Share on other sites

The image field is »array of items« with 0=no limit. And this is the code the template:

<?php
        foreach($page->images_slider as $slide_image) {
          $slide = $slide_image->size(0,700);
            echo "
              <li class='uk-width-3-4'>
                <img class='photo' src='{$slide->url}' alt='{$slide_image->basename}' height='700' />
              </li>

              ";
            }
        ?>

 

Share this post


Link to post
Share on other sites

Maybe rtrim can work as well? or that and mb_string?

E.g.

 

foreach($page->images_slider as $slide_image) {
   echo rtrim($slide_image->name,"{$slide_image->ext},.");
}

I haven't tested this much. I also cannot remember if ProcessWire allows dots in file names.

Share this post


Link to post
Share on other sites
1 hour ago, neophron said:

The image field is »array of items« with 0=no limit. And this is the code the template:


<?php
        foreach($page->images_slider as $slide_image) {
          $slide = $slide_image->size(0,700);
            echo "
              <li class='uk-width-3-4'>
                <img class='photo' src='{$slide->url}' alt='{$slide_image->basename}' height='700' />
              </li>

              ";
            }
        ?>

 

@neophron A solution about using filename as image alt info. You can try code below :

<?php
foreach($page->images_slider as $image) {
	$slide = $image->size(0,700);
	$alt = $image->description ?: str_replace('.' . $image->ext, '', $image->basename);

	echo "<li class='uk-width-3-4'>";
	echo "<img class='photo' src='{$slide->url}' alt='{$alt}' width='{$slide->width}' height='{$slide->height}' />";
	echo "</li>";
}
?>

 

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

@neophron Always using this method may cause headache for you. What about add this method as property for each image field and use it like $imageField->alt. We need to add a hook method.

Add hook method to existing /site/ready.php or create new /site/ready.php, copy below code and paste it in to /site/ready.php :

/site/ready.php

<?php

namespace ProcessWire;

wire()->addHookProperty('Pageimage::alt', function(HookEvent $event) {
    /* @var $image Pageimage */
    $image = $event->object;
    $event->return = $image->description ?: str_replace('.' . $image->ext, '', $image->basename);
});

Usage ($imageField->alt) :

<?php
foreach($page->images_slider as $image) {
	$slide = $image->size(0,700);

	echo "<li class='uk-width-3-4'>";
	echo "<img class='photo' src='{$slide->url}' alt='{$image->alt}' width='{$slide->width}' height='{$slide->height}' />";
	echo "</li>";
}
?>

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Marvin
      Hello,
      I'm new at process wire and i want to make an web using upload file and showing it at the table as a link to open it at the new tab. I wa succed while upload a file, but how i showing it as a link at the table to open it at the new tab of my browser? Any suggestion may helpfull
      Here i attach my code below :
      This code is for upload it to back-end (process wire)
      <?php $note = $note2 = $hidden =""; if($input->post->submit){ $upload_path = $config->paths->assets.'files/upload/'; if(!is_dir($upload_path)){ if(!wireMkdir($upload_path)) throw new WireException("No upload path"); } $original = $sanitizer->text($input->post->original); $indonesia = $sanitizer->text($input->post->indonesia); $other = $sanitizer->text($input->post->other); $composer = $sanitizer->text($input->post->composer); if(!$original || !$indonesia || !$other || !$composer){ $note = "Data tidak lengkap"; } else { $newFile = new WireUpload("song_files"); $newFile->setMaxFiles(1); $newFile->setOverwrite(false); $newFile->setDestinationPath($upload_path); $newFile->setValidExtensions(array('pdf','docx','doc')); $files = $newFile->execute(); if(!count($files)) { $newFile->error("No files received, so not creating page."); return false; } $newImg = new WireUpload("img_files"); $newImg->setMaxFiles(1); $newImg->setOverwrite(false); $newImg->setDestinationPath($upload_path); $newImg->setValidExtensions(array('jpeg','jpg','png','gif')); $files = $newImg->execute(); if(!count($files)) { $newImg->error("No files received, so not creating page."); return false; } $newPage = new Page(); $newPage->template = "files"; $newPage->parent = $pages->get("/files/"); $newPage->title = $original; $newPage->text_1 = $indonesia; $newPage->text_2 = $other; $newPage->text_3 = $composer; $newPage->of(false); $newPage->save(); foreach($files as $filename) { $filepath = $upload_path . $filename; $newPage->files->add($filepath); $newPage->message("Add file : $filename"); unlink($filepath); } $newPage->save(); } } ?> and this code to showing it as a link at the table
      <table class="border"> <tr> <th>No.</th> <th>Original Song Title</th> <th>Indonesia Song Title</th> <th>Other Song Title</th> <th>Composer</th> <th>File (pdf)</th> </tr> <?php $num = 1; $song; foreach($pages->get("/files/")->children as $child) { //showing every child at files parent directory $page == $child; $song = $pages->get("/files/".$child->id."/")->files; //showing uploaded files at child directory echo $child->id; echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$song->httpUrl."'</a>".$song->name."</td></tr>"; } ?> </table> Thank you for any suggestion
    • By theoretic
      Hi there! And thanks for Processwire!
      It appears there's a possible bug in Processwire 3.0.170 concerning file and/or image inputfield. Creating such a field results in the following error:

      Fatal Error: Uncaught Error: Call to a member function get() on null
      The inputfield is created however. The closer look reveals a problem at line 60 in wire\modules\Fieldtype\FieldtypeFile\config.php:
      if(!$value) $value = $fieldtype->get('defaultFileExtensions'); Commenting this line removes the problem, but the newly created inputfield requires 'Allowed file extensions' config option to be set (which is rather expectable since i commented the above-cited line of code). Never faced this problem before, hope it can be resolved.
    • By dotnetic
      Hi folks, I published "Simple file downloads with ProcessWire tutorial"  today which explains how to make a simple download function with ProcessWire (tested with version 3.0+).
      Basically this is based on my post here in the forums 
       
    • By xportde
      How does one enable executables to be uploaded to the File field? Just adding the file extension to the accepted extensions does not seem to work.

      screenshot attached (ignore the label 'video')

    • By hellerdruck
      Hi all
      I need help with something. Situation: We have let's say 2'000 Files (Excel) that should be displayed (list with links) on a page. We'd need to filter these files by given Keywords or a tree structure or both. Now, I'm looking for a solution whereas our customer can synchronise the files from his local computer with the folder on the webserver. They will update and upload files on a daily basis. Therefore, it would need to synchronise rather than load the files manually in pages or repeaters. Maybe indexing would be an idea, too.
      Are there any modules for Processwire that would help achieving this? Could anyone point me in the right direction?
      Thanks in advance.
×
×
  • Create New...