AnswerBun.com

How to save an image resource

Drupal Answers Asked by kosher on November 13, 2021

I have brought an image in from an API and cropped it using imagecrop in a custom module. I now have an image resource. But I cannot figure out how to turn the resource into a file I can save in Drupal or just save the resource directly as a file through Drupal. I have tried variations of file_get_contents and file_save_data. Any help would be appreciated.

When I use the following:

$cropped_image = imagecrop($image_resource, $rect);
$final_file = file_save_data($cropped_image);
$type = array('type' => $content_type);
$node = Drupal::entityTypeManager()
  ->getStorage('node')
  ->create($type);
$node->field_image = array(
  'target_id' => $image_file->id(),
);
$node->save();

I get the error "Call to a member function id() on bool in" on the $node->field_image line. That has made me think that the resource is not getting saved into a file with file_save_data() (obviously, not exactly sure what is happening).

When I included a file_get_contents($cropped_image) and then process that through file_save_data (as I do when I bring in an image from a URL), I end up with an error that the system is unable to handle the request in nothing in the recent log messages.

I have tried various other methods as well with no joy.

One Answer

I may have had the code correct at some time yesterday, but I was running out of memory on my localhost. I ended up rewriting some of the code and - once I realized I was having memory issues - moved everything onto the server, where it all worked like a charm. Sorry for asking the question. And, thanks, Simon, for provoking me to question everything anew with your comment.

Here is my final code;

            $cropped_image = imagecrop($precrop_resource, $rect);
            imagedestroy($precrop_resource);

            $file_path = Drupal::service('file_system')->realpath('public://');
            imagejpeg($cropped_image, $file_path . '/temp-image.jpg');

            // load the image
            $cropped_data = file_get_contents($file_path . '/temp-image.jpg');
            $cropped_file = file_save_data($cropped_data);
            unset($cropped_data);

Then I added the image to the node object and saved the node.

EDIT:

I eventually realized a couple issues with the above code. First, it was not just my memory on the localhost; processing 10MB+ jpegs would exhaust the memory on the server as well because the GD2 image tools process everything in uncompressed format. Second, the files were saving without .jpg on their filenames, so using them could be a problem.

So, I ended up using the ImageMagick to process things and added another variable to the file_save_data function call.

Here is my new and improved final code:

            // crop the image
            $precropped_image = new Imagick($precrop_file->getFileUri());           
            $precropped_image->cropImage($rect['width'], $rect['height'], $rect['x'], $rect['y']);
            $precropped_image->writeImage($file_path . '/temp-image.jpg');

            // save the cropped image in the Drupal DB
            $cropped_data = file_get_contents($file_path . '/temp-image.jpg');
            $cropped_file = file_save_data($cropped_data, 'public://' . $image_title . '-og.jpg');

Answered by kosher on November 13, 2021

Add your own answers!

Related Questions

Solr index fields not showing a field

1  Asked on January 12, 2021 by heather

     

Programmatically get product name by using order variable

2  Asked on January 7, 2021 by selva

   

Applying patch on core module failing

0  Asked on January 6, 2021 by nabil

   

Custom module is not working

1  Asked on December 30, 2020 by ts-guhan

 

Unlimited field collection pagination in node edit page

1  Asked on December 30, 2020 by dmk-i-t

     

Error: Private files directory not fully protected

2  Asked on December 28, 2020 by glenviewjeff

     

How to add Autocomplete to a Views Custom Textfield Filter

1  Asked on December 27, 2020 by laceysanderson

   

Cannot disable or replace logo

1  Asked on December 25, 2020 by digital-fire

   

Webform onchange added with hook_form_id_alter

1  Asked on December 25, 2020 by richard-luckhurst

     

Form API placeholder attribute rendered as “- Select -“

1  Asked on December 24, 2020 by hotwebmatter

   

Adding a dependencies in theme info file

1  Asked on December 23, 2020 by aryashree-pritikrishna

   

Javascript in Drupal

1  Asked on December 23, 2020 by zephirus

   

Titles not showing for tabs pages

2  Asked on December 21, 2020 by andyw

   

Can’t connect to remote DigitalOcean database

1  Asked on December 18, 2020 by ymdahi

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP