Mahtab Alam
Mahtab Alam

Reputation: 54

How to update the uploaded image style on S3 when image is replace by same filename?

When replacing image with same file name is not updating the image style on S3. I tried to upload the image to S3 and the image gets uploaded successfully but the image style is not updated/changed for the uploaded image on S3.

Upvotes: 1

Views: 1850

Answers (1)

user8550844
user8550844

Reputation: 293

please use following code:

  $imgage_path_uri = 'Path of image URI';
$derivative_uri = image_style_path('my_image_style', $imgage_path_uri);
$style = image_style_load('my_image_style');
// Generate derivative
$generated = image_style_create_derivative($style, $imgage_path_uri, $derivative_uri);
$s3_bucket = variable_get('s3fs_bucket', '');
$derivative_uri_u = str_replace('s3://', 'public://', $derivative_uri);
$derivative_uri = str_replace('s3://', '', $derivative_uri);
try{
  $config = _s3fs_get_config();
  $s3 = _s3fs_get_amazons3_client($config);
}catch(S3fsException $e){
  form_set_error('form', $e->getMessage());
   return FALSE;
}
$result = $s3->deleteObject(array(
  'Bucket' => $s3_bucket,
  'Key'    => $derivative_uri
));
//create destination 
$destination = file_stream_wrapper_uri_normalize($filepath .'/'. $filename);
//move your image file to from source to destination
$destination = file_unmanaged_move($source, $destination, FILE_EXISTS_REPLACE);
$dafd = image_style_create_derivative($style, $destination, $derivative_uri_u);
$result = $s3->putObject(array(
    'Bucket'       => $s3_bucket,
    'Key'          => $derivative_uri,
    'SourceFile'   => $derivative_uri_u,
    'ACL'          => 'public-read',
));

file_unmanaged_delete($derivative_uri_u);

Upvotes: 2

Related Questions