Jump to content

Resize image manually


DrQuincy
 Share

Recommended Posts

If you want to resize an image that is not a PageImage is there a way to do it?

No worries if not as I can just get Intervention Image via composer but didn't want to include a library I didn't need if PW can do it. I can't see anything in $files and it seems functions like size() can only be applied to PageImages.

Thanks.

Link to comment
Share on other sites

You can do with using the imagesizer directly:

$is = new ImageSizer("path/to/your/image/filename.jpg", [OPTIONS-ARRAY--OPTIONAL]);
// $is->setOptions([OPTIONS-ARRAY--OPTIONAL]); // or set it later here, or ommit setting individual options and use the defaults !
$is->resize($width, $height); // width or height can be 0 !

This resizes and overwrites the image you pass to it. So if you do this with a PW pageimage, you would overwrite the original image if you pass it the originals name!!

  • Like 2
Link to comment
Share on other sites

On 9/13/2021 at 12:27 PM, horst said:

You can do with using the imagesizer directly:

$is = new ImageSizer("path/to/your/image/filename.jpg", [OPTIONS-ARRAY--OPTIONAL]);
// $is->setOptions([OPTIONS-ARRAY--OPTIONAL]); // or set it later here, or ommit setting individual options and use the defaults !
$is->resize($width, $height); // width or height can be 0 !

This resizes and overwrites the image you pass to it. So if you do this with a PW pageimage, you would overwrite the original image if you pass it the originals name!!

@horst Thanks, this works great but I am left with some -tmp images. What I needed to do was create thumbnail images outside of PageImage context. So I need to duplicate the file first and then pass it to a new ImageSizer object and resize per your example. My code is like this:

public static function createDuplicateImage($src, $dest) {
	
	if (file_exists($src) === false) {
		
		return false;
		
	}
	
	// Duplicate the file
	\ProcessWire\wire('files')->copy($src, $dest);
	
	return new \ProcessWire\ImageSizer($dest);
	
}


// ...

$image = Util::createDuplicateImage('./foo.png', './foo-cropped.png');
$image->resize(250, 250);

I get foo-cropped.png sized to 250px x 250px but am left with a duplicate foo-cropped.png-tmp.png. Looking at the ImageSizeEngine class this line is returning false for some reason:

$this->wire('files')->unlink($this->tmpFile);

Any ideas why? I am just on a local MAMP set up and have never had problems creating and deleting files with PHP before.

Thanks. ?

Link to comment
Share on other sites

Thanks. I have found out the issue and it was not permission-related.

In file-errors.txt I had this error:

allowPath: pathname may not traverse “../”

I don't really understand how it works but from looking at the source is seems WireFileTools->allowPath() is blocking it.

This fixes the error:

public static function createDuplicateImage($src, $dest) {
	
	if (file_exists($src) === false) {
		
		return false;
		
	}
	
	// Duplicate the file
	\ProcessWire\wire('files')->copy($src, $dest);

	// NOTE new line
	$dest = realpath($dest);
	
	return new \ProcessWire\ImageSizer($dest);
	
}

You need to call realpath() after file duplication (since realpath() does not work on non-existent files). This sends the full canonicalised absolute pathname and WireFileTools via ImageSizer deletes the -tmp files.

I'm not sure if this is intended behaviour but I hope that helps anyone else who needs to do this. I presume it is a security feature that blocks certain paths from file manipulation.

  • Like 1
Link to comment
Share on other sites

..., yep, ooor, you should not use ../ this in your dest path. ? This is for security reasons.

If you give real $dest pathes without up-traversal segments into your createDuplicateImage() function, then you don't need realpath() there. 
Or you use it one step earlier:

public static function createDuplicateImage($src, $dest) {
	
	if (file_exists($src) === false) {
		return false;
	}
	
	// NOTE new line
	$dest = realpath($dest);
	
	// Duplicate the file
	\ProcessWire\wire('files')->copy($src, $dest);

	return new \ProcessWire\ImageSizer($dest);

}


But anyway. I'm glad you figured it out and finally it is working for you! ?

  • Like 3
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...