function image_gd_save

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

Parameters

$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

image_save()

Related topics

File

modules/system/image.gd.inc, line 290

Code

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 = drupal_tempnam('temporary://', 'gd_');
        }
        // Convert stream wrapper URI to normal path.
        $destination = drupal_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, variable_get('image_jpeg_quality', 75));
    }
    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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.