1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How does the image parser work?

Discussion in 'XenForo Development Discussions' started by Jaxel, Nov 8, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    Okay... lets say I have the URL to an image stored in a string:
    http://i.ytimg.com/vi/kCVZv-cPC10/hqdefault.jpg

    I want to take this image, crop resize from center into a 160x90 image... then store it into a jpg file...

    Any idea how to do this?
     
  2. Jaxel

    Jaxel Well-Known Member

    GOT IT! wow this was so much easier than vBulletin! This is how to do it... feel free to use in your own mods!

    Code:
    	// get extension of image
    	$extension = XenForo_Helper_File::getFileExtension($input['media_thumb']);
    
    	// get width, height and type of image
    	$imageInfo = @getimagesize($input['media_thumb']);
    
    	// set target save location
    	$targetloc = XenForo_Helper_File::getInternalDataPath().'/media/1.'.$extens;
    
    	// create image object from existing image
    	if ($image = XenForo_Image_Abstract::createFromFile($input['media_thumb'], $imageInfo[2]))
    	{
    		// resize image object to 160 width
    		$image->thumbnail('160');
    
    		// get image height and determine its distance from 90
    		$height = $image->getHeight();
    		$offset = ($height - 90) / 2;
    
    		// crop image height to 90 from center, extend if shorter
    		$image->crop('0', $offset, '160', '90');
    
    		// save thumbnail image to target location
    		$image->output($imageInfo[2], $targetloc);
    	}
     
    CyberAP and Darkimmortal like this.
  3. Jaxel

    Jaxel Well-Known Member

    If you wanted... you could force a JEPG file type for your thumbnails... (you could do the same with GIF and PNG)

    Code:
    		$targetLoc = XenForo_Helper_File::getInternalDataPath().'/media/1.jpg';
    		$imageInfo = @getimagesize($input['media_thumb']);
    
    		if ($image = XenForo_Image_Abstract::createFromFile($input['media_thumb'], $imageInfo[2]))
    		{
    			$image->thumbnail('160');
    
    			$height = $image->getHeight();
    			$offset = ($height - 90) / 2;
    
    			$image->crop('0', $offset, '160', '90');
    			$image->output(IMAGETYPE_JEPG, $targetLoc);
    		}
     
  4. Jamie

    Jamie Well-Known Member

    I know this is Off Topic, but I have noticed a trend... You REALLY like working with xF don't you? :D

    People like myself appreciate the work you do and it's great and very helpful, but I get a kick out of it when you're asking a question and figure it out and then find out it's easier than vB..

    If I only knew how to code... so I could have some fun! :)

    Jamie
     
  5. Jaxel

    Jaxel Well-Known Member

    Yes... I'm loving XenForo... I'm not happy with having to rewrite my Media Library mod... but cesta la vie...

    But it appears that the /internal_data/ path isn't accessable from the browser...

    Does anyone know an alternative to getInternalDataPath for the regular data path?
     
  6. Kier

    Kier XenForo Developer Staff Member

    Beta 3 exposes it. Prior to that, no.
     
  7. Jaxel

    Jaxel Well-Known Member

    Okay... is there a function to get the external data URL (not path)?

    Looking for this in both TEMPLATES and in models.
     
  8. Kier

    Kier XenForo Developer Staff Member

    The external data path is always expressed as a relative URL from the base path.
     
  9. Jaxel

    Jaxel Well-Known Member

    XenForo_Helper_File::getInternalDataPath() will return "/home/path/to/website/data" for me... thats not an URL, thats a PATH... I want to be able to get xen1.8wayrun.com/data in a variable automatically... since I know people can change the location of their data folder.
     
  10. Kier

    Kier XenForo Developer Staff Member

    You asked for the external path / URL.

     
  11. Jaxel

    Jaxel Well-Known Member

    Yes... the external URL... by default its yourwebsite.com/data

    However, in the config, you can change the external data path to something else... its why you guys created the getExternalDataPath function. However, in my templates I am hardcoding "data/" as the external data URL... is there a way to automatically get the external data URL, the same way that getExternalDataPath gets the external data PATH?
     
  12. Kier

    Kier XenForo Developer Staff Member

    XenForo_Helper_File::getExternalDataPath() will always return a realtive path to the base URL, so it can be combined with the base URL to form an absolute URL, or used on its own as a relative URL. Either way, it can be used as a URL within your templates.
     
  13. Jaxel

    Jaxel Well-Known Member

    Okay... how would I use this in combination with the base url? Is there a function to get the base url?

    If I use getExternalDataPath by itself, I NEVER get a relative path, it ALWAYS gives me the full path from root.
     
  14. Kier

    Kier XenForo Developer Staff Member

    Aaaah, sorry, I'm talking crap. You actually want XenForo_Application::$externalDataPath - that is the relative path.
     
    Jeremy likes this.
  15. Jaxel

    Jaxel Well-Known Member

    Thanks... now how do I get the base url? I tried this:

    Code:
    $external = XenForo_Link::buildPublicLink('full:'.XenForo_Application::$externalDataPath.'/media');
    works... but I think there should be a better way...
     
  16. Lawrence

    Lawrence Well-Known Member

    Is this what you are looking for?

    PHP:
                $options XenForo_Application::get('options');

                
    $boardUrl $options->boardUrl;
     
  17. Kier

    Kier XenForo Developer Staff Member

    If you're working from within the XenForo template system, there is no need for anything else, the base URL is already in place.

    Assuming that you pass the value of XenForo_Application::$externalDataPath out via the $viewParams in your public controller, you can simply link to it directly - ie:
    PHP:
    return $this->responseView('My_ViewPublic_Something''some_template', array(
     
    'dataPath' => XenForo_Application::$externalDataPath));
    HTML:
    <a href="{$dataPath}/path/to/something">Something</a>
    Or am I missing something here? If so, what exactly are you trying to achieve?[/php]
     

Share This Page