コード例 #1
0
/**
 * Return a Gravatar URL if one exists for the given user.
 *
 * @param string  $email         Email address of the user
 * @param object  $size          Maximum size of the image
 * @param boolean $notfound
 *
 * @returns string The URL of the image or FALSE if none was found
 */
function remote_avatar($email, $size, $notfound)
{
    if (!get_config('remoteavatars')) {
        return false;
    }
    require_once 'file.php';
    $md5sum = md5(strtolower($email));
    $s = 100;
    $newsize = image_get_new_dimensions($s, $s, $size);
    if ($newsize) {
        $s = min($newsize['w'], $newsize['h']);
    }
    $baseurl = 'http://www.gravatar.com/avatar/';
    if (get_config('remoteavatarbaseurl')) {
        $baseurl = get_config('remoteavatarbaseurl');
    }
    return "{$baseurl}{$md5sum}.jpg?r=g&s={$s}&d=" . urlencode($notfound);
}
コード例 #2
0
ファイル: user.php プロジェクト: patkira/mahara
/**
 * Return a Gravatar URL if one exists for the given user.
 *
 * @param string  $email         Email address of the user
 * @param object  $size          Maximum size of the image
 * @param boolean $notfound      The value to return if the avatar is not found
 *
 * @returns string The URL of the image or $notfound if none was found
 */
function remote_avatar($email, $size, $notfound)
{
    if (!get_config('remoteavatars')) {
        return false;
    }
    require_once 'file.php';
    $md5sum = md5(strtolower($email));
    $s = 100;
    $newsize = image_get_new_dimensions($s, $s, $size);
    if ($newsize) {
        $s = min($newsize['w'], $newsize['h']);
    }
    $baseurl = 'http://www.gravatar.com/avatar/';
    if (is_https() === true) {
        $baseurl = 'https://secure.gravatar.com/avatar/';
    }
    if (get_config('remoteavatarbaseurl')) {
        $baseurl = get_config('remoteavatarbaseurl');
    }
    // Check if it is a valid avatar
    $result = mahara_http_request(array(CURLOPT_URL => "{$baseurl}{$md5sum}.jpg?d=404", CURLOPT_HEADER => true, CURLOPT_NOBODY => true), true);
    if (!$result || $result->error || $result->info['http_code'] == 404) {
        return $notfound;
    }
    return "{$baseurl}{$md5sum}.jpg?r=g&s={$s}";
}
コード例 #3
0
ファイル: file.php プロジェクト: rboyatt/mahara
/**
 * Given a path under dataroot, an ID and a size, return the path to a file
 * matching all criteria.
 *
 * If the file with the ID exists but not of the correct size, this function
 * will make a copy that is resized to the correct size.
 *
 * @param string $path The base path in dataroot where the image is stored. For
 *                     example, 'artefact/file/profileicons/' for profile
 *                     icons
 * @param int $id      The ID of the image to return. Is typically the ID of an
 *                     artefact
 * @param mixed $size  The size the image should be.
 *
 *                      As a two element hash with 'w' and 'h' keys:
 *                     - If 'w' and 'h' are not empty, the image will be
 *                       exactly that size
 *                     - If just 'w' is not empty, the image will be that wide,
 *                       and the height will be set to make the image scale
 *                       correctly
 *                     - If just 'h' is not empty, the image will be that high,
 *                       and the width will be set to make the image scale
 *                       correctly
 *                     - If neither are set or the parameter is not set, the
 *                       image will not be resized
 *
 *                     As a number, the path returned will have the largest side being
 *                     the length specified.
 * @return string The path on disk where the appropriate file resides, or false
 *                if an appropriate file could not be located or generated
 */
