1 image.gd.inc image_gd_save(stdClass $image, $destination)

GD helper to write an image resource to a destination file.


$image: An image object.

$destination: A string file URI or path where the image should be saved.

Return value

TRUE or FALSE, based on success.:

See also


Related topics


core/modules/system/image.gd.inc, line 264
GD2 toolkit for image manipulation within Backdrop.


function image_gd_save(stdClass $image, $destination) {
  $scheme = file_uri_scheme($destination);
  // Work around lack of stream wrapper support in imagejpeg() and imagepng().
  if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
    // If destination is not local, save image to temporary local file.
    $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
    if (!isset($local_wrappers[$scheme])) {
      $permanent_destination = $destination;
      $destination = backdrop_tempnam('temporary://', 'gd_');
    // Convert stream wrapper URI to normal path.
    $destination = backdrop_realpath($destination);

  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
  $function = 'image' . $extension;
  if (!function_exists($function)) {
    return FALSE;
  if ($extension == 'jpeg') {
    $success = $function($image->resource, $destination, config_get('system.core', 'image_jpeg_quality'));
  else {
    // Always save PNG images with full transparency.
    if ($extension == 'png') {
      imagealphablending($image->resource, FALSE);
      imagesavealpha($image->resource, TRUE);
    $success = $function($image->resource, $destination);
  // Move temporary local file to remote destination.
  if (isset($permanent_destination) && $success) {
    return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
  return $success;