We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47725
    • 4 Posts
    Hello everyone,

    I am building a plugin and I want to use phpThumb to resize images, the problem is that what my server gets back from
    http://domain.tld/connectors/system/phpthumb.php?src=/var/www/upload/phpK9Dc5d.gif&w=300&h=300&f=gif&q=90
    is:
    {'success':false,'code':401}
    (Aka, error 401 Unauthorized Access)

    But when I go to the exact same url in my browser, I see the thumbnail of the image.

    Does anyone else have this issue or does anyone know the solution? I've been at this for a couple of hours now and couldn't find anything on it.

    This is the code I generate my url with / get the file contents:

    $downSizeQuery = http_build_query(
        array(
            'src' => "/var/www/upload/" . $moved['filename'],
            'w' => $downSizeWidth,
            'h' => $downSizeHeight,
            'f' => $ext,
            'q' => $downSizeQuality,
            ), '', '&');
    $ch = curl_init ($this->ctx->getOption('site_url') . "connectors/system/phpthumb.php?" . urldecode($downSizeQuery));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);


    Thanks in advance,

    Robin Rijkeboer

    This question has been answered by Mr5o1. See the first response.

    [ed. note: beagon last edited this post 12 years, 4 months ago.]
      • 47725
      • 4 Posts
      Alright, I've figured out what the problem is. The php process isn't authenticated with Modx for some reason so it can't get any content.

      I've tried giving
      'HTTP_MODAUTH' => $this->xpdo->user->getUserToken($this->xpdo->context->get('key')),
      'wctx' => $this->ctx->get('key'),
      'source' => $this->get('id'),
      to the url but it still returns me with a 401 error. Is there any way to let phpthumb know that the script needs access?

      Sincerely,

      Robin Rijkeboer
      • discuss.answer
        • 20371
        • 58 Posts
        I'm not really sure what you mean by "the php process isn't authenticated with Modx". It looks like you're trying to access the file from the external url, so I'd have thought the curl request would go through apache just like your web request?

        In any case, I think you might be going at it the hard way around. It certainly wasn't the cleanest approach, but I used the pThumb (not phpThumb) plugin to do the resize for me. It returns a url to the cached, modified image. From there you can just read the file from fs. Here's an exerpt..

         //run phpthumbof
        $args = array(
        'input' => $src,
        'options' => $phpThumbOpts
        );
        $src = $modx->runSnippet('phpThumbOf', $args);
         
        //translate path
        $src = preg_replace("/(.*assets\/)/", "", $src);
        $src = $modx->config['assets_path'] . $src;
         
        //debug
        if ($debug) {
        d($options);
        d(array($src, $args));
        die();
        }
        //output
        $im = imagecreatefromjpeg($src);
        header('Content-Type: image/jpeg');
        imagejpeg($im);
        die();
        


        full snippet

        A few things to bear in mind..


        • even though runSnippet calls 'phpThumbof', that's actually just a referrer to pThumb
        • I'm not sure if you need to do this.. but I couldn't just return nothing from the snippet.. whatever I tried MODx would mess up the file I was trying to return. That's why there's a closing die() statement.