0

The code outputs and does exactly what I intend it to do, but it throws the following notices:

Warning: Illegal string offset 'effects' in image_style_create_derivative() (line 928 of /var/www/html/modules/image/image.module). Warning: Invalid argument supplied for foreach() in image_style_create_derivative() (line 928 of /var/www/html/modules/image/image.module).

The code I am using is so:

(Builds the actual image style from base uri if the file doesn't currently exist)

/**
 * Check for and generate image derivatives if none exist.
 *
 * @param string $style
 *   Image style to check.
 * @param string $uri
 *   URI of the base image.
 *
 * @return false|string
 *   Returns uri for the image that should now exist.
 */
function _srv_image_derivitives($style, $uri) {

  // So I have something to return.
  $final_uri = image_style_path($style, $uri);

  // Gets the absolute file name to the file to check for.
  $filename = drupal_realpath($final_uri);

  // If it's not there, make it there.
  if (!file_exists($filename)) {
    $style_load = image_style_load($style);

    if (count($style_load)) {
      image_style_create_derivative(
        $style,
        $uri,
        image_style_path(
          $style,
          $uri
        )
      );
    }
  }
  return $final_uri;
}

(Builds an array of images that will be converted to a json array for an rest post)

/**
 * Build derivatives array from base image uri.
 *
 * @param string $image_uri
 *   The base image to construct.
 *
 * @return array
 *   Array of image derivatives.
 */
function _srv_build_derivative_array($image_uri) {
  // Set up the image styles to be used.
  $image_styles = array(
    'carousel',
    'rotator',
    'leadlandscape',
    'focus',
    'linkthumbnail',
    'topstorythumbnail',
    'topnavthumbnail',
    'lateststoriesthumbnail',
    'headline',
    'subcategorylead',
    'opinionlead',
    'entertainmentlead',
    'entertainmentlatestthumbnail',
    'gallerylandscape',
    'gallerythumbnail',
    'reporterstory',
    'galleryportrait',
  );
  $images_array = array();

  foreach ($image_styles as $key_one => $style) {
    $image_style_uri = _srv_image_derivitives($style, $image_uri);

    // EFS path instead of public:// uri.
    $images_array[$style] = str_replace("public://", "/data/sites_default_files/", $image_style_uri);
  }
  return $images_array;
}
3
  • Above code is correct, but you can check what is value of $style before passing to image_style_create_derivative() function. From error it seems that the $style doesn't has effects key in it. Also check if this is for all image styles or only for few, if only for few check if those image styles has any effect added into them
    – Yogesh
    Sep 27, 2016 at 12:08
  • Throws the warning each image style that exists. The only thing in each image style is a single scale and crop effect.
    – Remog
    Sep 27, 2016 at 12:10
  • Log $style variable into watchdog and check if it has effects key in it.
    – Yogesh
    Sep 27, 2016 at 12:26

1 Answer 1

0

The first argument should be array in the image_style_create_derivative() function, so the error is in the caller code.

Parameters

$style: An image style array.

$source: Path of the source file.

$destination: Path or URI of the destination file.

https://api.drupal.org/api/drupal/modules%21image%21image.module/function/image_style_create_derivative/7.x

There is a incorrect sample in the comments at api.drupal.org.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.