首页 > 解决方案 > 如何修复水印位置php?

问题描述

我希望将水印应用于选定的图像,在 5 个不同的位置,我有 3 个函数用于这些位置中心、bottom_right、top_left,它们适用于所有图像,但我不知道如何做 bottom_left、top_right。我尝试过,但使用不同的图像,水印会消失或出现错误的地方,这是三个有效的功能。我需要将水印放置在每个图像中的相同位置,例如响应式。

中心:

     $imgs = imagecreatefromjpeg("/var/www/$f$p/$n");

// Load the logo image
                    $logoImage = imagecreatefrompng("$waterpath/$watername");
                    imagealphablending($logoImage, true);

// Get dimensions
                    $imageWidth = imagesx($imgs);
                    $imageHeight = imagesy($imgs);

                    $logoWidth = imagesx($logoImage);
                    $logoHeight = imagesy($logoImage);

// Paste the logo
                    imagecopy(
                    // destination
                        $imgs,
                        // source
                        $logoImage,
                        // destination x and y
                        ($imageWidth - $logoWidth) / 2, ($imageHeight - $logoHeight) / 2,
                        // source x and y
                        0, 0,
                        // width and height of the area of the source to copy
                        $logoWidth, $logoHeight);
                    $watermark->update(array('status' => 'done'));

                    imagejpeg($imgs, "/var/www/$f$p/$n");

左上方:

     $imgs = imagecreatefromjpeg("/var/www/$f$p/$n");

// Load the logo image
                    $logoImage = imagecreatefrompng("$waterpath/$watername");
                    imagealphablending($logoImage, true);

                    // Get dimensions
                    $imageWidth = imagesx($imgs);
                    $imageHeight = imagesy($imgs);

                    $logoWidth = imagesx($logoImage);
                    $logoHeight = imagesy($logoImage);

                    // Paste the logo
                    imagecopy(
                    // destination
                        $imgs,
                        // source
                        $logoImage,
                        // destination x and y
                        ($imageWidth - $logoWidth) / 100, ($imageHeight - $logoHeight) / 100,
                        // source x and y
                        0, 0,
                        // width and height of the area of the source to copy
                        $logoWidth, $logoHeight);


                    imagejpeg($imgs, "/var/www/$f$p/$n");

右下角:

 $imgs = imagecreatefromjpeg("/var/www/$f$p/$n");

// Load the logo image
                    $stamps = imagecreatefrompng("$waterpath/$watername");

                    // Set the margins for the stamp and get the height/width of the stamp image
                    $marge_right = 10;
                    $marge_bottom = 10;
                    $sx = imagesx($stamps);
                    $sy = imagesy($stamps);
                    $imageWidth = imagesx($imgs);
                    $imageHeight = imagesy($imgs);

                    // Copy the stamp image onto our photo using the margin offsets and the photo
                    // width to calculate positioning of the stamp.
                    imagecopy($imgs, $stamps, $imageWidth - $sx - $marge_right, $imageHeight - $sy - $marge_bottom, 0, 0, imagesx($stamps), imagesy($stamps));

                    imagejpeg($imgs, "/var/www/$f$p/$n");

我尝试了这些功能,但它们不适用于所有图像:

左下方:

 $imgs = imagecreatefromjpeg("/var/www/$f$p/$n");

// Load the logo image
                    $stamps = imagecreatefrompng("$waterpath/$watername");

                    // Set the margins for the stamp and get the height/width of the stamp image
                    $marge_right = 670;
                    $marge_bottom = 10;
                    $sx = imagesx($stamps);
                    $sy = imagesy($stamps);
                    $imageWidth = imagesx($imgs);
                    $imageHeight = imagesy($imgs);


                    // Copy the stamp image onto our photo using the margin offsets and the photo
                    // width to calculate positioning of the stamp.
                    imagecopy($imgs, $stamps, $imageWidth - $sx - $marge_right, $imageHeight - $sy - $marge_bottom, 0, 0, imagesx($stamps), imagesy($stamps));

                    imagejpeg($imgs, "/var/www/$f$p/$n");

右上:

 $imgs = imagecreatefromjpeg("/var/www/$f$p/$n");

// Load the logo image
                    $stamps = imagecreatefrompng("$waterpath/$watername");

                    // Set the margins for the stamp and get the height/width of the stamp image
                    $marge_right = 10;
                    $marge_bottom = 430;
                    $sx = imagesx($stamps);
                    $sy = imagesy($stamps);
                    $imageWidth = imagesx($imgs);
                    $imageHeight = imagesy($imgs);

                    // Copy the stamp image onto our photo using the margin offsets and the photo
                    // width to calculate positioning of the stamp.
                    imagecopy($imgs, $stamps, $imageWidth - $sx - $marge_right, $imageHeight - $sy - $marge_bottom, 0, 0, imagesx($stamps), imagesy($stamps));

                    imagejpeg($imgs, "/var/www/$f$p/$n");

标签: phplaravel

解决方案


推荐阅读