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

How do I store an image from an external URL into a temporary file?

Discussion in 'XenForo Development Discussions' started by Jaxel, Dec 9, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    This is the current code:
        public function buildThumb($mediaID, $thumb)
            $targetLoc = XenForo_Helper_File::getExternalDataPath().'/media/'.$mediaID.'.jpg';
            $imageInfo = getimagesize($thumb);
            if ($image = XenForo_Image_Abstract::createFromFile($thumb, $imageInfo[2]))
                $ratio = 160/90;
                $width = $image->getWidth();
                $height = $image->getHeight();
                if ($width/$height > $ratio)
                    $image->thumbnail($width, '90');
                    $image->thumbnail('160', $height);
                $width = $image->getWidth();
                $height = $image->getHeight();
                $offWidth = ($width - 160) / 2;
                $offHeight = ($height - 90) / 2;
                $image->crop($offWidth, $offHeight, '160', '90');
                $image->output(IMAGETYPE_JPEG, $targetLoc);
    The problem is getimagesize wont work unless fURL is enabled. $thumb is the location of the thumbnail. Basically, all I need to do is take $thumb, store it in a temporary file... create the thumbnail image, then delete the temporary file... How would I do all this using Zend_Http?

    This is a brief summary of how the code above works...

    1 - it gets the thumbnail URL which is stored in $thumb...
    2 - gets thumb information (IMAGETYPE constant) from thumbnail URL using getImageSize
    3 - creates image from thumbnail URL and IMAGETYPE using createFromFile
    4 - takes newly created image and thumb-crop-centers the image to 160x90px (within the IF)

    Steps 2 and 3, dont work with my current code unless fURL is enabled. Without fURL, you can't run processes on external entities hosted on different services. So I can't run "getImageSize" (step 2), and I cant run "createFromFile" (step 3).

    So what I need to do is add two new steps:
    1.5 - store image from thumbnail URL into temporary file
    5 - delete temporary file

    How would I do that? There is no "upload" involved.
  2. Shadab

    Shadab Well-Known Member

    This should do:
    $client = new Zend_Http_Client('http://xenforo.com/community/data/avatars/l/0/779.jpg');
    $response $client->request();

    $image $response->getBody();
    $filePath XenForo_Helper_File::getTempDir() . '/' XenForo_Application::generateRandomString(10);


    // Process the image saved as a file ($filePath)

    Also, if you have the EXIF extension enabled in php (not sure if it's enabled by default?), you can replace the call to getimagesize() with exif_imagetype(), which returns the image type directly and is faster too.
    Jeremy, Indigo and Kier like this.
  3. Indigo

    Indigo Active Member

    Whoops, didn't see this thread and ended up posting mine in the Medio thread. Shadab's looks better though since it uses the file helper for the temp dir.
  4. Mike

    Mike XenForo Developer Staff Member

    Just as a clarification, that change would only make sense if the image is known to be a JPEG. That's not the case with avatars (any more). getimagesize() is probably safer, and gives you the image type constant that's used by our image manipulation classes.
  5. Shadab

    Shadab Well-Known Member

    Oopsie. You are right! I overlooked the fact that EXIF metadata can't be used with png's and gif's.

Share This Page