# phpThumb / pThumb - Ignoring Contexts

**URL:** <https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043>\
**Category:** Extras\
**Created:** [December 25, 2019, 4:45am UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043 "2019-12-25T04:45:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr\_JimWest](https://community.modx.com/user_avatar/community.modx.com/mr_jimwest/32/112_2.png) [@Mr\_JimWest](https://community.modx.com/u/Mr_JimWest)\
**Post date:** [December 25, 2019, 4:45am UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/1 "2019-12-25T04:45:16Z")

</div>

### Summary

I’m unable to get phpThumb / pThumb to work, I’m thinking it may have to do with my multi-site / multi-context setup…(different domains)

### Step to reproduce

I have tried uninstalling and reinstalling pThumb a few times with the same result.

Current code:  
`[[!pThumb?&input=`[[+photo\_a]]`&options=`w=100`]]`

“photo\_a” is the full resolution photo, currently rendering properly as text:  
`/_files/images/articles/1.jpg`

I have also tried setting `phpthumb_document_root` in the context settings. It does not seem to have taken any affect.

### Observed behavior

I’m assuming the final result would be a unique URL to a thumbnail of my photo, instead I’m getting back what I put in: `/_files/images/articles/1.jpg`.

I’m receiving the following error on installation:

```auto
xPDOVehicle validator failed: type php (XXXX/core/packages/pthumb-2.3.3-pl/xPDOTransportVehicle/aea43ca81ababaf9acb1782bbccb4b19.validate.resizer.validator)

```

I’m seeing this error repeating in the log when I refresh the live page:

```auto
[2019-12-24 22:53:10] (ERROR @ XXXX/core/components/phpthumbof/model/phpthumbof.class.php : 156) [pThumb] Resource: 5 || Image: (none)
File not found: XXXX/_files/images/articles/1.jpg ***Skipping***
[2019-12-24 22:53:11] (ERROR @ XXXX/core/components/fastfield/model/fastfield/fastfield.php : 97) fastField: Resource `0` doesn't exist
[2019-12-24 22:53:11] (ERROR in resource 5 @ XXXX/core/model/modx/modparser.class.php : 1373) Bad link tag `[[~0]]` encountered

```

_The second line (above) would never work, it’s not the correct path. Somehow I need to add a few folders to that_. 🤔

Here is a screenshot of my current phpThumb settings:

 ![Screenshot_2019-12-24 System Settings MODX Revolution](https://modx-community.s3.dualstack.us-east-1.amazonaws.com/original/1X/6b903cc90335d36f230841693b0d9d1fc9fbd466.png)

### Environment

- MODX: 2.7.1-pl
- MIGX: 2.12.0-pl
- PHP: 7.3.3
- MySQL: 5.1.73 (latin1, latin1\_swedish\_ci)
- pThumb 2.3.3-pl

### Overview

I would like to automatically produce thumbnails of higher-quality photos for lazy loading purposes. Initially I thought I could just use the thumbnail functionality already being produced in the MODX media browser, but I couldn’t get that to work. I’m open to the most efficient way of accomplishing this…any ideas?

Thank you for your help and Merry Christmas to you!

---

<div class="post-metadata">

**Author:** ![Mr\_JimWest](https://community.modx.com/user_avatar/community.modx.com/mr_jimwest/32/112_2.png) [@Mr\_JimWest](https://community.modx.com/u/Mr_JimWest)\
**Post date:** [December 30, 2019, 10:01pm UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/2 "2019-12-30T22:01:20Z")

</div>

### Update

I have spent **many** hours trying to figure this out. I think I have a better understanding of what the issue is, but I’m still stuck. 🥱

I’m using multiple contexts with separate media sources.

Let’s say I have this code in a chunk:  
`<img src="[[pthumb? &input=`[[+photo_a]]` &options=`w=150&h=150&zc=1`]]">`

Where **[[+photo\_a]]** = `/_files/images/articles/1.jpg` (working perfectly, but this is not the full path from the root, this is a relative path based on the context and domain)

On load I receive the following error in the log:

```auto
[2019-12-30 16:09:02] (ERROR @ XXX/core/components/phpthumbof/model/phpthumbof.class.php : 156) [pThumb] Resource: 5 || Image: (none)
File not found: XXX/_files/images/articles/1.jpg ***Skipping***

```

**XXX** = _the full path from the root of my server_

The error is correct, the file is not there. The path is missing a **couple** directories in between. The full path from the root is: XXX/ **sites/site\_a** /\_files/images/articles/1.jpg.

Why is it ignoring my context?!!!?!

So I changed my code in the chunk to the following, adding in the missing directories:  
`<img src="/sites/site_a[[pthumb? &input=`[[+photo_a]]` &options=`w=150&h=150&zc=1`]]">`

Success, I now have a thumbnail! But wait…Now the file location is based off the root of MODX, not the context/domain root, so it’s not displaying: “/sites/site\_a/\_files/cache/1.6481d8d1.jpg”.

Instead, it should be: “/\_files/cache/1.6481d8d1.jpg”, if I navigate there, it works.

How do I change the output to give me the relative path?!?!?!!?

### Settings/Properties

Here are a full list of my current Context Settings and Media Source Properties:

 ![image](https://modx-community.s3.dualstack.us-east-1.amazonaws.com/original/1X/a2995605adf3e2fb621b0788a69590fae324633b.png)

Am I better off doing this a completely different way?!?!? 😭

Thanks again for any help you might be able to offer me!

---

<div class="post-metadata">

**Author:** ![dev\_willis](https://community.modx.com/user_avatar/community.modx.com/dev_willis/32/1721_2.png) [@dev\_willis](https://community.modx.com/u/dev_willis)\
**Post date:** [December 31, 2019, 5:17pm UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/3 "2019-12-31T17:17:20Z")

</div>

I notice that you have `basePathRelative` set to `Yes` but your actual `basePath` is using an absolute path. Have you tried removing the leading slash?

---

<div class="post-metadata">

**Author:** ![Mr\_JimWest](https://community.modx.com/user_avatar/community.modx.com/mr_jimwest/32/112_2.png) [@Mr\_JimWest](https://community.modx.com/u/Mr_JimWest)\
**Post date:** [December 31, 2019, 11:31pm UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/4 "2019-12-31T23:31:55Z")

</div>

Thanks for the reply. Good call.

I just saw this post from @bobray too:

> [@Images not shown in second context](https://community.modx.com/t/images-not-shown-in-second-context/2066/8):
>
> Note that it’s a modx convention that all path and URL settings end in a slash. So the standard approach is to have all baseURL settings end in a slash and never use a slash at the beginning of anything that will be added to them.

I changed all of the paths to relative, no change. 😭

### Edit

I am continuing to play with this, here is the latest log:

```
[2019-12-31 21:02:10] (ERROR @ XXX/core/components/phpthumbof/model/phpthumbof.class.php : 156) [pThumb] Resource: 5 || Image: _files/images/articles/1.jpg
Could not generate thumbnail
phpThumb debug output:
    [0] => phpThumb() v1.7.15-201902101903 in file "phpthumb.class.php" on line 229
    [1] => setSourceFilename(XXX/_files/images/articles/1.jpg) set $this->sourceFilename to "XXX/_files/images/articles/1.jpg" in file "phpthumb.class.php" on line 294
    [2] => Addding ImageMagick formats to $AvailableImageOutputFormats (text;ico;bmp;wbmp;gif;png;jpeg;webp) in file "phpthumb.class.php" on line 999
    [3] => $AvailableImageOutputFormats = array(text;ico;bmp;wbmp;gif;png;jpeg;webp) in file "phpthumb.class.php" on line 1005
    [4] => $this->thumbnailFormat set to $this->config_output_format "jpeg" in file "phpthumb.class.php" on line 1016
    [5] => $this->thumbnailFormat set to $this->f "jpeg" in file "phpthumb.class.php" on line 1023
    [6] => $this->thumbnailQuality set to "75" in file "phpthumb.class.php" on line 1033
    [7] => ResolveFilenameToAbsolute() prepending $this->config_document_root (sites/site_a/) to $filename (XXX/_files/images/articles/1.jpg) resulting in ($AbsoluteFilename = "sites/site_a//XXX/_files/images/articles/1.jpg") in file "phpthumb.class.php" on line 1338
    [8] => resolvePath: sites/site_a//XXX/_files/images/articles/1.jpg (allowed_dirs: Array
(...

```

This could be a major part of my issue…it looks like it’s prepending my context path to the full root path of my image. That will never work! My context path needs to be between the path of the image and the root. ☠

````auto
[7] => ResolveFilenameToAbsolute() prepending $this->config_document_root (sites/site_a/) to $filename (XXX/_files/images/articles/1.jpg) resulting in ($AbsoluteFilename = "sites/site_a//XXX/_files/images/articles/1.jpg") in file "phpthumb.class.php" on line 1338```
````

---

<div class="post-metadata">

**Author:** ![Mr\_JimWest](https://community.modx.com/user_avatar/community.modx.com/mr_jimwest/32/112_2.png) [@Mr\_JimWest](https://community.modx.com/u/Mr_JimWest)\
**Post date:** [January 1, 2020, 3:29am UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/5 "2020-01-01T03:29:25Z")

</div>

Well, I figured out how to break the PHP to make it work for me 😠…and I did it in a very janky way:

### Edit 1

_XXX\core\components\phpthumbof\model\phpthumbof.class.php_: Line 419-420

**Replaced:**

```auto
$this->phpThumb->setSourceFilename(($this->input[0] === '/' || $this->input[1] === ':') ? $this->input : MODX_BASE_PATH . $this->input);

```

**With this:**

```auto
$testingthisthing = str_replace('XXX/', 'XXX/' . $this->phpThumb->config_document_root, (($this->input[0] === '/' || $this->input[1] === ':') ? $this->input : MODX_BASE_PATH . $this->input));
$this->phpThumb->setSourceFilename($testingthisthing);

```

Essentially I’m adding in the `config_document_root` (AKA context path: _sites/site\_a/_) in between the root and the image path. Which gives the **proper** full path to the root. Not the jacked up one shown in previous posts.

### Edit 2

_XXX\core\components\phpthumbof\model\phpthumbof.class.php_: Line 438

**Replaced:**

```auto
$output['src'] = $cacheUrl;

```

**With this:**

```auto
$output['src'] = str_replace($this->phpThumb->config_document_root, '', $cacheUrl);

```

Here I’m doing a _bang-up job_ removing the root path I needed to add for the PHP to see the image…but can’t be there for the sake of my context’s path.

It works, but it’s not a good solution. I know this is really bad programming - if anything, it needs to be re-written. Either way, I figure I’m just missing something else…somewhere…in the UI.

Any ideas anyone? Please? 😬

---

<div class="post-metadata">

**Author:** ![dev\_willis](https://community.modx.com/user_avatar/community.modx.com/dev_willis/32/1721_2.png) [@dev\_willis](https://community.modx.com/u/dev_willis)\
**Post date:** [January 1, 2020, 7:31pm UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/6 "2020-01-01T19:31:58Z")

</div>

Have you tried the phpThumbOf system settings? There are some path related settings there, including one about a base directory for images, that could have an impact on this. You can sometimes use placeholders and template tags in the paths for settings as well, which would allow you to dynamically set the path based on the media source. Redactor’s file path settings work that way but I don’t know for sure that pThumb can do that tho though.

---

<div class="post-metadata">

**Author:** ![Mr\_JimWest](https://community.modx.com/user_avatar/community.modx.com/mr_jimwest/32/112_2.png) [@Mr\_JimWest](https://community.modx.com/u/Mr_JimWest)\
**Post date:** [January 11, 2020, 5:17am UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/7 "2020-01-11T05:17:28Z")

</div>

Thanks for the reply @dev_willis. Sorry I haven’t gotten back to you sooner, I’ve been quite sick for the last two weeks. 🥶

I don’t think I had tried looking at “phpThumbOf” yet. But I took a look and only see the following two settings come close to your suggestion:

> Override Cache Path (phpthumbof.cache\_path): You may set an absolute path here to override the path for the cache.

and

> Override Cache URL (phpthumbof.cache\_url): An absolute URL to be prepended to generated image URLs. Can be useful when using contexts and subdomains.

Neither of them appear to change anything. Nor do their descriptions seem like something I would want to change.

I’m so confused why I have all of these separate settings, that appear to have the same job, all to change one thing, and none of them appear to work?!?1 😫

---

<div class="post-metadata">

**Author:** ![dev\_willis](https://community.modx.com/user_avatar/community.modx.com/dev_willis/32/1721_2.png) [@dev\_willis](https://community.modx.com/u/dev_willis)\
**Post date:** [January 11, 2020, 8:38pm UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/8 "2020-01-11T20:38:28Z")

</div>

No worries. Sorry you’re having so much trouble with this. Those settings you mention definitely _sound_ like they do what you want. If changing them has no effect, and assuming they actually do what they seem to be saying they do, then I would think there’s a cache that needs clearing or maybe there’s a bug in phpThumbOf. The description, with it’s mention of the setting being useful when using contexts, certainly makes it sound like what you need.

As for all the different settings, they do not have the same job nor change the same things and they all do work, at least as far as the system settings go–I can’t vouch for phpThumbOf. They’re just not the settings you’re looking for. There are many things that might need custom path options and nearly everything in MODX is configurable.

---

<div class="post-metadata">

**Author:** ![Mr\_JimWest](https://community.modx.com/user_avatar/community.modx.com/mr_jimwest/32/112_2.png) [@Mr\_JimWest](https://community.modx.com/u/Mr_JimWest)\
**Post date:** [January 11, 2020, 10:00pm UTC](https://community.modx.com/t/phpthumb-pthumb-ignoring-contexts/2043/9 "2020-01-11T22:00:22Z")

</div>

@dev_willis, I don’t think my cache path was ever “wrong”, and too much was already being prepended to the url as it is.

What I don’t get is, I have “phpThumb”, “pThumb”, and “phpThumbOf”, they all seem to have their own setting for cache locations and other things! How did I get in this mess? I just need one functioning extra, not three lol. When I modified the one you suggested, nothing happened - almost like it was being overwritten with one of the other very similar settings.

I agree that something is screwed up somewhere. I went through the file (`phpthumbof.class.php`) and printed 30+ variables trying to figure out what was going on. Based on what I saw in the output, I was unable to determine how I could ever get this to work without modifying it. So I came up with my own fix (below).

## This is what I had to do:

### Edit 1

_XXX\core\components\phpthumbof\model\phpthumbof.class.php_ : Line 419-420

**Replaced:**

```auto
$this->phpThumb->setSourceFilename(($this->input[0] === '/' || $this->input[1] === ':') ? $this->input : MODX_BASE_PATH . $this->input);`

```

**With this:**

```auto
$this->phpThumb->setSourceFilename(MODX_BASE_PATH . $this->modx->getOption('base_url') . $src);

```

I am correcting the output path from this:  
`XXX/_files/images/articles/1.jpg` (which is not even close to the correct folder structure)  
to this:  
`_files/cache/articles/1.43b11e84.jpg`

* * *

### Edit 2 (same file)

_XXX\core\components\phpthumbof\model\phpthumbof.class.php_ : Line 64-65

**Replaced:**

```auto
$this->config['cachePathUrl'] = str_replace(MODX_BASE_PATH, "$cacheurl/", $this->config['cachePath']);

```

**With this:**

```auto
$this->config['cachePathUrl'] = str_replace(MODX_BASE_PATH . $this->modx->getOption('base_url'), "", $this->config['cachePath']);

```

I’m just adding the context path in “sites/site\_a/”, which wasn’t there otherwise.

* * *

### Edit 3

_Context Setting Change_

**Changed:** Images Base Directory (`pthumb.ptcache_images_basedir`)  
**from** `sites/site_a/_files/images/` **to** `_files/images/`

This is probably where all my problems are, in these directory settings…but I had to output this setting next to other variables in the PHP to understand what the path should actually be! 😠

* * *

This took forever to figure out and I’m not too pleased with this solution…but it does work 100% as expected now. My images are being cached in the correct folder, with the correct folder structure, and are also displaying on the site as expected.
