In Drupal 7, I'm trying to use image_scale_and_crop() to take an image from an external URL and resize it. Then I want to print it out to my page. Here is my code:

$filename = $bjt_img;
$img_url = "http://www.mydomain.com/uploads/".$filename;
$myimg = image_load($img_url);
$myimg_scaled = image_scale_and_crop($myimg, 100, 80);

print '<img src="'.$myimg_scaled.'">'

I'm getting the message:

Recoverable fatal error: Argument 1 passed to image_scale_and_crop() must be an instance of stdClass, boolean given, called in...

How exactly is the image returned with image_scale_and_crop()?

2 Answers 2


You problem is image_load($img_url) is returning FALSE, which is not an object.

$filename = $bjt_img;
$img_url = "http://www.mydomain.com/uploads/".$filename;
if (isset($myimg = image_load($img_url)) {
  $myimg_scaled = image_scale_and_crop($myimg, 100, 80);
  print '<img src="'.$myimg_scaled.'">'
else {
  print 'error'

Plus I think it must be a file, not a url to a file


The problem is the value returned from image_load(), which is the boolean FALSE because the function is not able to load the image file.

Return value

An image object or FALSE if there was a problem loading the file. The image object has the following properties:

  • 'source' - The original file path.
  • 'info' - The array of information returned by image_get_info()
  • 'toolkit' - The name of the image toolkit requested when the image was loaded.

As for the parameters accepted from the function, the first one is the path to the image file, which normally doesn't mean the URL of the image. Check also that in your PHP installation, the file functions are allowed to open remote files; in the case it is not enabled, passing a URL when a function is expecting a file name would cause the error you noticed.

The documentation for imagecreatefromjpeg(), which is the function that is called if the image toolkit Drupal is using is GD2, and the image is a JPEG one, reports:

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Similar warning is given for imagecreatefrompng(), and the other imagecreatefrom[image-type]() functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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