We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5143
    • 116 Posts
    I bit the bullet and finally got round to writing some PHP, but have run into a problem I just can't figure out.

    I'm trying to get phpthumb to work from within a snippet I'm writing - I think I have almost all the pieces, but I've a hunch that maybe I'm not formatting the image filepath correctly.

    This is the code I have:
    $input = "http://www.domain.com/assets/files/".$cover;
    $width = 610;
    $height = 504;
    $options = "w=$width&h=$height&zc=1";   
    $thumb = $modx->runSnippet("phpthumbof",array('input' => $input, 'options' => $options));

    If I comment out the last line, the page loads without errors, but uncommented I get an error and nothing else:

    The image “http://www.domain.com/” cannot be displayed because it contains errors.

    But if I echo the value of $input, it does give the image URL, so I assume that phpthumb doesn't want a URI string...?
      • 35150 ☆ A M B ☆
      • 191 Posts
      Things are much simpler if you give it a path on the file system instead of a URL.
      $input = MODX_ASSETS_PATH . "files/$cover";
      if (is_readable($input)) {  // not a bad idea to check that there's really a file there
          $thumb = $modx->runSnippet(
              'phpthumbof', array(
              'input' => $input,
              'options' => 'w=610&h=504&zc=1'
          ));
      }
      else {
          // log an error message, return a placeholder image, etc.
      }
        Extras :: pThumb • Resizer • imageSlim • setPlaceholders
        • 5143
        • 116 Posts
        Aha - thank you.
        It seems that's exactly what the issue was.
          • 5143
          • 116 Posts
          One other thing's bugging me. I can't seem to get anything in the else block to execute. currently, I have the following:

          $input = MODX_ASSETS_PATH . "files/" . $cover;
          if (is_readable($input)) {
              $thumb = $modx->runSnippet('phpthumbof', array(
                  'input'   =>	$input,
                  'options' =>	'w=600&h=450&zc=1')
              );
          }
          else {
              $thumb = MODX_ASSETS_PATH . "img/cover-image-not-available.gif";
          }


          It doesn't matter what I put in the else statement - be it an echo command etc - it never runs, even when there's no cover image set.

          Here's the full code, in case I'm missing something else:

          function outputList($a,$b,$format){
          
          	global $modx;
          
          	// PAST PROJECTS FROM HERE
          
              echo '<div id="past-projects"><h2>Past<br/>Projects</h2><ul>';
          
          	foreach ($a as $p) {							// Loop through projects
          		$id = $p['id'];								// Get project ID
          		foreach ($b as $t) {						// Iterate over TVs
          			if ($t['contentid'] == $id) {			// Reduce selection to relevent
          				switch ($t['tmplvarid']) {
          					case 3:
          						$cover = $t['value'];
          						break;
          					case 5:
          						$start = formatDate($t['value']);
          						break;
          					case 6:
          						$end = formatDate($t['value']);
          						break;
          				}
          			}
          		}
          
          		$input = MODX_ASSETS_PATH . "files/" . $cover;
                  if (is_readable($input)) {					// Check a cover photo was actually selected
                      $thumb = $modx->runSnippet('phpthumbof', array(
                          'input'   =>	$input,
                          'options' =>	'w=600&h=450&zc=1')
                      );
                  }
          		else {
          			$thumb = MODX_ASSETS_PATH . "img/cover-image-not-available.gif";
          		}
          
          		echo $modx->getChunk('Past-Projects-Tpl',array(
          			'name'    =>	$p['pagetitle'],
          			'url'     =>	$p['uri'],
          			'start'   =>	$start,
          			'end'     =>	$end,
          			'content' =>	$p['content'],
          			'imgsrc'  =>	$thumb)
          		);
          
          		$cover = null;
          		$start = null;
          		$end = null;
          
          	};
          
              echo '</ul>';
          
          };