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 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.
    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By GradDev
      I have a file field named "resume". I want to delete the file saved in it using the API.
      The field is limited to only 1 upload of file and the return type is set to "Automatic" which means it will return a single object if a file is available.
      Now there are two options that I have tried- delete() and deleteAll().
      $page_to_change = $pages->get("name=xyz"); $page_to_change->resume->deleteAll(); //OR //$page_to_change->resume->delete($page_to_change->resume); $page_to_change->of(false); $page_to_change->save(); Both of these give an error like-
      Error: Exception: Method Pagefile::deleteAll does not exist or is not callable in this context(...) I understand that this error is because these two functions work on a wirearray rather than on a file object.
      But how do I delete a single file?
    • By gebeer
      Hello all,
      wasn't sure where to put this, so it goes in General section.
      Ryan shows a hook that we can use to mirror files on demand from live server to development environment to be up to date with the files on the server without having to download complete site/assets/files folder.
      I just implemented this but had problems getting files to load from a site in development that is secured with user/password via htaccess.
      First I tried to use WireHttp setHeader method for basic authentication like this
      function mirrorFilesfromLiveServer(HookEvent $event) { $config = $event->wire('config'); $file = $event->return; if ($event->method == 'url') { // convert url to disk path $file = $config->paths->root . substr($file, strlen($config->urls->root)); } if (!file_exists($file)) { // download file from source if it doesn't exist here $src = 'http://mydomain.com/site/assets/files/'; $url = str_replace($config->paths->files, $src, $file); $http = new WireHttp(); // basic authentication $u = 'myuser'; $pw = 'mypassword'; $http->setHeader('Authorization: Basic', base64_encode("$u:$pw")); $http->download($url, $file); } } But, unfortunately this didn't work.
      So now I am using curl to do the download. My hook function now looks like this
      function mirrorFilesfromLiveServer(HookEvent $event) { $config = $event->wire('config'); $file = $event->return; if ($event->method == 'url') { // convert url to disk path $file = $config->paths->root . substr($file, strlen($config->urls->root)); } if (!file_exists($file)) { // download file from source if it doesn't exist here $src = 'http://mydomain.com/site/assets/files/'; $fp = fopen($file, 'w+'); // init file pointer $url = str_replace($config->paths->files, $src, $file); $u = 'myuser'; $pw = 'mypassword'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 50); // crazy high timeout just in case there are very large files curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, "$u:$pw"); // authentication curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // authentication curl_setopt($ch, CURLOPT_FILE, $fp); // give curl the file pointer so that it can write to it curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch); curl_close($ch); } } Now I can load files and images from the htaccess protected development server 🙂
      If anyone knows how to get this to work with WireHttp, please let me know. Thank you.
    • By AndZyk
      Hello,
      I am currently building a intranet which will be hosted on the local network of a company. This intranet has many links to files on their fileserver with the protocol file://.
      So for example the links look like this file://domain.tld/filename.ext
      When I try to insert such a link into a URL field, I get the error, that only the protocol http:// is allowed. When I try to insert such a link into a CKEeditor link, it gets stripped out. Is it possible to insert such links into the FieldType URL and CKEditor.
      I know that I could use a FieldType Text or insert a RewriteRule in the .htaccess file, but I am looking for a more elegant solution. 😉
      Regards, Andreas
×
×
  • Create New...