设置最大高度和宽度

时间:2020-05-14 22:48:47

标签: php

我有完美的代码

只需要创建$watermarkImg

上传大图像时,会自动将其最小化

// Path configuration
$targetDir = "uploads/";
$watermarkImagePath = ($_POST['dsin']);

$statusMsg = '';
if (isset($_POST["submit"])){
    if (!empty($_FILES["file"]["name"])) {
        // File upload path
        $fileName = basename($_FILES["file"]["name"]);
        $targetFilePath = $targetDir . $fileName;
        $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);

        // Allow certain file formats
        $allowTypes = array(
            'jpg',
            'png',
            'jpeg'
        );
        if (in_array($fileType, $allowTypes)) {
            // Upload file to the server
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)) {
                // Load the stamp and the photo to apply the watermark to
                $watermarkImg = ($targetFilePath);
                switch ($fileType)
                {
                    case 'jpg':
                        $watermarkImg = imagecreatefromjpeg($targetFilePath);
                    break;
                    case 'jpeg':
                        $watermarkImg = imagecreatefromjpeg($targetFilePath);
                    break;
                    case 'png':
                        $watermarkImg = imagecreatefrompng($targetFilePath);
                    break;
                    default:
                        $watermarkImg = imagecreatefromjpeg($targetFilePath);
                }
                $im = imagecreatefrompng($watermarkImagePath);
                // Set the margins for the watermark
                $marge_right = 200;
                $marge_bottom = 550;

                // Get the height/width of the watermark image
                $sx = imagesx($watermarkImg);
                $sy = imagesy($watermarkImg);

                // Copy the watermark image onto our photo using the margin offsets and
                // the photo width to calculate the positioning of the watermark.
                imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImg) , imagesy($watermarkImg));

                // Save image and free memory
                imagepng($im, $targetFilePath);
                imagedestroy($im);

                if (file_exists($targetFilePath)) {
                    $statusMsg = "The image with watermark has been uploaded successfully.";
                } else {
                    $statusMsg = "Image upload failed, please try again.";
                }
            } else {
                $statusMsg = "Sorry, there was an error uploading your file.";
            }
        } else {
            $statusMsg = 'Sorry, only JPG, JPEG, and PNG files are allowed to upload.';
        }
    } else {
        $statusMsg = 'Please select a file to upload.';
    }
}
$r = ($_POST['n1']);
$g = ($_POST['n2']);
$b = ($_POST['n3']);
require dirname(__FILE__) . '/I18N/Arabic.php';
$Arabic = new I18N_Arabic('Glyphs');
$font = dirname(__FILE__) . '/hanimation-regular.ttf';

$text = $Arabic->utf8Glyphs($_POST['name']);
$font_size = 45; // Font size is in pixels.
$imagefile = $targetFilePath; // path to image
$img2 = 'imgs/image.png'; // path & name of the image to save on server
// Retrieve bounding text-box:
$txt_space = imagettfbbox($font_size, 0, $font, $text);

// Determine text width and height
$txt_width = abs($txt_space[4] - $txt_space[0]);
$txt_height = abs($txt_space[3] - $txt_space[1]);

function generateRandomString($length = 10){
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0;$i < $length;$i++){
        $randomString .= $characters[rand(0, $charactersLength - 1) ];
    }
    return $randomString;
}

if (file_exists($imagefile)){

    $img = @imagecreatefrompng($imagefile);

    /* Color will change from here */
    $vnum = generateRandomString(10);
    $clr = imagecolorallocate($img, $r, $g, $b);
    $image_width = imagesx($img);
    $image_height = imagesy($img);
    $x = abs($image_width - $txt_width) / 2;
    $y = abs($image_height - $txt_height) / 1.23;

    header("Content-type: image/png");

    imagettftext($img, $font_size, 0, $x, $y, $clr, $font, $text);

    imagepng($img, "" . $vnum . ".png");

    header('Content-type: image/png');

    header("Content-disposition: attachment; filename=" . $vnum . ".png");
    header('Content-Description: File Transfer');
    readfile("" . $vnum . ".png");
    imagedestroy($img);

}

// Display status message
echo $statusMsg;

我有完美的代码

只需要创建$watermarkImg

上传大图像时,会自动将其最小化

0 个答案:

没有答案