Verified Commit 6e7f64fe authored by Juraj Nemec's avatar Juraj Nemec
Browse files

Issue #3312970 by jan kellermann: Wrong image size because of bug in 7.91

parent ef48d5d5
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -897,10 +897,8 @@ function image_style_deliver($style, $scheme = NULL) {

  if ($success) {
    $image = image_load($derivative_uri);
    $headers += array(
      'Content-Type' => $image->info['mime_type'],
      'Content-Length' => $image->info['file_size']
    );
    $headers['Content-Type'] = $image->info['mime_type'];
    $headers['Content-Length'] = $image->info['file_size'];
    file_transfer($image->source, $headers);
  }
  else {
+157 −0
Original line number Diff line number Diff line
@@ -2023,4 +2023,161 @@ class ImageStyleFlushTest extends ImageFieldTestCase {
    // Post flush, expected no image in the 'private' wrapper.
    $this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', array('%style' => $style['name'], '%wrapper' => 'private')));
  }

}

/**
 * Tests for correct file size, dimensions and HTTP headers after scaling.
 */
class ImageStylesHTTPHeadersTestCase extends ImageFieldTestCase {

  /**
   * Generated style name.
   *
   * @var string
   */
  protected $styleName;

  /**
   * ID of generated style.
   *
   * @var int
   */
  protected $styleId;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Image styles HTTP headers tests',
      'description' => 'Tests file size, dimensions and HTTP headers of image styles generated with scaling.',
      'group' => 'Image',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp('image_module_test');

    $this->styleName = 'style_foo_scale';
    $style = image_style_save(array(
      'name' => $this->styleName,
      'label' => $this->randomString(),
    ));
    $this->styleId = $style['isid'];
  }

  /**
   * Test image_style_url() with a file with public scheme and upscaling.
   */
  public function testImageStyleUrlAndPathPublicAndUpscale() {
    $width = 3600;
    $height = 2400;
    $this->_imagestyleUpscale($width, $height);
    $this->_testImageStyleUrlAndPath('public', $width, $height);
  }

  /**
   * Test image_style_url() with a file with private scheme and upscaling.
   */
  public function testImageStyleUrlAndPathPrivateAndUpscale() {
    $width = 3600;
    $height = 2400;
    $this->_imagestyleUpscale($width, $height);
    $this->_testImageStyleUrlAndPath('private', $width, $height);
  }

  /**
   * Test image_style_url() with a file with public scheme and downscaling.
   */
  public function testImageStyleUrlAndPathPublicAndDownscale() {
    $width = 180;
    $height = 120;
    $this->_imagestyleUpscale($width, $height);
    $this->_testImageStyleUrlAndPath('public', $width, $height);
  }

  /**
   * Test image_style_url() with a file with private scheme and downscaling.
   */
  public function testImageStyleUrlAndPathPrivateAndDownscale() {
    $width = 180;
    $height = 120;
    $this->_imagestyleUpscale($width, $height);
    $this->_testImageStyleUrlAndPath('private', $width, $height);
  }

  /**
   * Adding upscale effect to imagestyle.
   */
  private function _imagestyleUpscale($width, $height) {
    // Scale an image that is wider than it is high.
    $effect = array(
      'name' => 'image_scale',
      'data' => array(
        'width' => $width,
        'height' => $height,
        'upscale' => TRUE,
      ),
      'isid' => $this->styleId,
    );
    image_effect_save($effect);
  }

  /**
   * Function to scale and test.
   */
  private function _testImageStyleUrlAndPath($scheme, $width, $height) {
    $directory = $scheme . '://styles/' . $this->styleName;
    $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
    $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.');

    // Create an image field that uses the new style.
    $field_name = strtolower($this->randomName(10));
    $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
    $instance = field_info_instance('node', $field_name, 'article');
    $instance['display']['default']['type'] = 'image';
    $instance['display']['default']['settings']['image_style'] = $this->styleName;
    field_update_instance($instance);

    // Create a new node with an image attached.
    $images = $this->drupalGetTestFiles('image');
    // Taking image no. 6 because Apache sets own
    // Content-Length if response is smaller than 8000 bytes.
    $test_image = $images[6];
    $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
    $node = node_load($nid);

    // Get image file from created node.
    $field_items = field_get_items('node', $node, $field_name);
    $file = file_load($field_items[0]['fid']);
    $original_uri = $file->uri;

    // Let the image_module_test module know about this file, so it can claim
    // ownership in hook_file_download().
    variable_set('image_module_test_file_download', $original_uri);

    // Get the URL of a file that has not been generated and try to create it.
    $generated_uri = image_style_path($this->styleName, $original_uri);
    $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
    $generate_url = image_style_url($this->styleName, $original_uri);

    // Fetch the URL that generates the file.
    $this->drupalGet($generate_url);
    $this->assertResponse(200, 'Image was generated at the URL.');
    $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it: ' . $generated_uri);

    // Store generated image info.
    $generated_image_info = image_get_info($generated_uri);

    // Compare result.
    $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], 'Expected Content-Type was reported.');
    $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], 'Expected Content-Length was reported: ' . $generated_image_info['file_size'] . ' vs. ' . $this->drupalGetHeader('Content-Length'));
    $this->assertEqual($width, $generated_image_info['width'], 'Expected width was reported.');
    $this->assertEqual($height, $generated_image_info['height'], 'Expected height was reported.');
  }

}