Lessons

Cropping monophonic fields in images

Often the image contains plain white frames. They do not carry any useful function and only increase the size of the image. This slows down the loading of the image and the page as a whole.

Remove the empty white edges from the image using the “Crop to content” function in graphic editors.

Demonstration of the work and code of the PHP function cropping images:

Demonstration
PHP code
HTML code
<?php
/**
 * Вам нужно установить imagemagick.
 * Для убунту это делается так apt install imagemagick
 */

$data = exec("magick photo.jpg -trim photo_crop.jpg", $output);
$data = array_pop($output);
?><!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .grid {
                display: grid;
                grid-template-columns: 1fr 1fr 1fr;
                grid-gap: 10px;
            }
            img {
                width: 100%;
                border:2px solid black;
                border-radius: 5px;
            }
        </style>
    </head>
    <body>
        <div class="grid">
            <div>
                Исходная картинка
                <img src="./photo.jpg" />
            </div>
            <div>
                Обрезанная картинка
                <img src="./photo_crop.jpg" />
            </div>
        </div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .grid {
                display: grid;
                grid-template-columns: 1fr 1fr 1fr;
                grid-gap: 10px;
            }
            img {
                width: 100%;
                border:2px solid black;
                border-radius: 5px;
            }
        </style>
    </head>
    <body>
        <div class="grid">
            <div>
                Исходная картинка
                <img src="./photo.jpg" />
            </div>
            <div>
                Обрезанная картинка
                <img src="./photo_crop.jpg" />
            </div>
        </div>
    </body>
</html>

Tariff plans for working with the service

We use cookies. By continuing to use the site, you agree to the processing of personal data in accordance with privacy policy. I agree