• 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?

Jaxel

Well-known member
#1
This is the current code:
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');
            }
            else
            {
                $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.
 

Shadab

Well-known member
#2
This should do:
PHP:
$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);

file_put_contents($filePath, $image);

// Process the image saved as a file ($filePath)
Zend_Debug::dump(getimagesize($filePath));

@unlink($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.
 

Indigo

Active member
#3
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.
 

Mike

XenForo developer
Staff member
#4
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.
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.