Question on PHP5 Class and Custom Size thumbnails

Update: The content in this post maybe out of date or inaccurate. Visit our Support Page

I was wondering how or if this can be done.

I would like to use the following code but modify it to grab custom size thumbnails using the xmax variable, is that possible and if so does someone have a suggestion on how to modify the code below to construct the correct call to Shrink the Web?

My understanding is that the code below was written to always grab the xlg thumbnail but basically my question is how can it be modified to grab a custom size thumbnail for local cache?

I realize there is a function in the class that allows you to scale the xlg thumbnail but scaling produces a fuzzy image and I would rather have a crisp image, if this is even possible.

  public static function queryRemoteThumbnail($url, $args = null, $debug = false) {
        $args = is_array($args) ? $args : array();

        $defaults["Service"] = "ShrinkWebUrlThumbnail";
        $defaults["Action"] = "Thumbnail";
        $defaults["STWAccessKeyId"] = self::ACCESS_KEY;
        $defaults["stwu"] = self::SECRET_KEY;

		foreach ($defaults as $k=>$v)
			if (!isset($args[$k]))
			$args[$k] = $v;
		
		$args["Url"] = $url; // now url is last


		
		$request_url = urldecode("http://www.shrinktheweb.com/xino.php?".http_build_query($args)); 

        $line = self::make_http_request($request_url);

        if ($debug) {
            echo '<pre style=font-size:10px>';
            unset($args["STWAccessKeyId"]);
            unset($args["stwu"]);
            print_r($args);
            echo '</pre>';
            echo '<div style=font-size:10px>';
            highlight_string($line);
            echo '</div>';
        }

        $regex = '/<[^:]*:Thumbnail\\s*(?:Exists=\"((?:true)|(?:false))\")?[^>]*>([^<]*)<\//';

        if (preg_match($regex, $line, $matches) == 1 && $matches[1] == "true")
            return $matches[2];

        return null;


    }

    public static function getThumbnail($url, $args = null, $force = false) {
        $args = $args ? $args : array("Size"=>"lg");
        $name = md5($url.serialize($args)).".jpg";
        $src = self::THUMBNAIL_URI."/$name";
        $path = self::THUMBNAIL_DIR.$src;
        $cutoff = time() - 3600 * 24 * self::CACHE_DAYS;

        if ($force || !file_exists($path) || filemtime($path) <= $cutoff)
            if (($jpgurl = self::queryRemoteThumbnail($url, $args)))
                if (($im = imagecreatefromjpeg($jpgurl)))
                     imagejpeg($im, $path, 100);

        if (file_exists($path))
            return $src;

        if (!file_exists($path))
		$src='/images/stwsitequeued.jpg';
            return $src;

        return null;
    }
puravida's picture
puravida
Jedi Warrior
Online
Joined: 09/01/2007
Visit puravida's Website

Hi Mark,

Yes, it is possible.

The PHP5 class was written by another member of the STW Community and we have not tested what you want.

However, it should simply be a matter of finding:

$args = $args ? $args : array("Size"=>"lg");

and replacing with:

$args = $args ? $args : array("xmax"=>"YYY");
*where YYY is the numeric value for width (e.g. 175, 225, etc)

Give that a go and let us know if that worked.

Best regards,

Brandon

GloTouch
Offline
Joined: 04/13/2008
Visit GloTouch's Website

Thanks!

I did try that but I keep getting the xlg thumbnail written to my cache directory, rather then the xmax which I have set to 645.

That made me think that maybe it had something to do with the queryRemoteThumbnail function but I think the $arg are passed up by the getThumbnail function, so basically I have no idea. I am still pretty much a novice.

When I use the simple method I can see that the custom thumbnail is captured but when I use the PHP5 class, with the change, as you have posted here, I still only cache the xlg thumbnail.

Any other thoughts?

Thanks again

puravida's picture
puravida
Jedi Warrior
Online
Joined: 09/01/2007
Visit puravida's Website

There must be somewhere else that &Size=xlg is being specified. That needs to be replaced with &xmax=645 in your case.

You should consider &xmax=640, because other requests might have already been made for certain sites at that more common resolution. It would speed up response times, if that happened to be the case.

I will take a more in-depth look and see if I can't see where else the &Size param is being passed.

-Brandon

puravida's picture
puravida
Jedi Warrior
Online
Joined: 09/01/2007
Visit puravida's Website

If you are using the provided Source code, you may need to replace:
$args["Size"] = "xlg";
with
$args["xmax"] = "645";

However, it also appears to depend upon the called "convenience functions" which are hard-coded in the corresponding size.

I have added another "convenience function" called getCustomThumbnail which allows you to modify the following:
return self::getThumbnail($url, array("xmax"=>"YYY"));
to
return self::getThumbnail($url, array("xmax"=>"645"));

You'll need to download the latest PHP5 class code to get the new "convenience function" I added.

Give that a go and let us know if you get it working.

Best regards,

Brandon

GloTouch
Offline
Joined: 04/13/2008
Visit GloTouch's Website

Excellent!

It works.

Now that is customer service,

Great work!

GloTouch
Offline
Joined: 04/13/2008
Visit GloTouch's Website

Hello,

I have new question.

I would like to add the "inside" varaible to the PHP5 class.

I have added this line:

$args = $args ? $args : array("inside"=>"1");

To the queryRemoteThumbnail function but it still only shows the "Top Level Domain" rather then the inside page.

Could you offer a suggestion on what needs to be modified in the PHP5class to add the inside page functionality to it?

Thanks,
Mark

puravida's picture
puravida
Jedi Warrior
Online
Joined: 09/01/2007
Visit puravida's Website

Hi Mark,

Your method looks like it would work at first glance, but since it doesn't; I would try adding it to the default argument list in the queryRemoteThumbnail function as follows:

$defaults["inside"] = "1";

Give that a go and report back the results, please.

Best regards,

Brandon

GloTouch
Offline
Joined: 04/13/2008
Visit GloTouch's Website

That Worked!

Thanks for such a quick reply.

Mark

Topic locked

ShrinkTheWeb® (About STW) is another innovation by Neosys Consulting

Contact Us | PagePix Benefits | Learn More | Our Partners | Privacy Policy | Terms of Use

©2014 ShrinkTheWeb. All rights reserved. ShrinkTheWeb is a registered trademark of ShrinkTheWeb.