Method: App42::ImageProcessor::ImageProcessorService#crop

Defined in:
lib/imageProcessor/ImageProcessorService.rb

#crop(name, imagePath, width, height, x, y) ⇒ Object

Crops image based on width, height and x, y coordinates. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls Resizing is done based on the width and height provided

Parameters:

  • name
    • Name of the image to crop

  • imagePath
    • Path of the local file to crop

  • width
    • Width of the image to crop

  • height
    • Height of the image to crop

  • x
    • Coordinate X

  • y
    • Coordinate Y

Returns:

  • Image object containing urls for the original and converted images

Raises:

  • App42Exception



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/imageProcessor/ImageProcessorService.rb', line 450

def crop(name, imagePath, width, height, x, y)
  puts "crop Called "
  puts "Base url #{@base_url}"
  response = nil
  imageObj = nil
  imageObj = Image.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(name, "Name");
  util.throwExceptionIfNullOrBlank(imagePath, "Image Path");
  util.throwExceptionIfNullOrBlank(width, "Width");
  util.throwExceptionIfNullOrBlank(height, "Height");
  util.throwExceptionIfNullOrBlank(x, "x");
  util.throwExceptionIfNullOrBlank(y, "y");
  begin
    file = File.new(imagePath,"rb")
    ext = File.extname(file)
    if((ext.eql?(".jpg") == false) && (ext.eql?(".JPG") == false) && (ext.eql?(".jpeg") == false) && (ext.eql?(".JPEG") == false) && (ext.eql?(".gif") == false) && (ext.eql?(".GIF") == false) && (ext.eql?(".png") == false) && (ext.eql?(".PNG") == false))
      raise TypeError,"The Request parameters are invalid. Only file with extensions jpg, jpeg, gif and png are supported"
    end
    if File.file?(imagePath) == false
      raise App42Exception.new(" File " + imagePath.to_s + " does not exist");
    end
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", name)
    params.store("width", (width.to_i).to_s + "")
    params.store("height", (height.to_i).to_s + "")
    params.store("x", (x.to_i).to_s + "")
    params.store("y", (y.to_i).to_s + "")
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/crop"
    response = connection.imageMultipart(signature, resource_url, query_params, params, imagePath)
    image = ImageProcessorResponseBuilder.new
    imageObj = image.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return imageObj
end