function get_dataroot_image_path($path, $id, $size = null)
{
    global $THEME;
    $dataroot = get_config('dataroot');
    $imagepath = $dataroot . $path;
    if (substr($imagepath, -1) == '/') {
        $imagepath = substr($imagepath, 0, -1);
    }
    if (!is_dir($imagepath) || !is_readable($imagepath)) {
        return false;
    }
    // Work out the location of the original image
    $originalimage = $imagepath . '/originals/' . $id % 256 . "/{$id}";
    // If the original has been deleted, then don't show any image, even a cached one.
    // delete_image only deletes the original, not any cached ones, so we have
    // to make sure the original is still around
    if (!is_readable($originalimage)) {
        return false;
    }
    if (!$size) {
        // No size has been asked for. Return the original
        return $originalimage;
    } else {
        // Check if the image is available in the size requested
        $sizestr = serialize($size);
        $md5 = md5("{$id}.{$sizestr}");
        $resizedimagedir = $imagepath . '/resized/';
        check_dir_exists($resizedimagedir);
        for ($i = 0; $i <= 2; $i++) {
            $resizedimagedir .= substr($md5, $i, 1) . '/';
            check_dir_exists($resizedimagedir);
        }
        $resizedimagefile = "{$resizedimagedir}{$md5}.{$id}";
        //.$sizestr";
        if (is_readable($resizedimagefile)) {
            return $resizedimagefile;
        }
        // Image is not available in this size. If there is a base image for
        // it, we can make one however.
        if (is_readable($originalimage) && filesize($originalimage)) {
            $imageinfo = getimagesize($originalimage);
            $originalmimetype = $imageinfo['mime'];
            // gd can eat a lot of memory shrinking large images, so use a placeholder image
            // here if necessary
            if (isset($imageinfo['bits'])) {
                $bits = $imageinfo['bits'];
            } else {
                if ($imageinfo['mime'] == 'image/gif') {
                    $bits = 8;
                }
            }
            if (isset($imageinfo[0]) && isset($imageinfo[1]) && !empty($bits)) {
                $approxmem = $imageinfo[0] * $imageinfo[1] * ($bits / 8) * (isset($imageinfo['channels']) ? $imageinfo['channels'] : 3);
            }
            if (empty($approxmem) || $approxmem > get_config('maximageresizememory')) {
                log_debug("Refusing to resize large image {$originalimage} {$originalmimetype} " . $imageinfo[0] . 'x' . $imageinfo[1] . ' ' . $imageinfo['bits'] . '-bit');
                $originalimage = $THEME->get_path('images/no_thumbnail.png');
                if (empty($originalimage) || !is_readable($originalimage)) {
                    return false;
                }
                $imageinfo = getimagesize($originalimage);
                $originalmimetype = $imageinfo['mime'];
            }
            $format = 'png';
            switch ($originalmimetype) {
                case 'image/jpeg':
                case 'image/jpg':
                    $format = 'jpeg';
                    $oldih = imagecreatefromjpeg($originalimage);
                    break;
                case 'image/png':
                    $oldih = imagecreatefrompng($originalimage);
                    break;
                case 'image/gif':
                    $format = 'gif';
                    $oldih = imagecreatefromgif($originalimage);
                    break;
                case 'image/bmp':
                case 'image/x-bmp':
                case 'image/ms-bmp':
                case 'image/x-ms-bmp':
                    if (!extension_loaded('imagick') || !class_exists('Imagick')) {
                        log_info('Bitmap image detected for resizing, but imagick extension is not available');
                        return false;
                    }
                    $ih = new Imagick($originalimage);
                    if (!($newdimensions = image_get_new_dimensions($ih->getImageWidth(), $ih->getImageHeight(), $size))) {
                        return false;
                    }
                    $ih->resizeImage($newdimensions['w'], $newdimensions['h'], imagick::FILTER_LANCZOS, 1);
                    if ($ih->writeImage($resizedimagefile)) {
                        return $resizedimagefile;
                    }
                    return false;
                default:
                    return false;
            }
            if (!$oldih) {
                return false;
            }
            $oldx = imagesx($oldih);
            $oldy = imagesy($oldih);
            if (!($newdimensions = image_get_new_dimensions($oldx, $oldy, $size))) {
                return false;
            }
            $newih = imagecreatetruecolor($newdimensions['w'], $newdimensions['h']);
            if ($originalmimetype == 'image/png' || $originalmimetype == 'image/gif') {
                // Create a new destination image which is completely
                // transparent and turn off alpha blending for it, so that when
                // the PNG source file is copied, the alpha channel is retained.
                // Thanks to http://alexle.net/archives/131
                $background = imagecolorallocate($newih, 0, 0, 0);
                imagecolortransparent($newih, $background);
                imagealphablending($newih, false);
                imagecopyresampled($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
                imagesavealpha($newih, true);
            } else {
                // imagecopyresized is faster, but results in noticeably worse image quality.
                // Given the images are resized only once each time they're
                // made, I suggest you just leave the good quality one in place
                imagecopyresampled($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
                //imagecopyresized($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
            }
            $outputfunction = "image{$format}";
            $result = $outputfunction($newih, $resizedimagefile);
            if ($result) {
                return $resizedimagefile;
            }
        }
        // end attempting to build a resized image
    }
    // Image not available in any size
    return false;
}
コード例 #4
0
/**
 * Return a Gravatar URL if one exists for the given user.
 *
 * @param string  $email         Email address of the user
 * @param object  $size          Maximum size of the image
 * @param boolean $notfound
 *
 * @returns string The URL of the image or FALSE if none was found
 */
function remote_avatar($email, $size, $notfound)
{
    if (!get_config('remoteavatars')) {
        return false;
    }
    require_once 'file.php';
    $md5sum = md5(strtolower($email));
    $s = 100;
    $newsize = image_get_new_dimensions($s, $s, $size);
    if ($newsize) {
        $s = min($newsize['w'], $newsize['h']);
    }
    $baseurl = 'http://www.gravatar.com/avatar/';
    if (is_https() === true) {
        $baseurl = 'https://secure.gravatar.com/avatar/';
    }
    if (get_config('remoteavatarbaseurl')) {
        $baseurl = get_config('remoteavatarbaseurl');
    }
    // Check if it is a valid avatar
    $result = @get_headers("{$baseurl}{$md5sum}.jpg?d=404");
    if (!$result || preg_match("#^HTTP/\\d+\\.\\d+ 404 #i", $result[0])) {
        return $notfound;
    }
    return "{$baseurl}{$md5sum}.jpg?r=g&s={$s}";
}
コード例 #5
0
ファイル: file.php プロジェクト: Br3nda/mahara
/**
 * Given a path under dataroot, an ID and a size, return the path to a file
 * matching all criteria.
 *
 * If the file with the ID exists but not of the correct size, this function
 * will make a copy that is resized to the correct size.
 *
 * @param string $path The base path in dataroot where the image is stored. For 
 *                     example, 'artefact/file/profileicons/' for profile 
 *                     icons
 * @param int $id      The ID of the image to return. Is typically the ID of an 
 *                     artefact
 * @param mixed $size  The size the image should be.
 *
 *                      As a two element hash with 'w' and 'h' keys:
 *                     - If 'w' and 'h' are not empty, the image will be 
 *                       exactly that size
 *                     - If just 'w' is not empty, the image will be that wide, 
 *                       and the height will be set to make the image scale 
 *                       correctly
 *                     - If just 'h' is not empty, the image will be that high, 
 *                       and the width will be set to make the image scale 
 *                       correctly
 *                     - If neither are set or the parameter is not set, the 
 *                       image will not be resized
 *
 *                     As a number, the path returned will have the largest side being 
 *                     the length specified.
 * @return string The path on disk where the appropriate file resides, or false 
 *                if an appropriate file could not be located or generated
 */
function get_dataroot_image_path($path, $id, $size = null)
{
    $dataroot = get_config('dataroot');
    $imagepath = $dataroot . $path;
    if (substr($imagepath, -1) == '/') {
        $imagepath = substr($imagepath, 0, -1);
    }
    if (!is_dir($imagepath) || !is_readable($imagepath)) {
        return false;
    }
    // Work out the location of the original image
    $originalimage = $imagepath . '/originals/' . $id % 256 . "/{$id}";
    // If the original has been deleted, then don't show any image, even a cached one.
    // delete_image only deletes the original, not any cached ones, so we have
    // to make sure the original is still around
    if (!is_readable($originalimage)) {
        return false;
    }
    if (!$size) {
        // No size has been asked for. Return the original
        return $originalimage;
    } else {
        // Check if the image is available in the size requested
        $sizestr = serialize($size);
        $md5 = md5("{$id}.{$sizestr}");
        $resizedimagedir = $imagepath . '/resized/';
        check_dir_exists($resizedimagedir);
        for ($i = 0; $i <= 2; $i++) {
            $resizedimagedir .= substr($md5, $i, 1) . '/';
            check_dir_exists($resizedimagedir);
        }
        $resizedimagefile = "{$resizedimagedir}{$md5}.{$id}";
        //.$sizestr";
        if (is_readable($resizedimagefile)) {
            return $resizedimagefile;
        }
        // Image is not available in this size. If there is a base image for
        // it, we can make one however.
        if (is_readable($originalimage)) {
            $imageinfo = getimagesize($originalimage);
            $originalmimetype = $imageinfo['mime'];
            switch ($originalmimetype) {
                case 'image/jpeg':
                case 'image/jpg':
                    $oldih = imagecreatefromjpeg($originalimage);
                    break;
                case 'image/png':
                    $oldih = imagecreatefrompng($originalimage);
                    break;
                case 'image/gif':
                    $oldih = imagecreatefromgif($originalimage);
                    break;
                case 'image/bmp':
                case 'image/x-bmp':
                case 'image/ms-bmp':
                case 'image/x-ms-bmp':
                    if (!extension_loaded('imagick')) {
                        log_info('Bitmap image detected for resizing, but imagick extension is not available');
                        return false;
                    }
                    $ih = imagick_readimage($originalimage);
                    if (!($newdimensions = image_get_new_dimensions(imagick_getwidth($ih), imagick_getheight($ih), $size))) {
                        return false;
                    }
                    imagick_resize($ih, $newdimensions['w'], $newdimensions['h'], IMAGICK_FILTER_LANCZOS, 1);
                    if (imagick_writeimage($ih, $resizedimagefile)) {
                        return $resizedimagefile;
                    }
                    return false;
                default:
                    return false;
            }
            if (!$oldih) {
                return false;
            }
            $oldx = imagesx($oldih);
            $oldy = imagesy($oldih);
            if (!($newdimensions = image_get_new_dimensions($oldx, $oldy, $size))) {
                return false;
            }
            $newih = imagecreatetruecolor($newdimensions['w'], $newdimensions['h']);
            if ($originalmimetype == 'image/png' || $originalmimetype == 'image/gif') {
                // Create a new destination image which is completely
                // transparent and turn off alpha blending for it, so that when
                // the PNG source file is copied, the alpha channel is retained.
                // Thanks to http://alexle.net/archives/131
                $background = imagecolorallocate($newih, 0, 0, 0);
                imagecolortransparent($newih, $background);
                imagealphablending($newih, false);
                imagecopyresampled($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
                imagesavealpha($newih, true);
            } else {
                // imagecopyresized is faster, but results in noticeably worse image quality.
                // Given the images are resized only once each time they're
                // made, I suggest you just leave the good quality one in place
                imagecopyresampled($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
                //imagecopyresized($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
            }
            $result = imagepng($newih, $resizedimagefile);
            if ($result) {
                return $resizedimagefile;
            }
        }
        // end attempting to build a resized image
    }
    // Image not available in any size
    return false;
}