/** * This ensures the resolution of a specific file is within bounds. * The image will be resized if it's too large. */ function testFileValidateImageResolution() { // Non-images. $errors = file_validate_image_resolution($this->nonImage); $this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File'); $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100'); $this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File'); // Minimum size. $errors = file_validate_image_resolution($this->image); $this->assertEqual(count($errors), 0, 'No errors for an image when there is no minimum or maximum resolution.', 'File'); $errors = file_validate_image_resolution($this->image, 0, '200x1'); $this->assertEqual(count($errors), 1, 'Got an error for an image that was not wide enough.', 'File'); $errors = file_validate_image_resolution($this->image, 0, '1x200'); $this->assertEqual(count($errors), 1, 'Got an error for an image that was not tall enough.', 'File'); $errors = file_validate_image_resolution($this->image, 0, '200x200'); $this->assertEqual(count($errors), 1, 'Small images report an error.', 'File'); // Maximum size. if ($this->container->get('image.factory')->getToolkitId()) { // Copy the image so that the original doesn't get resized. copy('core/misc/druplicon.png', 'temporary://druplicon.png'); $this->image->setFileUri('temporary://druplicon.png'); $errors = file_validate_image_resolution($this->image, '10x5'); $this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File'); $image = $this->container->get('image.factory')->get($this->image->getFileUri()); $this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File'); $this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File'); // Once again, now with negative width and height to force an error. copy('core/misc/druplicon.png', 'temporary://druplicon.png'); $this->image->setFileUri('temporary://druplicon.png'); $errors = file_validate_image_resolution($this->image, '-10x-5'); $this->assertEqual(count($errors), 1, 'An error reported for an oversized image that can not be scaled down.', 'File'); drupal_unlink('temporary://druplicon.png'); } else { // TODO: should check that the error is returned if no toolkit is available. $errors = file_validate_image_resolution($this->image, '5x10'); $this->assertEqual(count($errors), 1, 'Oversize images that cannot be scaled get an error.', 'File'); } }
public function downloadProfilePic($picture_url, $id, $user) { if (user_picture_enabled()) { // Make sure that we have everything we need if (!$picture_url || !$id) { return FALSE; } $picture_config = \Drupal::config('field.field.user.user.user_picture'); $pictureDirectory = $picture_config->get('settings.file_directory'); $data = array('user' => $user); $pictureDirectory = \Drupal::token()->replace($pictureDirectory, $data); // Check target directory from account settings and make sure it's writeable $directory = file_default_scheme() . '://' . $pictureDirectory; if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) { \Drupal::logger('sociallogin')->error('Could not save profile picture. Directory is not writeable: @directory', array('@dir' => $directory)); } // Download the picture. Facebook API always serves the images in jpg format. $destination = $directory . '/' . SafeMarkup::checkPlain($id) . '.jpg'; $request = @file_get_contents($picture_url); if ($request) { $picture_file_data = file_save_data($request, $destination, FILE_EXISTS_REPLACE); $maxResolution = $picture_config->get('settings.max_resolution'); $minResolution = $picture_config->get('settings.min_resolution'); file_validate_image_resolution($picture_file_data, $maxResolution, $minResolution); $user->set('user_picture', $picture_file_data->id()); $user->save(); unset($_SESSION['messages']['status']); return TRUE; } // Something went wrong \Drupal::logger('sociallogin')->error('Could not save profile picture. Unhandled error.'); return FALSE; } }