Cropping an image in AMP-HTML

308 Views Asked by At

I have the cropWidth, cropHeight and the start co-ordinates of the image from which I need to crop. How should I crop an <amp-img> using these information ?

For example, if the dimensions of the image is 10 X 10, and suppose cropwidth is 2 and cropheight is 3 and the start location is 3,5. This means I want the part of the image described by the rectangle with (3,5) as the top-left point and (5,8) as the top-right point.

The following is what I have tried and it is not working properly:

HTML code

<div style="width:400px;height:200px;position: relative;">
        <amp-img class="cropped2" width="2px" height="1px" layout="responsive" src="https://hips.hearstapps.com/ghk.h-cdn.co/assets/17/30/2560x1280/landscape-1500925839-golden-retriever-puppy.jpg?resize=480:*">
        </amp-img>
</div>

CSS code:

.cropped2 {
         width: 100px; /* width of container */
            height: 100px; /* height of container */
            object-fit: cover;
            object-position: 20% 10px; 
            border: 5px solid black;
    }

Can someone tell why this is not working and give a working solution for the same?

1

There are 1 best solutions below

0
On

Next time, try inserting the working code so that it can be run. Look at my example.

Like your code: I'm not sure if the option with layout= "responsive" and object-fit: cover is a good idea. Look at my example, I hope it will help you.

<!doctype html>
<html amp lang="en">

<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <title>Hello, AMPs</title>
  <link rel="canonical" href="https://amp.dev/documentation/guides-and-tutorials/start/create/basic_markup/">
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
  <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "NewsArticle",
      "headline": "Open-source framework for publishing content",
      "datePublished": "2015-10-07T12:02:41Z",
      "image": [
        "logo.jpg"
      ]
    }
  </script>
  <style amp-boilerplate>
    body {
      -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      animation: -amp-start 8s steps(1, end) 0s 1 normal both
    }

    @-webkit-keyframes -amp-start {
      from {
        visibility: hidden
      }

      to {
        visibility: visible
      }
    }

    @-moz-keyframes -amp-start {
      from {
        visibility: hidden
      }

      to {
        visibility: visible
      }
    }

    @-ms-keyframes -amp-start {
      from {
        visibility: hidden
      }

      to {
        visibility: visible
      }
    }

    @-o-keyframes -amp-start {
      from {
        visibility: hidden
      }

      to {
        visibility: visible
      }
    }

    @keyframes -amp-start {
      from {
        visibility: hidden
      }

      to {
        visibility: visible
      }
    }
  </style><noscript>
    <style amp-boilerplate>
      body {
        -webkit-animation: none;
        -moz-animation: none;
        -ms-animation: none;
        animation: none
      }
    </style>
  </noscript>

  <style amp-custom>
    .wrapper {
      margin: 15px;
      position: relative;
    }

    .wrapper_one {
      height: 100px;
      width: 100px;
    }

    .wrapper_two {
      height: 150px;
      width: 300px;
    }

    .cropped2 img {
      object-position: 20% 10px;
      border: 5px solid black;
      object-fit: cover;
    }
  </style>
</head>

<body>

  <h1>Welcome to the mobile web</h1>

  <div class="wrapper wrapper_one">
    <amp-img class="cropped2" layout="fill" src="https://hips.hearstapps.com/ghk.h-cdn.co/assets/17/30/2560x1280/landscape-1500925839-golden-retriever-puppy.jpg?resize=480:*">
    </amp-img>
  </div>

  <div class="wrapper wrapper_two">
    <amp-img class="cropped2" layout="fill" src="https://hips.hearstapps.com/ghk.h-cdn.co/assets/17/30/2560x1280/landscape-1500925839-golden-retriever-puppy.jpg?resize=480:*">
    </amp-img>
  </div>

</body>

</html>