Jump to content

Recommended Posts

Posted

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 

Posted

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? 

Posted
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?

Posted

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>

              ";
            }
        ?>

 

Posted

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.

Posted
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
Posted

@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 3
  • Thanks 1
  • 2 years later...
Posted

Thanks, very cool idea. The str_replace() isn't even necessary, if you use basename($ext = false).

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

https://processwire.com/api/ref/pagefile/basename/

  • Like 3

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
×
×
  • Create New...